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

Comparing Coro/Coro.pm (file contents):
Revision 1.36 by root, Mon Sep 24 01:36:20 2001 UTC vs.
Revision 1.40 by root, Sun Oct 28 17:00:05 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.51;
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 delete ((pop @destroy)->{_coro_state}) while @destroy; 124 # by overwriting the state object with the manager we destroy it
125 # while still being able to schedule this coroutine (in case it has
126 # been readied multiple times. this is harmless since the manager
127 # can be called as many times as neccessary and will always
128 # remove itself from the runqueue
129 while (@destroy) {
130 my $coro = pop @destroy;
131 $coro->{status} ||= [];
132 $_->ready for @{delete $coro->{join} || []};
133 $coro->{_coro_state} = $manager->{_coro_state};
134 }
124 &schedule; 135 &schedule;
125 } 136 }
126}; 137};
127 138
128# static methods. not really. 139# static methods. not really.
170ready 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
171current "timeslice" to other coroutines of the same or higher priority. 182current "timeslice" to other coroutines of the same or higher priority.
172 183
173=cut 184=cut
174 185
175=item terminate 186=item terminate [arg...]
176 187
177Terminates the current process. 188Terminates the current process.
178 189
179Future versions of this function will allow result arguments. 190Future versions of this function will allow result arguments.
180 191
181=cut 192=cut
182 193
183sub terminate { 194sub terminate {
195 $current->{status} = [@_];
184 $current->cancel; 196 $current->cancel;
185 &schedule; 197 &schedule;
186 die; # NORETURN 198 die; # NORETURN
187} 199}
188 200
197=over 4 209=over 4
198 210
199=item new Coro \&sub [, @args...] 211=item new Coro \&sub [, @args...]
200 212
201Create 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
202automatically terminates. To start the process you must first put it into 214automatically terminates as if C<terminate> with the returned values were
203the ready queue by calling the ready method. 215called. To start the process you must first put it into the ready queue by
216calling the ready method.
204 217
205The coderef you submit MUST NOT be a closure that refers to variables 218The coderef you submit MUST NOT be a closure that refers to variables
206in an outer scope. This does NOT work. Pass arguments into it instead. 219in an outer scope. This does NOT work. Pass arguments into it instead.
207 220
208=cut 221=cut
218 }, $class; 231 }, $class;
219} 232}
220 233
221=item $process->ready 234=item $process->ready
222 235
223Put the current process into the ready queue. 236Put the given process into the ready queue.
224 237
225=cut 238=cut
226 239
227=item $process->cancel 240=item $process->cancel
228 241
232 245
233sub cancel { 246sub cancel {
234 push @destroy, $_[0]; 247 push @destroy, $_[0];
235 $manager->ready; 248 $manager->ready;
236 &schedule if $current == $_[0]; 249 &schedule if $current == $_[0];
250}
251
252=item $process->join
253
254Wait until the coroutine terminates and return any values given to the
255C<terminate> function. C<join> can be called multiple times from multiple
256processes.
257
258=cut
259
260sub join {
261 my $self = shift;
262 unless ($self->{status}) {
263 push @{$self->{join}}, $current;
264 &schedule;
265 }
266 wantarray ? @{$self->{status}} : $self->{status}[0];
237} 267}
238 268
239=item $oldprio = $process->prio($newprio) 269=item $oldprio = $process->prio($newprio)
240 270
241Sets the priority of the process. Higher priority processes get run before 271Sets the priority of the process. Higher priority processes get run before

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines