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

Comparing Coro/Coro.pm (file contents):
Revision 1.39 by root, Tue Oct 9 00:39:08 2001 UTC vs.
Revision 1.40 by root, Sun Oct 28 17:00:05 2001 UTC

124 # by overwriting the state object with the manager we destroy it 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 125 # while still being able to schedule this coroutine (in case it has
126 # been readied multiple times. this is harmless since the manager 126 # been readied multiple times. this is harmless since the manager
127 # can be called as many times as neccessary and will always 127 # can be called as many times as neccessary and will always
128 # 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} || []};
129 (pop @destroy)->{_coro_state} = $manager->{_coro_state} while @destroy; 133 $coro->{_coro_state} = $manager->{_coro_state};
134 }
130 &schedule; 135 &schedule;
131 } 136 }
132}; 137};
133 138
134# static methods. not really. 139# static methods. not really.
176ready 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
177current "timeslice" to other coroutines of the same or higher priority. 182current "timeslice" to other coroutines of the same or higher priority.
178 183
179=cut 184=cut
180 185
181=item terminate 186=item terminate [arg...]
182 187
183Terminates the current process. 188Terminates the current process.
184 189
185Future versions of this function will allow result arguments. 190Future versions of this function will allow result arguments.
186 191
187=cut 192=cut
188 193
189sub terminate { 194sub terminate {
195 $current->{status} = [@_];
190 $current->cancel; 196 $current->cancel;
191 &schedule; 197 &schedule;
192 die; # NORETURN 198 die; # NORETURN
193} 199}
194 200
203=over 4 209=over 4
204 210
205=item new Coro \&sub [, @args...] 211=item new Coro \&sub [, @args...]
206 212
207Create 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
208automatically terminates. To start the process you must first put it into 214automatically terminates as if C<terminate> with the returned values were
209the 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.
210 217
211The 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
212in 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.
213 220
214=cut 221=cut
240 push @destroy, $_[0]; 247 push @destroy, $_[0];
241 $manager->ready; 248 $manager->ready;
242 &schedule if $current == $_[0]; 249 &schedule if $current == $_[0];
243} 250}
244 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];
267}
268
245=item $oldprio = $process->prio($newprio) 269=item $oldprio = $process->prio($newprio)
246 270
247Sets the priority of the process. Higher priority processes get run before 271Sets the priority of the process. Higher priority processes get run before
248lower priority processes. Priorities are smalled signed integer (currently 272lower priority processes. Priorities are smalled signed integer (currently
249-4 .. +3), that you can refer to using PRIO_xxx constants (use the import 273-4 .. +3), that you can refer to using PRIO_xxx constants (use the import

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines