… | |
… | |
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.12; |
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 | { |
… | |
… | |
119 | our $idle = new Coro sub { |
103 | our $idle = new Coro sub { |
120 | print STDERR "FATAL: deadlock detected\n"; |
104 | print STDERR "FATAL: deadlock detected\n"; |
121 | exit(51); |
105 | exit(51); |
122 | }; |
106 | }; |
123 | |
107 | |
|
|
108 | # this coroutine is necessary because a coroutine |
|
|
109 | # cannot destroy itself. |
|
|
110 | my @destroy; |
|
|
111 | my $manager = new Coro sub { |
|
|
112 | while() { |
|
|
113 | delete ((pop @destroy)->{_coro_state}) while @destroy; |
|
|
114 | &schedule; |
|
|
115 | } |
|
|
116 | }; |
|
|
117 | |
124 | # we really need priorities... |
118 | # we really need priorities... |
125 | my @ready; # the ready queue. hehe, rather broken ;) |
119 | my @ready; # the ready queue. hehe, rather broken ;) |
126 | |
120 | |
127 | # static methods. not really. |
121 | # static methods. not really. |
128 | |
122 | |
… | |
… | |
148 | |
142 | |
149 | =cut |
143 | =cut |
150 | |
144 | |
151 | sub async(&@) { |
145 | sub async(&@) { |
152 | my $pid = new Coro @_; |
146 | my $pid = new Coro @_; |
|
|
147 | $manager->ready; # this ensures that the stack is cloned from the manager |
153 | $pid->ready; |
148 | $pid->ready; |
154 | $pid; |
149 | $pid; |
155 | } |
150 | } |
156 | |
151 | |
157 | =item schedule |
152 | =item schedule |
… | |
… | |
190 | Future versions of this function will allow result arguments. |
185 | Future versions of this function will allow result arguments. |
191 | |
186 | |
192 | =cut |
187 | =cut |
193 | |
188 | |
194 | sub terminate { |
189 | sub terminate { |
195 | my $self = $current; |
190 | push @destroy, $current; |
196 | $self->{_results} = [@_]; |
191 | $manager->ready; |
197 | $current = shift @ready || $idle; |
192 | &schedule; |
198 | Coro::State::transfer(delete $self->{_coro_state}, $current); |
193 | # NORETURN |
199 | # cannot return |
|
|
200 | die; |
|
|
201 | } |
194 | } |
202 | |
195 | |
203 | =back |
196 | =back |
204 | |
197 | |
205 | # dynamic methods |
198 | # dynamic methods |