… | |
… | |
24 | Threads but don't run in parallel. |
24 | Threads but don't run in parallel. |
25 | |
25 | |
26 | This module is still experimental, see the BUGS section below. |
26 | This module is still experimental, see the BUGS section below. |
27 | |
27 | |
28 | In this module, coroutines are defined as "callchain + lexical variables |
28 | In this module, coroutines are defined as "callchain + lexical variables |
29 | + @_ + $_ + $@ + $^W), that is, a coroutine has it's own callchain, it's |
29 | + @_ + $_ + $@ + $^W + C stack), that is, a coroutine has it's own |
30 | own set of lexicals and it's own set of perl's most important global |
30 | callchain, it's own set of lexicals and it's own set of perl's most |
31 | variables. |
31 | important global variables. |
32 | |
|
|
33 | WARNING: When using this module, make sure that, at program end, no |
|
|
34 | coroutines are still running OR just call exit before falling off the |
|
|
35 | end. The reason for this is that some coroutine of yours might have called |
|
|
36 | into a C function, and falling off the end of main:: results in returning |
|
|
37 | to that C function instead if to the main C interpreter. |
|
|
38 | |
|
|
39 | WARNING: Unless you really know what you are doing, do NOT do context |
|
|
40 | switches inside callbacks from the XS level. The reason for this is |
|
|
41 | similar to the reason above: A callback calls a perl function, this |
|
|
42 | perl function does a context switch, some other callback is called, the |
|
|
43 | original function returns from it - to what? To the wrong XS function, |
|
|
44 | with totally different return values. Unfortunately, this includes |
|
|
45 | callbacks done by perl itself (tie'd variables!). |
|
|
46 | |
|
|
47 | The only workaround for this is to do coroutines on C level. |
|
|
48 | |
32 | |
49 | =cut |
33 | =cut |
50 | |
34 | |
51 | package Coro; |
35 | package Coro; |
52 | |
36 | |
53 | use Coro::State; |
37 | use Coro::State; |
54 | |
38 | |
55 | use base Exporter; |
39 | use base Exporter; |
56 | |
40 | |
57 | $VERSION = 0.10; |
41 | $VERSION = 0.13; |
58 | |
42 | |
59 | @EXPORT = qw(async cede schedule terminate current); |
43 | @EXPORT = qw(async cede schedule terminate current); |
60 | @EXPORT_OK = qw($current); |
44 | @EXPORT_OK = qw($current); |
61 | |
45 | |
62 | { |
46 | { |
63 | my @async; |
47 | my @async; |
|
|
48 | my $init; |
64 | |
49 | |
65 | # this way of handling attributes simply is NOT scalable ;() |
50 | # this way of handling attributes simply is NOT scalable ;() |
66 | sub import { |
51 | sub import { |
67 | Coro->export_to_level(1, @_); |
52 | Coro->export_to_level(1, @_); |
68 | my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; |
53 | my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; |
… | |
… | |
70 | my ($package, $ref) = (shift, shift); |
55 | my ($package, $ref) = (shift, shift); |
71 | my @attrs; |
56 | my @attrs; |
72 | for (@_) { |
57 | for (@_) { |
73 | if ($_ eq "Coro") { |
58 | if ($_ eq "Coro") { |
74 | push @async, $ref; |
59 | push @async, $ref; |
|
|
60 | unless ($init++) { |
|
|
61 | eval q{ |
|
|
62 | sub INIT { |
|
|
63 | &async(pop @async) while @async; |
|
|
64 | } |
|
|
65 | }; |
|
|
66 | } |
75 | } else { |
67 | } else { |
76 | push @attrs, $_; |
68 | push @attrs, $_; |
77 | } |
69 | } |
78 | } |
70 | } |
79 | return $old ? $old->($package, $ref, @attrs) : @attrs; |
71 | return $old ? $old->($package, $ref, @attrs) : @attrs; |
80 | }; |
72 | }; |
81 | } |
73 | } |
82 | |
74 | |
83 | sub INIT { |
|
|
84 | &async(pop @async) while @async; |
|
|
85 | } |
|
|
86 | } |
75 | } |
87 | |
76 | |
88 | =item $main |
77 | =item $main |
89 | |
78 | |
90 | This coroutine represents the main program. |
79 | This coroutine represents the main program. |
… | |
… | |
119 | our $idle = new Coro sub { |
108 | our $idle = new Coro sub { |
120 | print STDERR "FATAL: deadlock detected\n"; |
109 | print STDERR "FATAL: deadlock detected\n"; |
121 | exit(51); |
110 | exit(51); |
122 | }; |
111 | }; |
123 | |
112 | |
|
|
113 | # this coroutine is necessary because a coroutine |
|
|
114 | # cannot destroy itself. |
|
|
115 | my @destroy; |
|
|
116 | my $manager = new Coro sub { |
|
|
117 | while() { |
|
|
118 | delete ((pop @destroy)->{_coro_state}) while @destroy; |
|
|
119 | &schedule; |
|
|
120 | } |
|
|
121 | }; |
|
|
122 | |
124 | # we really need priorities... |
123 | # we really need priorities... |
125 | my @ready; # the ready queue. hehe, rather broken ;) |
124 | my @ready; # the ready queue. hehe, rather broken ;) |
126 | |
125 | |
127 | # static methods. not really. |
126 | # static methods. not really. |
128 | |
127 | |
… | |
… | |
148 | |
147 | |
149 | =cut |
148 | =cut |
150 | |
149 | |
151 | sub async(&@) { |
150 | sub async(&@) { |
152 | my $pid = new Coro @_; |
151 | my $pid = new Coro @_; |
|
|
152 | $manager->ready; # this ensures that the stack is cloned from the manager |
153 | $pid->ready; |
153 | $pid->ready; |
154 | $pid; |
154 | $pid; |
155 | } |
155 | } |
156 | |
156 | |
157 | =item schedule |
157 | =item schedule |
… | |
… | |
190 | Future versions of this function will allow result arguments. |
190 | Future versions of this function will allow result arguments. |
191 | |
191 | |
192 | =cut |
192 | =cut |
193 | |
193 | |
194 | sub terminate { |
194 | sub terminate { |
195 | my $self = $current; |
195 | $current->cancel; |
196 | $self->{_results} = [@_]; |
196 | &schedule; |
197 | $current = shift @ready || $idle; |
197 | die; # NORETURN |
198 | Coro::State::transfer(delete $self->{_coro_state}, $current); |
|
|
199 | # cannot return |
|
|
200 | die; |
|
|
201 | } |
198 | } |
202 | |
199 | |
203 | =back |
200 | =back |
204 | |
201 | |
205 | # dynamic methods |
202 | # dynamic methods |
… | |
… | |
238 | |
235 | |
239 | =cut |
236 | =cut |
240 | |
237 | |
241 | sub ready { |
238 | sub ready { |
242 | push @ready, $_[0]; |
239 | push @ready, $_[0]; |
|
|
240 | } |
|
|
241 | |
|
|
242 | =item $process->cancel |
|
|
243 | |
|
|
244 | Like C<terminate>, but terminates the specified process instead. |
|
|
245 | |
|
|
246 | =cut |
|
|
247 | |
|
|
248 | sub cancel { |
|
|
249 | push @destroy, $_[0]; |
|
|
250 | $manager->ready; |
243 | } |
251 | } |
244 | |
252 | |
245 | =back |
253 | =back |
246 | |
254 | |
247 | =cut |
255 | =cut |
… | |
… | |
263 | allow per-thread schedulers, but Coro::State does not yet allow this). |
271 | allow per-thread schedulers, but Coro::State does not yet allow this). |
264 | |
272 | |
265 | =head1 SEE ALSO |
273 | =head1 SEE ALSO |
266 | |
274 | |
267 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
275 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
268 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |
276 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>, L<Coro::RWLock>, |
|
|
277 | L<Coro::Handle>, L<Coro::Socket>. |
269 | |
278 | |
270 | =head1 AUTHOR |
279 | =head1 AUTHOR |
271 | |
280 | |
272 | Marc Lehmann <pcg@goof.com> |
281 | Marc Lehmann <pcg@goof.com> |
273 | http://www.goof.com/pcg/marc/ |
282 | http://www.goof.com/pcg/marc/ |