… | |
… | |
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 | |
23 | =cut |
28 | =cut |
24 | |
29 | |
25 | package Coro; |
30 | package Coro; |
26 | |
31 | |
27 | use Coro::State; |
32 | use Coro::State; |
28 | |
33 | |
29 | use base Exporter; |
34 | use base Exporter; |
30 | |
35 | |
31 | $VERSION = 0.04; |
36 | $VERSION = 0.07; |
32 | |
37 | |
33 | @EXPORT = qw(async yield schedule); |
38 | @EXPORT = qw(async yield schedule terminate); |
34 | @EXPORT_OK = qw($current); |
39 | @EXPORT_OK = qw($current); |
35 | |
40 | |
36 | { |
41 | { |
37 | use subs 'async'; |
42 | use subs 'async'; |
38 | |
43 | |
… | |
… | |
94 | print STDERR "FATAL: deadlock detected\n"; |
99 | print STDERR "FATAL: deadlock detected\n"; |
95 | exit(51); |
100 | exit(51); |
96 | }; |
101 | }; |
97 | |
102 | |
98 | # we really need priorities... |
103 | # we really need priorities... |
99 | my @ready = (); # the ready queue. hehe, rather broken ;) |
104 | my @ready; # the ready queue. hehe, rather broken ;) |
100 | |
105 | |
101 | # static methods. not really. |
106 | # static methods. not really. |
102 | |
107 | |
103 | =head2 STATIC METHODS |
108 | =head2 STATIC METHODS |
104 | |
109 | |
105 | Static methods are actually functions that operate on the current process only. |
110 | Static methods are actually functions that operate on the current process only. |
106 | |
111 | |
107 | =over 4 |
112 | =over 4 |
108 | |
113 | |
109 | =item async { ... }; |
114 | =item async { ... } [@args...] |
110 | |
115 | |
111 | Create a new asynchronous process and return it's process object |
116 | Create a new asynchronous process and return it's process object |
112 | (usually unused). When the sub returns the new process is automatically |
117 | (usually unused). When the sub returns the new process is automatically |
113 | terminated. |
118 | terminated. |
114 | |
119 | |
115 | =cut |
120 | # create a new coroutine that just prints its arguments |
|
|
121 | async { |
|
|
122 | print "@_\n"; |
|
|
123 | } 1,2,3,4; |
116 | |
124 | |
|
|
125 | The coderef you submit MUST NOT be a closure that refers to variables |
|
|
126 | in an outer scope. This does NOT work. Pass arguments into it instead. |
|
|
127 | |
|
|
128 | =cut |
|
|
129 | |
117 | sub async(&) { |
130 | sub async(&@) { |
118 | (new Coro $_[0])->ready; |
131 | my $pid = new Coro @_; |
|
|
132 | $pid->ready; |
|
|
133 | $pid; |
119 | } |
134 | } |
120 | |
135 | |
121 | =item schedule |
136 | =item schedule |
122 | |
137 | |
123 | Calls the scheduler. Please note that the current process will not be put |
138 | Calls the scheduler. Please note that the current process will not be put |
… | |
… | |
148 | |
163 | |
149 | =item terminate |
164 | =item terminate |
150 | |
165 | |
151 | Terminates the current process. |
166 | Terminates the current process. |
152 | |
167 | |
|
|
168 | Future versions of this function will allow result arguments. |
|
|
169 | |
153 | =cut |
170 | =cut |
154 | |
171 | |
155 | sub terminate { |
172 | sub terminate { |
|
|
173 | $current->{_results} = [@_]; |
156 | &schedule; |
174 | &schedule; |
157 | } |
175 | } |
158 | |
176 | |
159 | =back |
177 | =back |
160 | |
178 | |
… | |
… | |
164 | |
182 | |
165 | These are the methods you can call on process objects. |
183 | These are the methods you can call on process objects. |
166 | |
184 | |
167 | =over 4 |
185 | =over 4 |
168 | |
186 | |
169 | =item new Coro \⊂ |
187 | =item new Coro \&sub [, @args...] |
170 | |
188 | |
171 | Create a new process and return it. When the sub returns the process |
189 | Create a new process and return it. When the sub returns the process |
172 | automatically terminates. To start the process you must first put it into |
190 | automatically terminates. To start the process you must first put it into |
173 | the ready queue by calling the ready method. |
191 | the ready queue by calling the ready method. |
174 | |
192 | |
|
|
193 | The coderef you submit MUST NOT be a closure that refers to variables |
|
|
194 | in an outer scope. This does NOT work. Pass arguments into it instead. |
|
|
195 | |
175 | =cut |
196 | =cut |
|
|
197 | |
|
|
198 | sub _newcoro { |
|
|
199 | terminate &{+shift}; |
|
|
200 | } |
176 | |
201 | |
177 | sub new { |
202 | sub new { |
178 | my $class = shift; |
203 | my $class = shift; |
179 | my $proc = $_[0]; |
|
|
180 | bless { |
204 | bless { |
181 | _coro_state => new Coro::State ($proc ? sub { &$proc; &terminate } : $proc), |
205 | _coro_state => (new Coro::State $_[0] && \&_newcoro, @_), |
182 | }, $class; |
206 | }, $class; |
183 | } |
207 | } |
184 | |
208 | |
185 | =item $process->ready |
209 | =item $process->ready |
186 | |
210 | |
… | |
… | |
196 | |
220 | |
197 | =cut |
221 | =cut |
198 | |
222 | |
199 | 1; |
223 | 1; |
200 | |
224 | |
|
|
225 | =head1 BUGS |
|
|
226 | |
|
|
227 | - could be faster, especially when the core would introduce special |
|
|
228 | support for coroutines (like it does for threads). |
|
|
229 | - there is still a memleak on coroutine termination that I could not |
|
|
230 | identify. Could be as small as a single SV. |
|
|
231 | - this module is not well-tested. |
|
|
232 | |
201 | =head1 SEE ALSO |
233 | =head1 SEE ALSO |
202 | |
234 | |
203 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
235 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
204 | L<Coro::Signal>, L<Coro::State>. |
236 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |
205 | |
237 | |
206 | =head1 AUTHOR |
238 | =head1 AUTHOR |
207 | |
239 | |
208 | Marc Lehmann <pcg@goof.com> |
240 | Marc Lehmann <pcg@goof.com> |
209 | http://www.goof.com/pcg/marc/ |
241 | http://www.goof.com/pcg/marc/ |