ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro.pm
(Generate patch)

Comparing Coro/Coro.pm (file contents):
Revision 1.37 by root, Mon Sep 24 02:25:44 2001 UTC vs.
Revision 1.41 by root, Tue Nov 6 20:34:09 2001 UTC

38 38
39use Coro::State; 39use Coro::State;
40 40
41use base Exporter; 41use base Exporter;
42 42
43$VERSION = 0.5; 43$VERSION = 0.52;
44 44
45@EXPORT = qw(async cede schedule terminate current); 45@EXPORT = qw(async cede schedule terminate current);
46%EXPORT_TAGS = ( 46%EXPORT_TAGS = (
47 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], 47 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)],
48); 48);
116}; 116};
117 117
118# this coroutine is necessary because a coroutine 118# this coroutine is necessary because a coroutine
119# cannot destroy itself. 119# cannot destroy itself.
120my @destroy; 120my @destroy;
121my $manager;
121my $manager = new Coro sub { 122$manager = new Coro sub {
122 while() { 123 while() {
123 # by overwriting the state object with the manager we destroy it 124 # by overwriting the state object with the manager we destroy it
124 # while still being able to schedule this coroutine (in case it has 125 # while still being able to schedule this coroutine (in case it has
125 # been readied multiple times. this is harmless since the manager 126 # been readied multiple times. this is harmless since the manager
126 # can be called as many times as neccessary and will always 127 # can be called as many times as neccessary and will always
127 # remove itself from the runqueue 128 # remove itself from the runqueue
129 while (@destroy) {
130 my $coro = pop @destroy;
131 $coro->{status} ||= [];
132 $_->ready for @{delete $coro->{join} || []};
128 (pop @destroy)->{_coro_state} = $manager->{_coro_state} while @destroy; 133 $coro->{_coro_state} = $manager->{_coro_state};
134 }
129 &schedule; 135 &schedule;
130 } 136 }
131}; 137};
132 138
133# static methods. not really. 139# static methods. not really.
175ready queue and calls C<schedule>, which has the effect of giving up the 181ready queue and calls C<schedule>, which has the effect of giving up the
176current "timeslice" to other coroutines of the same or higher priority. 182current "timeslice" to other coroutines of the same or higher priority.
177 183
178=cut 184=cut
179 185
180=item terminate 186=item terminate [arg...]
181 187
182Terminates the current process. 188Terminates the current process.
183 189
184Future versions of this function will allow result arguments. 190Future versions of this function will allow result arguments.
185 191
186=cut 192=cut
187 193
188sub terminate { 194sub terminate {
195 $current->{status} = [@_];
189 $current->cancel; 196 $current->cancel;
190 &schedule; 197 &schedule;
191 die; # NORETURN 198 die; # NORETURN
192} 199}
193 200
202=over 4 209=over 4
203 210
204=item new Coro \&sub [, @args...] 211=item new Coro \&sub [, @args...]
205 212
206Create a new process and return it. When the sub returns the process 213Create a new process and return it. When the sub returns the process
207automatically terminates. To start the process you must first put it into 214automatically terminates as if C<terminate> with the returned values were
215called. To make the process run you must first put it into the ready queue
208the ready queue by calling the ready method. 216by calling the ready method.
209
210The coderef you submit MUST NOT be a closure that refers to variables
211in an outer scope. This does NOT work. Pass arguments into it instead.
212 217
213=cut 218=cut
214 219
215sub _newcoro { 220sub _newcoro {
216 terminate &{+shift}; 221 terminate &{+shift};
223 }, $class; 228 }, $class;
224} 229}
225 230
226=item $process->ready 231=item $process->ready
227 232
228Put the current process into the ready queue. 233Put the given process into the ready queue.
229 234
230=cut 235=cut
231 236
232=item $process->cancel 237=item $process->cancel
233 238
239 push @destroy, $_[0]; 244 push @destroy, $_[0];
240 $manager->ready; 245 $manager->ready;
241 &schedule if $current == $_[0]; 246 &schedule if $current == $_[0];
242} 247}
243 248
249=item $process->join
250
251Wait until the coroutine terminates and return any values given to the
252C<terminate> function. C<join> can be called multiple times from multiple
253processes.
254
255=cut
256
257sub join {
258 my $self = shift;
259 unless ($self->{status}) {
260 push @{$self->{join}}, $current;
261 &schedule;
262 }
263 wantarray ? @{$self->{status}} : $self->{status}[0];
264}
265
244=item $oldprio = $process->prio($newprio) 266=item $oldprio = $process->prio($newprio)
245 267
246Sets the priority of the process. Higher priority processes get run before 268Sets (or gets, if the argument is missing) the priority of the
269process. Higher priority processes get run before lower priority
247lower priority processes. Priorities are smalled signed integer (currently 270processes. Priorities are smalled signed integer (currently -4 .. +3),
248-4 .. +3), that you can refer to using PRIO_xxx constants (use the import 271that you can refer to using PRIO_xxx constants (use the import tag :prio
249tag :prio to get then): 272to get then):
250 273
251 PRIO_MAX > PRIO_HIGH > PRIO_NORMAL > PRIO_LOW > PRIO_IDLE > PRIO_MIN 274 PRIO_MAX > PRIO_HIGH > PRIO_NORMAL > PRIO_LOW > PRIO_IDLE > PRIO_MIN
252 3 > 1 > 0 > -1 > -3 > -4 275 3 > 1 > 0 > -1 > -3 > -4
253 276
254 # set priority to HIGH 277 # set priority to HIGH
277 300
278=cut 301=cut
279 302
280sub nice { 303sub nice {
281 $_[0]{prio} -= $_[1]; 304 $_[0]{prio} -= $_[1];
305}
306
307=item $olddesc = $process->desc($newdesc)
308
309Sets (or gets in case the argument is missing) the description for this
310process. This is just a free-form string you can associate with a process.
311
312=cut
313
314sub desc {
315 my $old = $_[0]{desc};
316 $_[0]{desc} = $_[1] if @_ > 1;
317 $old;
282} 318}
283 319
284=back 320=back
285 321
286=cut 322=cut

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines