ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro.pm
(Generate patch)

Comparing Coro/Coro.pm (file contents):
Revision 1.13 by root, Tue Jul 17 00:24:14 2001 UTC vs.
Revision 1.22 by root, Mon Jul 23 02:14:19 2001 UTC

14 14
15 sub some_func : Coro { 15 sub some_func : Coro {
16 # some more async code 16 # some more async code
17 } 17 }
18 18
19 yield; 19 cede;
20 20
21=head1 DESCRIPTION 21=head1 DESCRIPTION
22 22
23This module collection manages coroutines. Coroutines are similar to
24Threads but don't run in parallel.
25
26This module is still experimental, see the BUGS section below.
27
28In this module, coroutines are defined as "callchain + lexical variables
29+ @_ + $_ + $@ + $^W), that is, a coroutine has it's own callchain, it's
30own set of lexicals and it's own set of perl's most important global
31variables.
32
33WARNING: When using this module, make sure that, at program end, no
34coroutines are still running OR just call exit before falling off the
35end. The reason for this is that some coroutine of yours might have called
36into a C function, and falling off the end of main:: results in returning
37to that C function instead if to the main C interpreter.
38
39WARNING: Unless you really know what you are doing, do NOT do context
40switches inside callbacks from the XS level. The reason for this is
41similar to the reason above: A callback calls a perl function, this
42perl function does a context switch, some other callback is called, the
43original function returns from it - to what? To the wrong XS function,
44with totally different return values. Unfortunately, this includes
45callbacks done by perl itself (tie'd variables!).
46
47The only workaround for this is to do coroutines on C level.
48
23=cut 49=cut
24 50
25package Coro; 51package Coro;
26 52
27use Coro::State; 53use Coro::State;
28 54
29use base Exporter; 55use base Exporter;
30 56
31$VERSION = 0.05; 57$VERSION = 0.10;
32 58
33@EXPORT = qw(async yield schedule terminate); 59@EXPORT = qw(async cede schedule terminate current);
34@EXPORT_OK = qw($current); 60@EXPORT_OK = qw($current);
35 61
36{ 62{
37 use subs 'async';
38
39 my @async; 63 my @async;
40 64
41 # this way of handling attributes simply is NOT scalable ;() 65 # this way of handling attributes simply is NOT scalable ;()
42 sub import { 66 sub import {
43 Coro->export_to_level(1, @_); 67 Coro->export_to_level(1, @_);
47 my @attrs; 71 my @attrs;
48 for (@_) { 72 for (@_) {
49 if ($_ eq "Coro") { 73 if ($_ eq "Coro") {
50 push @async, $ref; 74 push @async, $ref;
51 } else { 75 } else {
52 push @attrs, @_; 76 push @attrs, $_;
53 } 77 }
54 } 78 }
55 return $old ? $old->($package, $name, @attrs) : @attrs; 79 return $old ? $old->($package, $ref, @attrs) : @attrs;
56 }; 80 };
57 } 81 }
58 82
59 sub INIT { 83 sub INIT {
60 async pop @async while @async; 84 &async(pop @async) while @async;
61 } 85 }
62} 86}
63 87
64=item $main 88=item $main
65 89
67 91
68=cut 92=cut
69 93
70our $main = new Coro; 94our $main = new Coro;
71 95
72=item $current 96=item $current (or as function: current)
73 97
74The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course). 98The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course).
75 99
76=cut 100=cut
77 101
79if ($current) { 103if ($current) {
80 $main->{specific} = $current->{specific}; 104 $main->{specific} = $current->{specific};
81} 105}
82 106
83our $current = $main; 107our $current = $main;
108
109sub current() { $current }
84 110
85=item $idle 111=item $idle
86 112
87The coroutine to switch to when no other coroutine is running. The default 113The coroutine to switch to when no other coroutine is running. The default
88implementation prints "FATAL: deadlock detected" and exits. 114implementation prints "FATAL: deadlock detected" and exits.
94 print STDERR "FATAL: deadlock detected\n"; 120 print STDERR "FATAL: deadlock detected\n";
95 exit(51); 121 exit(51);
96}; 122};
97 123
98# we really need priorities... 124# we really need priorities...
99## my @ready; #d#
100our @ready = (); # the ready queue. hehe, rather broken ;) 125my @ready; # the ready queue. hehe, rather broken ;)
101 126
102# static methods. not really. 127# static methods. not really.
103 128
104=head2 STATIC METHODS 129=head2 STATIC METHODS
105 130
143 # should be done using priorities :( 168 # should be done using priorities :(
144 ($prev, $current) = ($current, shift @ready || $idle); 169 ($prev, $current) = ($current, shift @ready || $idle);
145 Coro::State::transfer($prev, $current); 170 Coro::State::transfer($prev, $current);
146} 171}
147 172
148=item yield 173=item cede
149 174
150Yield to other processes. This function puts the current process into the 175"Cede" to other processes. This function puts the current process into the
151ready queue and calls C<schedule>. 176ready queue and calls C<schedule>, which has the effect of giving up the
177current "timeslice" to other coroutines of the same or higher priority.
152 178
153=cut 179=cut
154 180
155sub yield { 181sub cede {
156 $current->ready; 182 $current->ready;
157 &schedule; 183 &schedule;
158} 184}
159 185
160=item terminate 186=item terminate
164Future versions of this function will allow result arguments. 190Future versions of this function will allow result arguments.
165 191
166=cut 192=cut
167 193
168sub terminate { 194sub terminate {
195 my $self = $current;
169 $current->{_results} = [@_]; 196 $self->{_results} = [@_];
170 &schedule; 197 $current = shift @ready || $idle;
198 Coro::State::transfer(delete $self->{_coro_state}, $current);
199 # cannot return
200 die;
171} 201}
172 202
173=back 203=back
174 204
175# dynamic methods 205# dynamic methods
216 246
217=cut 247=cut
218 248
2191; 2491;
220 250
251=head1 BUGS/LIMITATIONS
252
253 - could be faster, especially when the core would introduce special
254 support for coroutines (like it does for threads).
255 - there is still a memleak on coroutine termination that I could not
256 identify. Could be as small as a single SV.
257 - this module is not well-tested.
258 - if variables or arguments "disappear" (become undef) or become
259 corrupted please contact the author so he cen iron out the
260 remaining bugs.
261 - this module is not thread-safe. You must only ever use this module from
262 the same thread (this requirement might be loosened in the future to
263 allow per-thread schedulers, but Coro::State does not yet allow this).
264
221=head1 SEE ALSO 265=head1 SEE ALSO
222 266
223L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, 267L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>,
224L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. 268L<Coro::Signal>, L<Coro::State>, L<Coro::Event>.
225 269

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines