… | |
… | |
3240 | |
3240 | |
3241 | =head1 THREADS AND COROUTINES |
3241 | =head1 THREADS AND COROUTINES |
3242 | |
3242 | |
3243 | =head2 THREADS |
3243 | =head2 THREADS |
3244 | |
3244 | |
3245 | Libev itself is completely thread-safe, but it uses no locking. This |
3245 | Libev itself is thread-safe (unless the opposite is specifically |
|
|
3246 | documented for a function), but it uses no locking itself. This means that |
3246 | means that you can use as many loops as you want in parallel, as long as |
3247 | you can use as many loops as you want in parallel, as long as only one |
3247 | only one thread ever calls into one libev function with the same loop |
3248 | thread ever calls into one libev function with the same loop parameter: |
3248 | parameter. |
3249 | libev guarentees that different event loops share no data structures that |
|
|
3250 | need locking. |
3249 | |
3251 | |
3250 | Or put differently: calls with different loop parameters can be done in |
3252 | Or to put it differently: calls with different loop parameters can be done |
3251 | parallel from multiple threads, calls with the same loop parameter must be |
3253 | concurrently from multiple threads, calls with the same loop parameter |
3252 | done serially (but can be done from different threads, as long as only one |
3254 | must be done serially (but can be done from different threads, as long as |
3253 | thread ever is inside a call at any point in time, e.g. by using a mutex |
3255 | only one thread ever is inside a call at any point in time, e.g. by using |
3254 | per loop). |
3256 | a mutex per loop). |
|
|
3257 | |
|
|
3258 | Specifically to support threads (and signal handlers), libev implements |
|
|
3259 | so-called C<ev_async> watchers, which allow some limited form of |
|
|
3260 | concurrency on the same event loop. |
3255 | |
3261 | |
3256 | If you want to know which design (one loop, locking, or multiple loops |
3262 | If you want to know which design (one loop, locking, or multiple loops |
3257 | without or something else still) is best for your problem, then I cannot |
3263 | without or something else still) is best for your problem, then I cannot |
3258 | help you. I can give some generic advice however: |
3264 | help you. I can give some generic advice however: |
3259 | |
3265 | |
… | |
… | |
3277 | better than you currently do :-) |
3283 | better than you currently do :-) |
3278 | |
3284 | |
3279 | =item * often you need to talk to some other thread which blocks in the |
3285 | =item * often you need to talk to some other thread which blocks in the |
3280 | event loop - C<ev_async> watchers can be used to wake them up from other |
3286 | event loop - C<ev_async> watchers can be used to wake them up from other |
3281 | threads safely (or from signal contexts...). |
3287 | threads safely (or from signal contexts...). |
|
|
3288 | |
|
|
3289 | =item * some watcher types are only supported in the default loop - use |
|
|
3290 | C<ev_async> watchers to tell your other loops about any such events. |
3282 | |
3291 | |
3283 | =back |
3292 | =back |
3284 | |
3293 | |
3285 | =head2 COROUTINES |
3294 | =head2 COROUTINES |
3286 | |
3295 | |
… | |
… | |
3289 | coroutines (e.g. you can call C<ev_loop> on the same loop from two |
3298 | coroutines (e.g. you can call C<ev_loop> on the same loop from two |
3290 | different coroutines and switch freely between both coroutines running the |
3299 | different coroutines and switch freely between both coroutines running the |
3291 | loop, as long as you don't confuse yourself). The only exception is that |
3300 | loop, as long as you don't confuse yourself). The only exception is that |
3292 | you must not do this from C<ev_periodic> reschedule callbacks. |
3301 | you must not do this from C<ev_periodic> reschedule callbacks. |
3293 | |
3302 | |
3294 | Care has been invested into making sure that libev does not keep local |
3303 | Care has been taken to ensure that libev does not keep local state inside |
3295 | state inside C<ev_loop>, and other calls do not usually allow coroutine |
3304 | C<ev_loop>, and other calls do not usually allow coroutine switches. |
3296 | switches. |
|
|
3297 | |
3305 | |
3298 | |
3306 | |
3299 | =head1 COMPLEXITIES |
3307 | =head1 COMPLEXITIES |
3300 | |
3308 | |
3301 | In this section the complexities of (many of) the algorithms used inside |
3309 | In this section the complexities of (many of) the algorithms used inside |