… | |
… | |
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 | |
23 | This module collection manages coroutines. Coroutines are similar to |
23 | This module collection manages coroutines. Coroutines are similar to |
24 | Threads but don't run in parallel. |
24 | Threads but don't run in parallel. |
… | |
… | |
34 | coroutines are still running OR just call exit before falling off the |
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 |
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 |
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. |
37 | to that C function instead if to the main C interpreter. |
38 | |
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 | |
39 | =cut |
49 | =cut |
40 | |
50 | |
41 | package Coro; |
51 | package Coro; |
42 | |
52 | |
43 | use Coro::State; |
53 | use Coro::State; |
44 | |
54 | |
45 | use base Exporter; |
55 | use base Exporter; |
46 | |
56 | |
47 | $VERSION = 0.10; |
57 | $VERSION = 0.10; |
48 | |
58 | |
49 | @EXPORT = qw(async yield schedule terminate current); |
59 | @EXPORT = qw(async cede schedule terminate current); |
50 | @EXPORT_OK = qw($current); |
60 | @EXPORT_OK = qw($current); |
51 | |
61 | |
52 | { |
62 | { |
53 | my @async; |
63 | my @async; |
54 | |
64 | |
… | |
… | |
158 | # should be done using priorities :( |
168 | # should be done using priorities :( |
159 | ($prev, $current) = ($current, shift @ready || $idle); |
169 | ($prev, $current) = ($current, shift @ready || $idle); |
160 | Coro::State::transfer($prev, $current); |
170 | Coro::State::transfer($prev, $current); |
161 | } |
171 | } |
162 | |
172 | |
163 | =item yield |
173 | =item cede |
164 | |
174 | |
165 | Yield to other processes. This function puts the current process into the |
175 | "Cede" to other processes. This function puts the current process into the |
166 | ready queue and calls C<schedule>. |
176 | 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. |
167 | |
178 | |
168 | =cut |
179 | =cut |
169 | |
180 | |
170 | sub yield { |
181 | sub cede { |
171 | $current->ready; |
182 | $current->ready; |
172 | &schedule; |
183 | &schedule; |
173 | } |
184 | } |
174 | |
185 | |
175 | =item terminate |
186 | =item terminate |
… | |
… | |
179 | Future versions of this function will allow result arguments. |
190 | Future versions of this function will allow result arguments. |
180 | |
191 | |
181 | =cut |
192 | =cut |
182 | |
193 | |
183 | sub terminate { |
194 | sub terminate { |
|
|
195 | my $self = $current; |
184 | $current->{_results} = [@_]; |
196 | $self->{_results} = [@_]; |
185 | delete $current->{_coro_state}; |
197 | $current = shift @ready || $idle; |
186 | &schedule; |
198 | Coro::State::transfer(delete $self->{_coro_state}, $current); |
|
|
199 | # cannot return |
|
|
200 | die; |
187 | } |
201 | } |
188 | |
202 | |
189 | =back |
203 | =back |
190 | |
204 | |
191 | # dynamic methods |
205 | # dynamic methods |