… | |
… | |
17 | cede; |
17 | cede; |
18 | |
18 | |
19 | DESCRIPTION |
19 | DESCRIPTION |
20 | This module collection manages coroutines. Coroutines are similar to |
20 | This module collection manages coroutines. Coroutines are similar to |
21 | threads but don't run in parallel at the same time even on SMP machines. |
21 | threads but don't run in parallel at the same time even on SMP machines. |
22 | The specific flavor of coroutine use din this module also guarentees you |
22 | The specific flavor of coroutine used in this module also guarantees you |
23 | that it will not switch between coroutines unless necessary, at |
23 | that it will not switch between coroutines unless necessary, at |
24 | easily-identified points in your program, so locking and parallel access |
24 | easily-identified points in your program, so locking and parallel access |
25 | are rarely an issue, making coroutine programming much safer than |
25 | are rarely an issue, making coroutine programming much safer than |
26 | threads programming. |
26 | threads programming. |
27 | |
27 | |
… | |
… | |
41 | $current (or as function: current) |
41 | $current (or as function: current) |
42 | The current coroutine (the last coroutine switched to). The initial |
42 | The current coroutine (the last coroutine switched to). The initial |
43 | value is $main (of course). |
43 | value is $main (of course). |
44 | |
44 | |
45 | This variable is strictly *read-only*. It is provided for |
45 | This variable is strictly *read-only*. It is provided for |
46 | performance reasons. If performance is not essentiel you are |
46 | performance reasons. If performance is not essential you are |
47 | encouraged to use the "Coro::current" function instead. |
47 | encouraged to use the "Coro::current" function instead. |
48 | |
48 | |
49 | $idle |
49 | $idle |
50 | A callback that is called whenever the scheduler finds no ready |
50 | A callback that is called whenever the scheduler finds no ready |
51 | coroutines to run. The default implementation prints "FATAL: |
51 | coroutines to run. The default implementation prints "FATAL: |
… | |
… | |
66 | async { ... } [@args...] |
66 | async { ... } [@args...] |
67 | Create a new asynchronous coroutine and return it's coroutine object |
67 | Create a new asynchronous coroutine and return it's coroutine object |
68 | (usually unused). When the sub returns the new coroutine is |
68 | (usually unused). When the sub returns the new coroutine is |
69 | automatically terminated. |
69 | automatically terminated. |
70 | |
70 | |
71 | Calling "exit" in a coroutine will not work correctly, so do not do |
71 | See the "Coro::State::new" constructor for info about the coroutine |
72 | that. |
72 | environment. |
73 | |
73 | |
74 | When the coroutine dies, the program will exit, just as in the main |
74 | Calling "exit" in a coroutine will do the same as calling exit |
75 | program. |
75 | outside the coroutine. Likewise, when the coroutine dies, the |
|
|
76 | program will exit, just as it would in the main program. |
76 | |
77 | |
77 | # create a new coroutine that just prints its arguments |
78 | # create a new coroutine that just prints its arguments |
78 | async { |
79 | async { |
79 | print "@_\n"; |
80 | print "@_\n"; |
80 | } 1,2,3,4; |
81 | } 1,2,3,4; |
… | |
… | |
89 | be issued in case of an exception instead of terminating the |
90 | be issued in case of an exception instead of terminating the |
90 | program, as "async" does. As the coroutine is being reused, stuff |
91 | program, as "async" does. As the coroutine is being reused, stuff |
91 | like "on_destroy" will not work in the expected way, unless you call |
92 | like "on_destroy" will not work in the expected way, unless you call |
92 | terminate or cancel, which somehow defeats the purpose of pooling. |
93 | terminate or cancel, which somehow defeats the purpose of pooling. |
93 | |
94 | |
94 | The priority will be reset to 0 after each job, otherwise the |
95 | The priority will be reset to 0 after each job, tracing will be |
95 | coroutine will be re-used "as-is". |
96 | disabled, the description will be reset and the default output |
|
|
97 | filehandle gets restored, so you can change alkl these. Otherwise |
|
|
98 | the coroutine will be re-used "as-is": most notably if you change |
|
|
99 | other per-coroutine global stuff such as $/ you need to revert that |
|
|
100 | change, which is most simply done by using local as in " local $/ ". |
96 | |
101 | |
97 | The pool size is limited to 8 idle coroutines (this can be adjusted |
102 | The pool size is limited to 8 idle coroutines (this can be adjusted |
98 | by changing $Coro::POOL_SIZE), and there can be as many non-idle |
103 | by changing $Coro::POOL_SIZE), and there can be as many non-idle |
99 | coros as required. |
104 | coros as required. |
100 | |
105 | |
101 | If you are concerned about pooled coroutines growing a lot because a |
106 | If you are concerned about pooled coroutines growing a lot because a |
102 | single "async_pool" used a lot of stackspace you can e.g. |
107 | single "async_pool" used a lot of stackspace you can e.g. |
103 | "async_pool { terminate }" once per second or so to slowly replenish |
108 | "async_pool { terminate }" once per second or so to slowly replenish |
104 | the pool. |
109 | the pool. In addition to that, when the stacks used by a handler |
|
|
110 | grows larger than 16kb (adjustable with $Coro::POOL_RSS) it will |
|
|
111 | also exit. |
105 | |
112 | |
106 | schedule |
113 | schedule |
107 | Calls the scheduler. Please note that the current coroutine will not |
114 | Calls the scheduler. Please note that the current coroutine will not |
108 | be put into the ready queue, so calling this function usually means |
115 | be put into the ready queue, so calling this function usually means |
109 | you will never be called again unless something else (e.g. an event |
116 | you will never be called again unless something else (e.g. an event |
… | |
… | |
120 | # wake up sleeping coroutine |
127 | # wake up sleeping coroutine |
121 | $current->ready; |
128 | $current->ready; |
122 | undef $current; |
129 | undef $current; |
123 | }; |
130 | }; |
124 | |
131 | |
125 | # call schedule until event occured. |
132 | # call schedule until event occurred. |
126 | # in case we are woken up for other reasons |
133 | # in case we are woken up for other reasons |
127 | # (current still defined), loop. |
134 | # (current still defined), loop. |
128 | Coro::schedule while $current; |
135 | Coro::schedule while $current; |
129 | } |
136 | } |
130 | |
137 | |
… | |
… | |
144 | |
151 | |
145 | terminate [arg...] |
152 | terminate [arg...] |
146 | Terminates the current coroutine with the given status values (see |
153 | Terminates the current coroutine with the given status values (see |
147 | cancel). |
154 | cancel). |
148 | |
155 | |
|
|
156 | killall |
|
|
157 | Kills/terminates/cancels all coroutines except the currently running |
|
|
158 | one. This is useful after a fork, either in the child or the parent, |
|
|
159 | as usually only one of them should inherit the running coroutines. |
|
|
160 | |
149 | # dynamic methods |
161 | # dynamic methods |
150 | |
162 | |
151 | COROUTINE METHODS |
163 | COROUTINE METHODS |
152 | These are the methods you can call on coroutine objects. |
164 | These are the methods you can call on coroutine objects. |
153 | |
165 | |
… | |
… | |
155 | Create a new coroutine and return it. When the sub returns the |
167 | Create a new coroutine and return it. When the sub returns the |
156 | coroutine automatically terminates as if "terminate" with the |
168 | coroutine automatically terminates as if "terminate" with the |
157 | returned values were called. To make the coroutine run you must |
169 | returned values were called. To make the coroutine run you must |
158 | first put it into the ready queue by calling the ready method. |
170 | first put it into the ready queue by calling the ready method. |
159 | |
171 | |
160 | Calling "exit" in a coroutine will not work correctly, so do not do |
172 | See "async" and "Coro::State::new" for additional info about the |
161 | that. |
173 | coroutine environment. |
162 | |
174 | |
163 | $success = $coroutine->ready |
175 | $success = $coroutine->ready |
164 | Put the given coroutine into the ready queue (according to it's |
176 | Put the given coroutine into the ready queue (according to it's |
165 | priority) and return true. If the coroutine is already in the ready |
177 | priority) and return true. If the coroutine is already in the ready |
166 | queue, do nothing and return false. |
178 | queue, do nothing and return false. |
… | |
… | |
173 | arguments as status (default: the empty list). Never returns if the |
185 | arguments as status (default: the empty list). Never returns if the |
174 | coroutine is the current coroutine. |
186 | coroutine is the current coroutine. |
175 | |
187 | |
176 | $coroutine->join |
188 | $coroutine->join |
177 | Wait until the coroutine terminates and return any values given to |
189 | Wait until the coroutine terminates and return any values given to |
178 | the "terminate" or "cancel" functions. "join" can be called multiple |
190 | the "terminate" or "cancel" functions. "join" can be called |
179 | times from multiple coroutine. |
191 | concurrently from multiple coroutines. |
180 | |
192 | |
181 | $coroutine->on_destroy (\&cb) |
193 | $coroutine->on_destroy (\&cb) |
182 | Registers a callback that is called when this coroutine gets |
194 | Registers a callback that is called when this coroutine gets |
183 | destroyed, but before it is joined. The callback gets passed the |
195 | destroyed, but before it is joined. The callback gets passed the |
184 | terminate arguments, if any. |
196 | terminate arguments, if any. |
… | |
… | |
212 | $olddesc = $coroutine->desc ($newdesc) |
224 | $olddesc = $coroutine->desc ($newdesc) |
213 | Sets (or gets in case the argument is missing) the description for |
225 | Sets (or gets in case the argument is missing) the description for |
214 | this coroutine. This is just a free-form string you can associate |
226 | this coroutine. This is just a free-form string you can associate |
215 | with a coroutine. |
227 | with a coroutine. |
216 | |
228 | |
|
|
229 | This method simply sets the "$coroutine->{desc}" member to the given |
|
|
230 | string. You can modify this member directly if you wish. |
|
|
231 | |
217 | GLOBAL FUNCTIONS |
232 | GLOBAL FUNCTIONS |
218 | Coro::nready |
233 | Coro::nready |
219 | Returns the number of coroutines that are currently in the ready |
234 | Returns the number of coroutines that are currently in the ready |
220 | state, i.e. that can be swicthed to. The value 0 means that the only |
235 | state, i.e. that can be switched to. The value 0 means that the only |
221 | runnable coroutine is the currently running one, so "cede" would |
236 | runnable coroutine is the currently running one, so "cede" would |
222 | have no effect, and "schedule" would cause a deadlock unless there |
237 | have no effect, and "schedule" would cause a deadlock unless there |
223 | is an idle handler that wakes up some coroutines. |
238 | is an idle handler that wakes up some coroutines. |
224 | |
239 | |
225 | my $guard = Coro::guard { ... } |
240 | my $guard = Coro::guard { ... } |
226 | This creates and returns a guard object. Nothing happens until the |
241 | This creates and returns a guard object. Nothing happens until the |
227 | objetc gets destroyed, in which case the codeblock given as argument |
242 | object gets destroyed, in which case the codeblock given as argument |
228 | will be executed. This is useful to free locks or other resources in |
243 | will be executed. This is useful to free locks or other resources in |
229 | case of a runtime error or when the coroutine gets canceled, as in |
244 | case of a runtime error or when the coroutine gets canceled, as in |
230 | both cases the guard block will be executed. The guard object |
245 | both cases the guard block will be executed. The guard object |
231 | supports only one method, "->cancel", which will keep the codeblock |
246 | supports only one method, "->cancel", which will keep the codeblock |
232 | from being executed. |
247 | from being executed. |
… | |
… | |
246 | it, returning the new coderef. This means that the new coderef will |
261 | it, returning the new coderef. This means that the new coderef will |
247 | return immediately without blocking, returning nothing, while the |
262 | return immediately without blocking, returning nothing, while the |
248 | original code ref will be called (with parameters) from within its |
263 | original code ref will be called (with parameters) from within its |
249 | own coroutine. |
264 | own coroutine. |
250 | |
265 | |
251 | The reason this fucntion exists is that many event libraries (such |
266 | The reason this function exists is that many event libraries (such |
252 | as the venerable Event module) are not coroutine-safe (a weaker form |
267 | as the venerable Event module) are not coroutine-safe (a weaker form |
253 | of thread-safety). This means you must not block within event |
268 | of thread-safety). This means you must not block within event |
254 | callbacks, otherwise you might suffer from crashes or worse. |
269 | callbacks, otherwise you might suffer from crashes or worse. |
255 | |
270 | |
256 | This function allows your callbacks to block by executing them in |
271 | This function allows your callbacks to block by executing them in |
… | |
… | |
264 | BUGS/LIMITATIONS |
279 | BUGS/LIMITATIONS |
265 | - you must make very sure that no coro is still active on global |
280 | - you must make very sure that no coro is still active on global |
266 | destruction. very bad things might happen otherwise (usually segfaults). |
281 | destruction. very bad things might happen otherwise (usually segfaults). |
267 | |
282 | |
268 | - this module is not thread-safe. You should only ever use this module |
283 | - this module is not thread-safe. You should only ever use this module |
269 | from the same thread (this requirement might be losened in the future |
284 | from the same thread (this requirement might be loosened in the future |
270 | to allow per-thread schedulers, but Coro::State does not yet allow |
285 | to allow per-thread schedulers, but Coro::State does not yet allow |
271 | this). |
286 | this). |
272 | |
287 | |
273 | SEE ALSO |
288 | SEE ALSO |
274 | Support/Utility: Coro::Cont, Coro::Specific, Coro::State, Coro::Util. |
289 | Support/Utility: Coro::Specific, Coro::State, Coro::Util. |
275 | |
290 | |
276 | Locking/IPC: Coro::Signal, Coro::Channel, Coro::Semaphore, |
291 | Locking/IPC: Coro::Signal, Coro::Channel, Coro::Semaphore, |
277 | Coro::SemaphoreSet, Coro::RWLock. |
292 | Coro::SemaphoreSet, Coro::RWLock. |
278 | |
293 | |
279 | Event/IO: Coro::Timer, Coro::Event, Coro::Handle, Coro::Socket, |
294 | Event/IO: Coro::Timer, Coro::Event, Coro::Handle, Coro::Socket, |