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

Comparing Coro/Coro.pm (file contents):
Revision 1.88 by root, Sun Nov 26 02:54:55 2006 UTC vs.
Revision 1.91 by root, Fri Dec 1 02:17:37 2006 UTC

117 117
118=item $idle 118=item $idle
119 119
120A callback that is called whenever the scheduler finds no ready coroutines 120A callback that is called whenever the scheduler finds no ready coroutines
121to run. The default implementation prints "FATAL: deadlock detected" and 121to run. The default implementation prints "FATAL: deadlock detected" and
122exits. 122exits, because the program has no other way to continue.
123 123
124This hook is overwritten by modules such as C<Coro::Timer> and 124This hook is overwritten by modules such as C<Coro::Timer> and
125C<Coro::Event> to wait on an external event that hopefully wakes up some 125C<Coro::Event> to wait on an external event that hopefully wake up a
126coroutine. 126coroutine so the scheduler can run it.
127
128Please note that if your callback recursively invokes perl (e.g. for event
129handlers), then it must be prepared to be called recursively.
127 130
128=cut 131=cut
129 132
130$idle = sub { 133$idle = sub {
131 print STDERR "FATAL: deadlock detected\n"; 134 print STDERR "FATAL: deadlock detected\n";
171 174
172Create a new asynchronous process and return it's process object 175Create a new asynchronous process and return it's process object
173(usually unused). When the sub returns the new process is automatically 176(usually unused). When the sub returns the new process is automatically
174terminated. 177terminated.
175 178
179Calling C<exit> in a coroutine will not work correctly, so do not do that.
180
176When the coroutine dies, the program will exit, just as in the main 181When the coroutine dies, the program will exit, just as in the main
177program. 182program.
178 183
179 # create a new coroutine that just prints its arguments 184 # create a new coroutine that just prints its arguments
180 async { 185 async {
191 196
192=item schedule 197=item schedule
193 198
194Calls the scheduler. Please note that the current process will not be put 199Calls the scheduler. Please note that the current process will not be put
195into the ready queue, so calling this function usually means you will 200into the ready queue, so calling this function usually means you will
196never be called again. 201never be called again unless something else (e.g. an event handler) calls
202ready.
203
204The canonical way to wait on external events is this:
205
206 {
207 # remember current process
208 my $current = $Coro::current;
209
210 # register a hypothetical event handler
211 on_event_invoke sub {
212 # wake up sleeping coroutine
213 $current->ready;
214 undef $current;
215 };
216
217 # call schedule until event occured.
218 # in case we are woken up for other reasons
219 # (current still defined), loop.
220 Coro::schedule while $current;
221 }
197 222
198=cut 223=cut
199 224
200=item cede 225=item cede
201 226
230Create a new process and return it. When the sub returns the process 255Create a new process and return it. When the sub returns the process
231automatically terminates as if C<terminate> with the returned values were 256automatically terminates as if C<terminate> with the returned values were
232called. To make the process run you must first put it into the ready queue 257called. To make the process run you must first put it into the ready queue
233by calling the ready method. 258by calling the ready method.
234 259
260Calling C<exit> in a coroutine will not work correctly, so do not do that.
261
235=cut 262=cut
236 263
237sub _new_coro { 264sub _new_coro {
238 terminate &{+shift}; 265 terminate &{+shift};
239} 266}
242 my $class = shift; 269 my $class = shift;
243 270
244 $class->SUPER::new (\&_new_coro, @_) 271 $class->SUPER::new (\&_new_coro, @_)
245} 272}
246 273
247=item $process->ready 274=item $success = $process->ready
248 275
249Put the given process into the ready queue. 276Put the given process into the ready queue (according to it's priority)
277and return true. If the process is already in the ready queue, do nothing
278and return false.
250 279
251=cut 280=item $is_ready = $process->is_ready
281
282Return wether the process is currently the ready queue or not,
252 283
253=item $process->cancel (arg...) 284=item $process->cancel (arg...)
254 285
255Terminates the given process and makes it return the given arguments as 286Terminates the given process and makes it return the given arguments as
256status (default: the empty list). 287status (default: the empty list).

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines