… | |
… | |
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.45; |
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 | |
124 | # we really need priorities... |
113 | # this coroutine is necessary because a coroutine |
125 | my @ready; # the ready queue. hehe, rather broken ;) |
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 | }; |
126 | |
122 | |
127 | # static methods. not really. |
123 | # static methods. not really. |
128 | |
124 | |
129 | =head2 STATIC METHODS |
125 | =head2 STATIC METHODS |
130 | |
126 | |
… | |
… | |
148 | |
144 | |
149 | =cut |
145 | =cut |
150 | |
146 | |
151 | sub async(&@) { |
147 | sub async(&@) { |
152 | my $pid = new Coro @_; |
148 | my $pid = new Coro @_; |
|
|
149 | $manager->ready; # this ensures that the stack is cloned from the manager |
153 | $pid->ready; |
150 | $pid->ready; |
154 | $pid; |
151 | $pid; |
155 | } |
152 | } |
156 | |
153 | |
157 | =item schedule |
154 | =item schedule |
… | |
… | |
160 | into the ready queue, so calling this function usually means you will |
157 | into the ready queue, so calling this function usually means you will |
161 | never be called again. |
158 | never be called again. |
162 | |
159 | |
163 | =cut |
160 | =cut |
164 | |
161 | |
165 | my $prev; |
|
|
166 | |
|
|
167 | sub schedule { |
|
|
168 | # should be done using priorities :( |
|
|
169 | ($prev, $current) = ($current, shift @ready || $idle); |
|
|
170 | Coro::State::transfer($prev, $current); |
|
|
171 | } |
|
|
172 | |
|
|
173 | =item cede |
162 | =item cede |
174 | |
163 | |
175 | "Cede" to other processes. This function puts the current process into the |
164 | "Cede" to other processes. This function puts the current process into the |
176 | ready queue and calls C<schedule>, which has the effect of giving up the |
165 | ready queue and calls C<schedule>, which has the effect of giving up the |
177 | current "timeslice" to other coroutines of the same or higher priority. |
166 | current "timeslice" to other coroutines of the same or higher priority. |
178 | |
167 | |
179 | =cut |
168 | =cut |
180 | |
169 | |
181 | sub cede { |
170 | =item terminate |
|
|
171 | |
|
|
172 | Terminates the current process. |
|
|
173 | |
|
|
174 | Future versions of this function will allow result arguments. |
|
|
175 | |
|
|
176 | =cut |
|
|
177 | |
|
|
178 | sub terminate { |
182 | $current->ready; |
179 | $current->cancel; |
183 | &schedule; |
180 | &schedule; |
184 | } |
181 | die; # NORETURN |
185 | |
|
|
186 | =item terminate |
|
|
187 | |
|
|
188 | Terminates the current process. |
|
|
189 | |
|
|
190 | Future versions of this function will allow result arguments. |
|
|
191 | |
|
|
192 | =cut |
|
|
193 | |
|
|
194 | sub terminate { |
|
|
195 | my $self = $current; |
|
|
196 | $self->{_results} = [@_]; |
|
|
197 | $current = shift @ready || $idle; |
|
|
198 | Coro::State::transfer(delete $self->{_coro_state}, $current); |
|
|
199 | # cannot return |
|
|
200 | die; |
|
|
201 | } |
182 | } |
202 | |
183 | |
203 | =back |
184 | =back |
204 | |
185 | |
205 | # dynamic methods |
186 | # dynamic methods |
… | |
… | |
236 | |
217 | |
237 | Put the current process into the ready queue. |
218 | Put the current process into the ready queue. |
238 | |
219 | |
239 | =cut |
220 | =cut |
240 | |
221 | |
241 | sub ready { |
222 | =item $process->cancel |
|
|
223 | |
|
|
224 | Like C<terminate>, but terminates the specified process instead. |
|
|
225 | |
|
|
226 | =cut |
|
|
227 | |
|
|
228 | sub cancel { |
242 | push @ready, $_[0]; |
229 | push @destroy, $_[0]; |
|
|
230 | $manager->ready; |
243 | } |
231 | } |
244 | |
232 | |
245 | =back |
233 | =back |
246 | |
234 | |
247 | =cut |
235 | =cut |
… | |
… | |
263 | allow per-thread schedulers, but Coro::State does not yet allow this). |
251 | allow per-thread schedulers, but Coro::State does not yet allow this). |
264 | |
252 | |
265 | =head1 SEE ALSO |
253 | =head1 SEE ALSO |
266 | |
254 | |
267 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
255 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
268 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |
256 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>, L<Coro::RWLock>, |
|
|
257 | L<Coro::Handle>, L<Coro::Socket>. |
269 | |
258 | |
270 | =head1 AUTHOR |
259 | =head1 AUTHOR |
271 | |
260 | |
272 | Marc Lehmann <pcg@goof.com> |
261 | Marc Lehmann <pcg@goof.com> |
273 | http://www.goof.com/pcg/marc/ |
262 | http://www.goof.com/pcg/marc/ |