… | |
… | |
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. |
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 |
|
|
29 | + @_ + $_ + $@ + $^W + C stack), that is, a coroutine has it's own |
|
|
30 | callchain, it's own set of lexicals and it's own set of perl's most |
|
|
31 | important global variables. |
|
|
32 | |
28 | =cut |
33 | =cut |
29 | |
34 | |
30 | package Coro; |
35 | package Coro; |
31 | |
36 | |
32 | use Coro::State; |
37 | use Coro::State; |
33 | |
38 | |
34 | use base Exporter; |
39 | use base Exporter; |
35 | |
40 | |
36 | $VERSION = 0.09; |
41 | $VERSION = 0.12; |
37 | |
42 | |
38 | @EXPORT = qw(async yield schedule terminate current); |
43 | @EXPORT = qw(async cede schedule terminate current); |
39 | @EXPORT_OK = qw($current); |
44 | @EXPORT_OK = qw($current); |
40 | |
45 | |
41 | { |
46 | { |
42 | use subs 'async'; |
|
|
43 | |
|
|
44 | my @async; |
47 | my @async; |
45 | |
48 | |
46 | # this way of handling attributes simply is NOT scalable ;() |
49 | # this way of handling attributes simply is NOT scalable ;() |
47 | sub import { |
50 | sub import { |
48 | Coro->export_to_level(1, @_); |
51 | Coro->export_to_level(1, @_); |
… | |
… | |
60 | return $old ? $old->($package, $ref, @attrs) : @attrs; |
63 | return $old ? $old->($package, $ref, @attrs) : @attrs; |
61 | }; |
64 | }; |
62 | } |
65 | } |
63 | |
66 | |
64 | sub INIT { |
67 | sub INIT { |
65 | async pop @async while @async; |
68 | &async(pop @async) while @async; |
66 | } |
69 | } |
67 | } |
70 | } |
68 | |
71 | |
69 | =item $main |
72 | =item $main |
70 | |
73 | |
… | |
… | |
100 | our $idle = new Coro sub { |
103 | our $idle = new Coro sub { |
101 | print STDERR "FATAL: deadlock detected\n"; |
104 | print STDERR "FATAL: deadlock detected\n"; |
102 | exit(51); |
105 | exit(51); |
103 | }; |
106 | }; |
104 | |
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 | |
105 | # we really need priorities... |
118 | # we really need priorities... |
106 | my @ready; # the ready queue. hehe, rather broken ;) |
119 | my @ready; # the ready queue. hehe, rather broken ;) |
107 | |
120 | |
108 | # static methods. not really. |
121 | # static methods. not really. |
109 | |
122 | |
… | |
… | |
129 | |
142 | |
130 | =cut |
143 | =cut |
131 | |
144 | |
132 | sub async(&@) { |
145 | sub async(&@) { |
133 | my $pid = new Coro @_; |
146 | my $pid = new Coro @_; |
|
|
147 | $manager->ready; # this ensures that the stack is cloned from the manager |
134 | $pid->ready; |
148 | $pid->ready; |
135 | $pid; |
149 | $pid; |
136 | } |
150 | } |
137 | |
151 | |
138 | =item schedule |
152 | =item schedule |
… | |
… | |
149 | # should be done using priorities :( |
163 | # should be done using priorities :( |
150 | ($prev, $current) = ($current, shift @ready || $idle); |
164 | ($prev, $current) = ($current, shift @ready || $idle); |
151 | Coro::State::transfer($prev, $current); |
165 | Coro::State::transfer($prev, $current); |
152 | } |
166 | } |
153 | |
167 | |
154 | =item yield |
168 | =item cede |
155 | |
169 | |
156 | Yield to other processes. This function puts the current process into the |
170 | "Cede" to other processes. This function puts the current process into the |
157 | ready queue and calls C<schedule>. |
171 | ready queue and calls C<schedule>, which has the effect of giving up the |
|
|
172 | current "timeslice" to other coroutines of the same or higher priority. |
158 | |
173 | |
159 | =cut |
174 | =cut |
160 | |
175 | |
161 | sub yield { |
176 | sub cede { |
162 | $current->ready; |
177 | $current->ready; |
163 | &schedule; |
178 | &schedule; |
164 | } |
179 | } |
165 | |
180 | |
166 | =item terminate |
181 | =item terminate |
… | |
… | |
170 | Future versions of this function will allow result arguments. |
185 | Future versions of this function will allow result arguments. |
171 | |
186 | |
172 | =cut |
187 | =cut |
173 | |
188 | |
174 | sub terminate { |
189 | sub terminate { |
175 | $current->{_results} = [@_]; |
190 | push @destroy, $current; |
|
|
191 | $manager->ready; |
176 | &schedule; |
192 | &schedule; |
|
|
193 | # NORETURN |
177 | } |
194 | } |
178 | |
195 | |
179 | =back |
196 | =back |
180 | |
197 | |
181 | # dynamic methods |
198 | # dynamic methods |
… | |
… | |
234 | - if variables or arguments "disappear" (become undef) or become |
251 | - if variables or arguments "disappear" (become undef) or become |
235 | corrupted please contact the author so he cen iron out the |
252 | corrupted please contact the author so he cen iron out the |
236 | remaining bugs. |
253 | remaining bugs. |
237 | - this module is not thread-safe. You must only ever use this module from |
254 | - this module is not thread-safe. You must only ever use this module from |
238 | the same thread (this requirement might be loosened in the future to |
255 | the same thread (this requirement might be loosened in the future to |
239 | allow per-thread schedulers, but Coro::Satte does not yet allow this). |
256 | allow per-thread schedulers, but Coro::State does not yet allow this). |
240 | |
257 | |
241 | =head1 SEE ALSO |
258 | =head1 SEE ALSO |
242 | |
259 | |
243 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
260 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
244 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |
261 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>, L<Coro::RWLock>, |
|
|
262 | L<Coro::L<Coro::Handle>, L<Coro::Socket>. |
245 | |
263 | |
246 | =head1 AUTHOR |
264 | =head1 AUTHOR |
247 | |
265 | |
248 | Marc Lehmann <pcg@goof.com> |
266 | Marc Lehmann <pcg@goof.com> |
249 | http://www.goof.com/pcg/marc/ |
267 | http://www.goof.com/pcg/marc/ |