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

Comparing Coro/Coro.pm (file contents):
Revision 1.83 by root, Fri Nov 24 15:34:33 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 {
184 185
185=cut 186=cut
186 187
187sub async(&@) { 188sub async(&@) {
188 my $pid = new Coro @_; 189 my $pid = new Coro @_;
189 $manager->ready; # this ensures that the stack is cloned from the manager
190 $pid->ready; 190 $pid->ready;
191 $pid; 191 $pid
192} 192}
193 193
194=item schedule 194=item schedule
195 195
196Calls the scheduler. Please note that the current process will not be put 196Calls the scheduler. Please note that the current process will not be put
232Create 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
233automatically terminates as if C<terminate> with the returned values were 233automatically terminates as if C<terminate> with the returned values were
234called. 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
235by calling the ready method. 235by calling the ready method.
236 236
237=cut 237Calling C<exit> in a coroutine will not work correctly, so do not do that.
238 238
239=cut
240
239sub _newcoro { 241sub _new_coro {
240 terminate &{+shift}; 242 terminate &{+shift};
241} 243}
242 244
243sub new { 245sub new {
244 my $class = shift; 246 my $class = shift;
245 247
246 $class->SUPER::new (\&_newcoro, @_) 248 $class->SUPER::new (\&_new_coro, @_)
247} 249}
248 250
249=item $process->ready 251=item $success = $process->ready
250 252
251Put 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.
252 256
253=cut 257=item $is_ready = $process->is_ready
258
259Return wether the process is currently the ready queue or not,
254 260
255=item $process->cancel (arg...) 261=item $process->cancel (arg...)
256 262
257Terminates the given process and makes it return the given arguments as 263Terminates the given process and makes it return the given arguments as
258status (default: the empty list). 264status (default: the empty list).

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines