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

Comparing Coro/Coro.pm (file contents):
Revision 1.85 by root, Sat Nov 25 00:56:35 2006 UTC vs.
Revision 1.90 by root, Thu Nov 30 18:21:14 2006 UTC

41 41
42our $idle; # idle handler 42our $idle; # idle handler
43our $main; # main coroutine 43our $main; # main coroutine
44our $current; # current coroutine 44our $current; # current coroutine
45 45
46our $VERSION = '2.5'; 46our $VERSION = '3.0';
47 47
48our @EXPORT = qw(async cede schedule terminate current); 48our @EXPORT = qw(async cede schedule terminate current);
49our %EXPORT_TAGS = ( 49our %EXPORT_TAGS = (
50 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], 50 prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)],
51); 51);
133}; 133};
134 134
135# this coroutine is necessary because a coroutine 135# this coroutine is necessary because a coroutine
136# cannot destroy itself. 136# cannot destroy itself.
137my @destroy; 137my @destroy;
138my $manager;
139$manager = new Coro sub { 138my $manager; $manager = new Coro sub {
140 while () { 139 while () {
141 # by overwriting the state object with the manager we destroy it 140 # by overwriting the state object with the manager we destroy it
142 # while still being able to schedule this coroutine (in case it has 141 # while still being able to schedule this coroutine (in case it has
143 # been readied multiple times. this is harmless since the manager 142 # been readied multiple times. this is harmless since the manager
144 # can be called as many times as neccessary and will always 143 # can be called as many times as neccessary and will always
172 171
173Create a new asynchronous process and return it's process object 172Create a new asynchronous process and return it's process object
174(usually unused). When the sub returns the new process is automatically 173(usually unused). When the sub returns the new process is automatically
175terminated. 174terminated.
176 175
176Calling C<exit> in a coroutine will not work correctly, so do not do that.
177
177When the coroutine dies, the program will exit, just as in the main 178When the coroutine dies, the program will exit, just as in the main
178program. 179program.
179 180
180 # create a new coroutine that just prints its arguments 181 # create a new coroutine that just prints its arguments
181 async { 182 async {
231Create a new process and return it. When the sub returns the process 232Create a new process and return it. When the sub returns the process
232automatically terminates as if C<terminate> with the returned values were 233automatically terminates as if C<terminate> with the returned values were
233called. To make the process run you must first put it into the ready queue 234called. To make the process run you must first put it into the ready queue
234by calling the ready method. 235by calling the ready method.
235 236
237Calling C<exit> in a coroutine will not work correctly, so do not do that.
238
236=cut 239=cut
237 240
238sub _new_coro { 241sub _new_coro {
239 $current->_clear_idle_sp; # set the idle sp on the following cede
240 _set_cede_self; # ensures that cede cede's us first
241 cede;
242 terminate &{+shift}; 242 terminate &{+shift};
243} 243}
244 244
245sub new { 245sub new {
246 my $class = shift; 246 my $class = shift;
247 247
248 $class->SUPER::new (\&_new_coro, @_) 248 $class->SUPER::new (\&_new_coro, @_)
249} 249}
250 250
251=item $process->ready 251=item $success = $process->ready
252 252
253Put the given process into the ready queue. 253Put the given process into the ready queue (according to it's priority)
254and return true. If the process is already in the ready queue, do nothing
255and return false.
254 256
255=cut 257=item $is_ready = $process->is_ready
258
259Return wether the process is currently the ready queue or not,
256 260
257=item $process->cancel (arg...) 261=item $process->cancel (arg...)
258 262
259Terminates the given process and makes it return the given arguments as 263Terminates the given process and makes it return the given arguments as
260status (default: the empty list). 264status (default: the empty list).

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines