… | |
… | |
14 | cede; # yield to coroutine |
14 | cede; # yield to coroutine |
15 | print "3\n"; |
15 | print "3\n"; |
16 | cede; # and again |
16 | cede; # and again |
17 | |
17 | |
18 | # use locking |
18 | # use locking |
|
|
19 | use Coro::Semaphore; |
19 | my $lock = new Coro::Semaphore; |
20 | my $lock = new Coro::Semaphore; |
20 | my $locked; |
21 | my $locked; |
21 | |
22 | |
22 | $lock->down; |
23 | $lock->down; |
23 | $locked = 1; |
24 | $locked = 1; |
… | |
… | |
53 | |
54 | |
54 | $Coro::main |
55 | $Coro::main |
55 | This variable stores the coroutine object that represents the main |
56 | This variable stores the coroutine object that represents the main |
56 | program. While you cna "ready" it and do most other things you can |
57 | program. While you cna "ready" it and do most other things you can |
57 | do to coroutines, it is mainly useful to compare again |
58 | do to coroutines, it is mainly useful to compare again |
58 | $Coro::current, to see wether you are running in the main program or |
59 | $Coro::current, to see whether you are running in the main program |
59 | not. |
60 | or not. |
60 | |
61 | |
61 | $Coro::current |
62 | $Coro::current |
62 | The coroutine object representing the current coroutine (the last |
63 | The coroutine object representing the current coroutine (the last |
63 | coroutine that the Coro scheduler switched to). The initial value is |
64 | coroutine that the Coro scheduler switched to). The initial value is |
64 | $main (of course). |
65 | $main (of course). |
… | |
… | |
177 | This makes "schedule" *the* generic method to use to block the |
178 | This makes "schedule" *the* generic method to use to block the |
178 | current coroutine and wait for events: first you remember the |
179 | current coroutine and wait for events: first you remember the |
179 | current coroutine in a variable, then arrange for some callback of |
180 | current coroutine in a variable, then arrange for some callback of |
180 | yours to call "->ready" on that once some event happens, and last |
181 | yours to call "->ready" on that once some event happens, and last |
181 | you call "schedule" to put yourself to sleep. Note that a lot of |
182 | you call "schedule" to put yourself to sleep. Note that a lot of |
182 | things can wake your coroutine up, so you need to check wether the |
183 | things can wake your coroutine up, so you need to check whether the |
183 | event indeed happened, e.g. by storing the status in a variable. |
184 | event indeed happened, e.g. by storing the status in a variable. |
184 | |
185 | |
185 | The canonical way to wait on external events is this: |
186 | The canonical way to wait on external events is this: |
186 | |
187 | |
187 | { |
188 | { |
… | |
… | |
223 | Kills/terminates/cancels all coroutines except the currently running |
224 | Kills/terminates/cancels all coroutines except the currently running |
224 | one. This is useful after a fork, either in the child or the parent, |
225 | one. This is useful after a fork, either in the child or the parent, |
225 | as usually only one of them should inherit the running coroutines. |
226 | as usually only one of them should inherit the running coroutines. |
226 | |
227 | |
227 | Note that while this will try to free some of the main programs |
228 | Note that while this will try to free some of the main programs |
228 | resources, you cnanot free all of them, so if a coroutine that is |
229 | resources, you cannot free all of them, so if a coroutine that is |
229 | not the main program calls this function, there will be some |
230 | not the main program calls this function, there will be some |
230 | one-time resource leak. |
231 | one-time resource leak. |
231 | |
232 | |
232 | COROUTINE METHODS |
233 | COROUTINE METHODS |
233 | These are the methods you can call on coroutine objects (or to create |
234 | These are the methods you can call on coroutine objects (or to create |
… | |
… | |
251 | automatically once all the coroutines of higher priority and all |
252 | automatically once all the coroutines of higher priority and all |
252 | coroutines of the same priority that were put into the ready queue |
253 | coroutines of the same priority that were put into the ready queue |
253 | earlier have been resumed. |
254 | earlier have been resumed. |
254 | |
255 | |
255 | $is_ready = $coroutine->is_ready |
256 | $is_ready = $coroutine->is_ready |
256 | Return wether the coroutine is currently the ready queue or not, |
257 | Return whether the coroutine is currently the ready queue or not, |
257 | |
258 | |
258 | $coroutine->cancel (arg...) |
259 | $coroutine->cancel (arg...) |
259 | Terminates the given coroutine and makes it return the given |
260 | Terminates the given coroutine and makes it return the given |
260 | arguments as status (default: the empty list). Never returns if the |
261 | arguments as status (default: the empty list). Never returns if the |
261 | coroutine is the current coroutine. |
262 | coroutine is the current coroutine. |