… | |
… | |
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 |
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 | |
32 | |
33 | =cut |
33 | =cut |
34 | |
34 | |
35 | package Coro; |
35 | package Coro; |
36 | |
36 | |
37 | use Coro::State; |
37 | use Coro::State; |
38 | |
38 | |
39 | use base Exporter; |
39 | use base Exporter; |
40 | |
40 | |
41 | $VERSION = 0.10; |
41 | $VERSION = 0.45; |
42 | |
42 | |
43 | @EXPORT = qw(async yield schedule terminate current); |
43 | @EXPORT = qw(async cede schedule terminate current); |
44 | @EXPORT_OK = qw($current); |
44 | @EXPORT_OK = qw($current); |
45 | |
45 | |
46 | { |
46 | { |
47 | my @async; |
47 | my @async; |
|
|
48 | my $init; |
48 | |
49 | |
49 | # this way of handling attributes simply is NOT scalable ;() |
50 | # this way of handling attributes simply is NOT scalable ;() |
50 | sub import { |
51 | sub import { |
51 | Coro->export_to_level(1, @_); |
52 | Coro->export_to_level(1, @_); |
52 | my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; |
53 | my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; |
… | |
… | |
54 | my ($package, $ref) = (shift, shift); |
55 | my ($package, $ref) = (shift, shift); |
55 | my @attrs; |
56 | my @attrs; |
56 | for (@_) { |
57 | for (@_) { |
57 | if ($_ eq "Coro") { |
58 | if ($_ eq "Coro") { |
58 | 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 | } |
59 | } else { |
67 | } else { |
60 | push @attrs, $_; |
68 | push @attrs, $_; |
61 | } |
69 | } |
62 | } |
70 | } |
63 | return $old ? $old->($package, $ref, @attrs) : @attrs; |
71 | return $old ? $old->($package, $ref, @attrs) : @attrs; |
64 | }; |
72 | }; |
65 | } |
73 | } |
66 | |
74 | |
67 | sub INIT { |
|
|
68 | &async(pop @async) while @async; |
|
|
69 | } |
|
|
70 | } |
75 | } |
71 | |
76 | |
72 | =item $main |
77 | =item $main |
73 | |
78 | |
74 | This coroutine represents the main program. |
79 | This coroutine represents the main program. |
… | |
… | |
103 | our $idle = new Coro sub { |
108 | our $idle = new Coro sub { |
104 | print STDERR "FATAL: deadlock detected\n"; |
109 | print STDERR "FATAL: deadlock detected\n"; |
105 | exit(51); |
110 | exit(51); |
106 | }; |
111 | }; |
107 | |
112 | |
108 | # we really need priorities... |
113 | # this coroutine is necessary because a coroutine |
109 | 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 | }; |
110 | |
122 | |
111 | # static methods. not really. |
123 | # static methods. not really. |
112 | |
124 | |
113 | =head2 STATIC METHODS |
125 | =head2 STATIC METHODS |
114 | |
126 | |
… | |
… | |
132 | |
144 | |
133 | =cut |
145 | =cut |
134 | |
146 | |
135 | sub async(&@) { |
147 | sub async(&@) { |
136 | my $pid = new Coro @_; |
148 | my $pid = new Coro @_; |
|
|
149 | $manager->ready; # this ensures that the stack is cloned from the manager |
137 | $pid->ready; |
150 | $pid->ready; |
138 | $pid; |
151 | $pid; |
139 | } |
152 | } |
140 | |
153 | |
141 | =item schedule |
154 | =item schedule |
… | |
… | |
144 | 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 |
145 | never be called again. |
158 | never be called again. |
146 | |
159 | |
147 | =cut |
160 | =cut |
148 | |
161 | |
149 | my $prev; |
|
|
150 | |
|
|
151 | sub schedule { |
|
|
152 | # should be done using priorities :( |
|
|
153 | ($prev, $current) = ($current, shift @ready || $idle); |
|
|
154 | Coro::State::transfer($prev, $current); |
|
|
155 | } |
|
|
156 | |
|
|
157 | =item yield |
162 | =item cede |
158 | |
163 | |
159 | Yield to other processes. This function puts the current process into the |
164 | "Cede" to other processes. This function puts the current process into the |
160 | ready queue and calls C<schedule>. |
165 | ready queue and calls C<schedule>, which has the effect of giving up the |
|
|
166 | current "timeslice" to other coroutines of the same or higher priority. |
161 | |
167 | |
162 | =cut |
168 | =cut |
163 | |
169 | |
164 | sub yield { |
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 { |
165 | $current->ready; |
179 | $current->cancel; |
166 | &schedule; |
180 | &schedule; |
167 | } |
181 | die; # NORETURN |
168 | |
|
|
169 | =item terminate |
|
|
170 | |
|
|
171 | Terminates the current process. |
|
|
172 | |
|
|
173 | Future versions of this function will allow result arguments. |
|
|
174 | |
|
|
175 | =cut |
|
|
176 | |
|
|
177 | sub terminate { |
|
|
178 | $current->{_results} = [@_]; |
|
|
179 | &schedule; |
|
|
180 | } |
182 | } |
181 | |
183 | |
182 | =back |
184 | =back |
183 | |
185 | |
184 | # dynamic methods |
186 | # dynamic methods |
… | |
… | |
215 | |
217 | |
216 | Put the current process into the ready queue. |
218 | Put the current process into the ready queue. |
217 | |
219 | |
218 | =cut |
220 | =cut |
219 | |
221 | |
220 | sub ready { |
222 | =item $process->cancel |
|
|
223 | |
|
|
224 | Like C<terminate>, but terminates the specified process instead. |
|
|
225 | |
|
|
226 | =cut |
|
|
227 | |
|
|
228 | sub cancel { |
221 | push @ready, $_[0]; |
229 | push @destroy, $_[0]; |
|
|
230 | $manager->ready; |
222 | } |
231 | } |
223 | |
232 | |
224 | =back |
233 | =back |
225 | |
234 | |
226 | =cut |
235 | =cut |
… | |
… | |
242 | 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). |
243 | |
252 | |
244 | =head1 SEE ALSO |
253 | =head1 SEE ALSO |
245 | |
254 | |
246 | 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>, |
247 | 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>. |
248 | |
258 | |
249 | =head1 AUTHOR |
259 | =head1 AUTHOR |
250 | |
260 | |
251 | Marc Lehmann <pcg@goof.com> |
261 | Marc Lehmann <pcg@goof.com> |
252 | http://www.goof.com/pcg/marc/ |
262 | http://www.goof.com/pcg/marc/ |