… | |
… | |
18 | |
18 | |
19 | yield; |
19 | yield; |
20 | |
20 | |
21 | =head1 DESCRIPTION |
21 | =head1 DESCRIPTION |
22 | |
22 | |
|
|
23 | This module collection manages coroutines. Coroutines are similar to |
|
|
24 | Threads but don't run in parallel. |
|
|
25 | |
|
|
26 | This module is still experimental, see the BUGS section below. |
|
|
27 | |
|
|
28 | In this module, coroutines are defined as "callchain + lexical variables |
|
|
29 | + @_ + $_ + $@ + $^W), that is, a coroutine has it's own callchain, it's |
|
|
30 | own set of lexicals and it's own set of perl's most important global |
|
|
31 | 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 | |
23 | =cut |
39 | =cut |
24 | |
40 | |
25 | package Coro; |
41 | package Coro; |
26 | |
42 | |
27 | use Coro::State; |
43 | use Coro::State; |
28 | |
44 | |
29 | use base Exporter; |
45 | use base Exporter; |
30 | |
46 | |
31 | $VERSION = 0.03; |
47 | $VERSION = 0.10; |
32 | |
48 | |
33 | @EXPORT = qw(async yield schedule); |
49 | @EXPORT = qw(async yield schedule terminate current); |
34 | @EXPORT_OK = qw($current); |
50 | @EXPORT_OK = qw($current); |
35 | |
51 | |
36 | { |
52 | { |
37 | use subs 'async'; |
|
|
38 | |
|
|
39 | my @async; |
53 | my @async; |
40 | |
54 | |
41 | # this way of handling attributes simply is NOT scalable ;() |
55 | # this way of handling attributes simply is NOT scalable ;() |
42 | sub import { |
56 | sub import { |
43 | Coro->export_to_level(1, @_); |
57 | Coro->export_to_level(1, @_); |
… | |
… | |
47 | my @attrs; |
61 | my @attrs; |
48 | for (@_) { |
62 | for (@_) { |
49 | if ($_ eq "Coro") { |
63 | if ($_ eq "Coro") { |
50 | push @async, $ref; |
64 | push @async, $ref; |
51 | } else { |
65 | } else { |
52 | push @attrs, @_; |
66 | push @attrs, $_; |
53 | } |
67 | } |
54 | } |
68 | } |
55 | return $old ? $old->($package, $name, @attrs) : @attrs; |
69 | return $old ? $old->($package, $ref, @attrs) : @attrs; |
56 | }; |
70 | }; |
57 | } |
71 | } |
58 | |
72 | |
59 | sub INIT { |
73 | sub INIT { |
60 | async pop @async while @async; |
74 | &async(pop @async) while @async; |
61 | } |
75 | } |
62 | } |
76 | } |
63 | |
77 | |
64 | my $idle = new Coro sub { |
|
|
65 | &yield while 1; |
|
|
66 | }; |
|
|
67 | |
|
|
68 | =item $main |
78 | =item $main |
69 | |
79 | |
70 | This coroutine represents the main program. |
80 | This coroutine represents the main program. |
71 | |
81 | |
72 | =cut |
82 | =cut |
73 | |
83 | |
74 | $main = new Coro; |
84 | our $main = new Coro; |
75 | |
85 | |
76 | =item $current |
86 | =item $current (or as function: current) |
77 | |
87 | |
78 | The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course). |
88 | The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course). |
79 | |
89 | |
80 | =cut |
90 | =cut |
81 | |
91 | |
82 | # maybe some other module used Coro::Specific before... |
92 | # maybe some other module used Coro::Specific before... |
83 | if ($current) { |
93 | if ($current) { |
84 | $main->{specific} = $current->{specific}; |
94 | $main->{specific} = $current->{specific}; |
85 | } |
95 | } |
86 | |
96 | |
87 | $current = $main; |
97 | our $current = $main; |
|
|
98 | |
|
|
99 | sub current() { $current } |
|
|
100 | |
|
|
101 | =item $idle |
|
|
102 | |
|
|
103 | The coroutine to switch to when no other coroutine is running. The default |
|
|
104 | implementation prints "FATAL: deadlock detected" and exits. |
|
|
105 | |
|
|
106 | =cut |
|
|
107 | |
|
|
108 | # should be done using priorities :( |
|
|
109 | our $idle = new Coro sub { |
|
|
110 | print STDERR "FATAL: deadlock detected\n"; |
|
|
111 | exit(51); |
|
|
112 | }; |
88 | |
113 | |
89 | # we really need priorities... |
114 | # we really need priorities... |
90 | my @ready = (); # the ready queue. hehe, rather broken ;) |
115 | my @ready; # the ready queue. hehe, rather broken ;) |
91 | |
116 | |
92 | # static methods. not really. |
117 | # static methods. not really. |
93 | |
118 | |
94 | =head2 STATIC METHODS |
119 | =head2 STATIC METHODS |
95 | |
120 | |
96 | Static methods are actually functions that operate on the current process only. |
121 | Static methods are actually functions that operate on the current process only. |
97 | |
122 | |
98 | =over 4 |
123 | =over 4 |
99 | |
124 | |
100 | =item async { ... }; |
125 | =item async { ... } [@args...] |
101 | |
126 | |
102 | Create a new asynchronous process and return it's process object |
127 | Create a new asynchronous process and return it's process object |
103 | (usually unused). When the sub returns the new process is automatically |
128 | (usually unused). When the sub returns the new process is automatically |
104 | terminated. |
129 | terminated. |
105 | |
130 | |
106 | =cut |
131 | # create a new coroutine that just prints its arguments |
|
|
132 | async { |
|
|
133 | print "@_\n"; |
|
|
134 | } 1,2,3,4; |
107 | |
135 | |
|
|
136 | The coderef you submit MUST NOT be a closure that refers to variables |
|
|
137 | in an outer scope. This does NOT work. Pass arguments into it instead. |
|
|
138 | |
|
|
139 | =cut |
|
|
140 | |
108 | sub async(&) { |
141 | sub async(&@) { |
109 | (new Coro $_[0])->ready; |
142 | my $pid = new Coro @_; |
|
|
143 | $pid->ready; |
|
|
144 | $pid; |
110 | } |
145 | } |
111 | |
146 | |
112 | =item schedule |
147 | =item schedule |
113 | |
148 | |
114 | Calls the scheduler. Please note that the current process will not be put |
149 | Calls the scheduler. Please note that the current process will not be put |
… | |
… | |
118 | =cut |
153 | =cut |
119 | |
154 | |
120 | my $prev; |
155 | my $prev; |
121 | |
156 | |
122 | sub schedule { |
157 | sub schedule { |
|
|
158 | # should be done using priorities :( |
123 | ($prev, $current) = ($current, shift @ready); |
159 | ($prev, $current) = ($current, shift @ready || $idle); |
124 | Coro::State::transfer($prev, $current); |
160 | Coro::State::transfer($prev, $current); |
125 | } |
161 | } |
126 | |
162 | |
127 | =item yield |
163 | =item yield |
128 | |
164 | |
… | |
… | |
138 | |
174 | |
139 | =item terminate |
175 | =item terminate |
140 | |
176 | |
141 | Terminates the current process. |
177 | Terminates the current process. |
142 | |
178 | |
|
|
179 | Future versions of this function will allow result arguments. |
|
|
180 | |
143 | =cut |
181 | =cut |
144 | |
182 | |
145 | sub terminate { |
183 | sub terminate { |
|
|
184 | $current->{_results} = [@_]; |
|
|
185 | delete $current->{_coro_state}; |
146 | &schedule; |
186 | &schedule; |
147 | } |
187 | } |
148 | |
188 | |
149 | =back |
189 | =back |
150 | |
190 | |
… | |
… | |
154 | |
194 | |
155 | These are the methods you can call on process objects. |
195 | These are the methods you can call on process objects. |
156 | |
196 | |
157 | =over 4 |
197 | =over 4 |
158 | |
198 | |
159 | =item new Coro \⊂ |
199 | =item new Coro \&sub [, @args...] |
160 | |
200 | |
161 | Create a new process and return it. When the sub returns the process |
201 | Create a new process and return it. When the sub returns the process |
162 | automatically terminates. To start the process you must first put it into |
202 | automatically terminates. To start the process you must first put it into |
163 | the ready queue by calling the ready method. |
203 | the ready queue by calling the ready method. |
164 | |
204 | |
|
|
205 | The coderef you submit MUST NOT be a closure that refers to variables |
|
|
206 | in an outer scope. This does NOT work. Pass arguments into it instead. |
|
|
207 | |
165 | =cut |
208 | =cut |
|
|
209 | |
|
|
210 | sub _newcoro { |
|
|
211 | terminate &{+shift}; |
|
|
212 | } |
166 | |
213 | |
167 | sub new { |
214 | sub new { |
168 | my $class = shift; |
215 | my $class = shift; |
169 | my $proc = $_[0]; |
|
|
170 | bless { |
216 | bless { |
171 | _coro_state => new Coro::State ($proc ? sub { &$proc; &terminate } : $proc), |
217 | _coro_state => (new Coro::State $_[0] && \&_newcoro, @_), |
172 | }, $class; |
218 | }, $class; |
173 | } |
219 | } |
174 | |
220 | |
175 | =item $process->ready |
221 | =item $process->ready |
176 | |
222 | |
… | |
… | |
186 | |
232 | |
187 | =cut |
233 | =cut |
188 | |
234 | |
189 | 1; |
235 | 1; |
190 | |
236 | |
|
|
237 | =head1 BUGS/LIMITATIONS |
|
|
238 | |
|
|
239 | - could be faster, especially when the core would introduce special |
|
|
240 | support for coroutines (like it does for threads). |
|
|
241 | - there is still a memleak on coroutine termination that I could not |
|
|
242 | identify. Could be as small as a single SV. |
|
|
243 | - this module is not well-tested. |
|
|
244 | - if variables or arguments "disappear" (become undef) or become |
|
|
245 | corrupted please contact the author so he cen iron out the |
|
|
246 | remaining bugs. |
|
|
247 | - this module is not thread-safe. You must only ever use this module from |
|
|
248 | the same thread (this requirement might be loosened in the future to |
|
|
249 | allow per-thread schedulers, but Coro::State does not yet allow this). |
|
|
250 | |
|
|
251 | =head1 SEE ALSO |
|
|
252 | |
|
|
253 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
|
|
254 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |
|
|
255 | |
191 | =head1 AUTHOR |
256 | =head1 AUTHOR |
192 | |
257 | |
193 | Marc Lehmann <pcg@goof.com> |
258 | Marc Lehmann <pcg@goof.com> |
194 | http://www.goof.com/pcg/marc/ |
259 | http://www.goof.com/pcg/marc/ |
195 | |
260 | |