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