ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.pod
(Generate patch)

Comparing libev/ev.pod (file contents):
Revision 1.179 by root, Sat Sep 13 19:14:21 2008 UTC vs.
Revision 1.182 by root, Fri Sep 19 03:52:56 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines