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

Comparing libev/ev.pod (file contents):
Revision 1.354 by root, Tue Jan 11 01:40:25 2011 UTC vs.
Revision 1.359 by root, Tue Jan 11 10:56:01 2011 UTC

1357See also C<ev_feed_fd_event> and C<ev_feed_signal_event> for related 1357See also C<ev_feed_fd_event> and C<ev_feed_signal_event> for related
1358functions that do not need a watcher. 1358functions that do not need a watcher.
1359 1359
1360=back 1360=back
1361 1361
1362=head2 ASSOCIATING CUSTOM DATA WITH A WATCHER 1362See also the L<ASSOCIATING CUSTOM DATA WITH A WATCHER> and L<BUILDING YOUR
1363 1363OWN COMPOSITE WATCHERS> idioms.
1364Each watcher has, by default, a member C<void *data> that you can change
1365and read at any time: libev will completely ignore it. This can be used
1366to associate arbitrary data with your watcher. If you need more data and
1367don't want to allocate memory and store a pointer to it in that data
1368member, you can also "subclass" the watcher type and provide your own
1369data:
1370
1371 struct my_io
1372 {
1373 ev_io io;
1374 int otherfd;
1375 void *somedata;
1376 struct whatever *mostinteresting;
1377 };
1378
1379 ...
1380 struct my_io w;
1381 ev_io_init (&w.io, my_cb, fd, EV_READ);
1382
1383And since your callback will be called with a pointer to the watcher, you
1384can cast it back to your own type:
1385
1386 static void my_cb (struct ev_loop *loop, ev_io *w_, int revents)
1387 {
1388 struct my_io *w = (struct my_io *)w_;
1389 ...
1390 }
1391
1392More interesting and less C-conformant ways of casting your callback type
1393instead have been omitted.
1394
1395Another common scenario is to use some data structure with multiple
1396embedded watchers:
1397
1398 struct my_biggy
1399 {
1400 int some_data;
1401 ev_timer t1;
1402 ev_timer t2;
1403 }
1404
1405In this case getting the pointer to C<my_biggy> is a bit more
1406complicated: Either you store the address of your C<my_biggy> struct
1407in the C<data> member of the watcher (for woozies), or you need to use
1408some pointer arithmetic using C<offsetof> inside your watchers (for real
1409programmers):
1410
1411 #include <stddef.h>
1412
1413 static void
1414 t1_cb (EV_P_ ev_timer *w, int revents)
1415 {
1416 struct my_biggy big = (struct my_biggy *)
1417 (((char *)w) - offsetof (struct my_biggy, t1));
1418 }
1419
1420 static void
1421 t2_cb (EV_P_ ev_timer *w, int revents)
1422 {
1423 struct my_biggy big = (struct my_biggy *)
1424 (((char *)w) - offsetof (struct my_biggy, t2));
1425 }
1426 1364
1427=head2 WATCHER STATES 1365=head2 WATCHER STATES
1428 1366
1429There are various watcher states mentioned throughout this manual - 1367There are various watcher states mentioned throughout this manual -
1430active, pending and so on. In this section these states and the rules to 1368active, pending and so on. In this section these states and the rules to
1680always get a readiness notification instantly, and your read (or possibly 1618always get a readiness notification instantly, and your read (or possibly
1681write) will still block on the disk I/O. 1619write) will still block on the disk I/O.
1682 1620
1683Another way to view it is that in the case of sockets, pipes, character 1621Another way to view it is that in the case of sockets, pipes, character
1684devices and so on, there is another party (the sender) that delivers data 1622devices and so on, there is another party (the sender) that delivers data
1685on it's own, but in the case of files, there is no such thing: the disk 1623on its own, but in the case of files, there is no such thing: the disk
1686will not send data on it's own, simply because it doesn't know what you 1624will not send data on its own, simply because it doesn't know what you
1687wish to read - you would first have to request some data. 1625wish to read - you would first have to request some data.
1688 1626
1689Since files are typically not-so-well supported by advanced notification 1627Since files are typically not-so-well supported by advanced notification
1690mechanism, libev tries hard to emulate POSIX behaviour with respect 1628mechanism, libev tries hard to emulate POSIX behaviour with respect
1691to files, even though you should not use it. The reason for this is 1629to files, even though you should not use it. The reason for this is
3456 3394
3457This section explains some common idioms that are not immediately 3395This section explains some common idioms that are not immediately
3458obvious. Note that examples are sprinkled over the whole manual, and this 3396obvious. Note that examples are sprinkled over the whole manual, and this
3459section only contains stuff that wouldn't fit anywhere else. 3397section only contains stuff that wouldn't fit anywhere else.
3460 3398
3461=over 4 3399=head2 ASSOCIATING CUSTOM DATA WITH A WATCHER
3462 3400
3463=item Model/nested event loop invocations and exit conditions. 3401Each watcher has, by default, a C<void *data> member that you can read
3402or modify at any time: libev will completely ignore it. This can be used
3403to associate arbitrary data with your watcher. If you need more data and
3404don't want to allocate memory separately and store a pointer to it in that
3405data member, you can also "subclass" the watcher type and provide your own
3406data:
3407
3408 struct my_io
3409 {
3410 ev_io io;
3411 int otherfd;
3412 void *somedata;
3413 struct whatever *mostinteresting;
3414 };
3415
3416 ...
3417 struct my_io w;
3418 ev_io_init (&w.io, my_cb, fd, EV_READ);
3419
3420And since your callback will be called with a pointer to the watcher, you
3421can cast it back to your own type:
3422
3423 static void my_cb (struct ev_loop *loop, ev_io *w_, int revents)
3424 {
3425 struct my_io *w = (struct my_io *)w_;
3426 ...
3427 }
3428
3429More interesting and less C-conformant ways of casting your callback
3430function type instead have been omitted.
3431
3432=head2 BUILDING YOUR OWN COMPOSITE WATCHERS
3433
3434Another common scenario is to use some data structure with multiple
3435embedded watchers, in effect creating your own watcher that combines
3436multiple libev event sources into one "super-watcher":
3437
3438 struct my_biggy
3439 {
3440 int some_data;
3441 ev_timer t1;
3442 ev_timer t2;
3443 }
3444
3445In this case getting the pointer to C<my_biggy> is a bit more
3446complicated: Either you store the address of your C<my_biggy> struct in
3447the C<data> member of the watcher (for woozies or C++ coders), or you need
3448to use some pointer arithmetic using C<offsetof> inside your watchers (for
3449real programmers):
3450
3451 #include <stddef.h>
3452
3453 static void
3454 t1_cb (EV_P_ ev_timer *w, int revents)
3455 {
3456 struct my_biggy big = (struct my_biggy *)
3457 (((char *)w) - offsetof (struct my_biggy, t1));
3458 }
3459
3460 static void
3461 t2_cb (EV_P_ ev_timer *w, int revents)
3462 {
3463 struct my_biggy big = (struct my_biggy *)
3464 (((char *)w) - offsetof (struct my_biggy, t2));
3465 }
3466
3467=head2 MODEL/NESTED EVENT LOOP INVOCATIONS AND EXIT CONDITIONS
3464 3468
3465Often (especially in GUI toolkits) there are places where you have 3469Often (especially in GUI toolkits) there are places where you have
3466I<modal> interaction, which is most easily implemented by recursively 3470I<modal> interaction, which is most easily implemented by recursively
3467invoking C<ev_run>. 3471invoking C<ev_run>.
3468 3472
3497 exit_main_loop = 1; 3501 exit_main_loop = 1;
3498 3502
3499 // exit both 3503 // exit both
3500 exit_main_loop = exit_nested_loop = 1; 3504 exit_main_loop = exit_nested_loop = 1;
3501 3505
3502=item Thread locking example 3506=head2 THREAD LOCKING EXAMPLE
3503 3507
3504Here is a fictitious example of how to run an event loop in a different 3508Here is a fictitious example of how to run an event loop in a different
3505thread than where callbacks are being invoked and watchers are 3509thread from where callbacks are being invoked and watchers are
3506created/added/removed. 3510created/added/removed.
3507 3511
3508For a real-world example, see the C<EV::Loop::Async> perl module, 3512For a real-world example, see the C<EV::Loop::Async> perl module,
3509which uses exactly this technique (which is suited for many high-level 3513which uses exactly this technique (which is suited for many high-level
3510languages). 3514languages).
3635Note that sending the C<ev_async> watcher is required because otherwise 3639Note that sending the C<ev_async> watcher is required because otherwise
3636an event loop currently blocking in the kernel will have no knowledge 3640an event loop currently blocking in the kernel will have no knowledge
3637about the newly added timer. By waking up the loop it will pick up any new 3641about the newly added timer. By waking up the loop it will pick up any new
3638watchers in the next event loop iteration. 3642watchers in the next event loop iteration.
3639 3643
3640=back 3644=head2 THREADS, COROUTINES, CONTINUATIONS, QUEUES... INSTEAD OF CALLBACKS
3645
3646While the overhead of a callback that e.g. schedules a thread is small, it
3647is still an overhead. If you embed libev, and your main usage is with some
3648kind of threads or coroutines, you might want to customise libev so that
3649doesn't need callbacks anymore.
3650
3651Imagine you have coroutines that you can switch to using a function
3652C<switch_to (coro)>, that libev runs in a coroutine called C<libev_coro>
3653and that due to some magic, the currently active coroutine is stored in a
3654global called C<current_coro>. Then you can build your own "wait for libev
3655event" primitive by changing C<EV_CB_DECLARE> and C<EV_CB_INVOKE> (note
3656the differing C<;> conventions):
3657
3658 #define EV_CB_DECLARE(type) struct my_coro *cb;
3659 #define EV_CB_INVOKE(watcher) switch_to ((watcher)->cb)
3660
3661That means instead of having a C callback function, you store the
3662coroutine to switch to in each watcher, and instead of having libev call
3663your callback, you instead have it switch to that coroutine.
3664
3665A coroutine might now wait for an event with a function called
3666C<wait_for_event>. (the watcher needs to be started, as always, but it doesn't
3667matter when, or whether the watcher is active or not when this function is
3668called):
3669
3670 void
3671 wait_for_event (ev_watcher *w)
3672 {
3673 ev_cb_set (w) = current_coro;
3674 switch_to (libev_coro);
3675 }
3676
3677That basically suspends the coroutine inside C<wait_for_event> and
3678continues the libev coroutine, which, when appropriate, switches back to
3679this or any other coroutine. I am sure if you sue this your own :)
3680
3681You can do similar tricks if you have, say, threads with an event queue -
3682instead of storing a coroutine, you store the queue object and instead of
3683switching to a coroutine, you push the watcher onto the queue and notify
3684any waiters.
3685
3686To embed libev, see L<EMBEDDING>, but in short, it's easiest to create two
3687files, F<my_ev.h> and F<my_ev.c> that include the respective libev files:
3688
3689 // my_ev.h
3690 #define EV_CB_DECLARE(type) struct my_coro *cb;
3691 #define EV_CB_INVOKE(watcher) switch_to ((watcher)->cb);
3692 #include "../libev/ev.h"
3693
3694 // my_ev.c
3695 #define EV_H "my_ev.h"
3696 #include "../libev/ev.c"
3697
3698And then use F<my_ev.h> when you would normally use F<ev.h>, and compile
3699F<my_ev.c> into your project. When properly specifying include paths, you
3700can even use F<ev.h> as header file name directly.
3641 3701
3642 3702
3643=head1 LIBEVENT EMULATION 3703=head1 LIBEVENT EMULATION
3644 3704
3645Libev offers a compatibility emulation layer for libevent. It cannot 3705Libev offers a compatibility emulation layer for libevent. It cannot
4575And a F<ev_cpp.C> implementation file that contains libev proper and is compiled: 4635And a F<ev_cpp.C> implementation file that contains libev proper and is compiled:
4576 4636
4577 #include "ev_cpp.h" 4637 #include "ev_cpp.h"
4578 #include "ev.c" 4638 #include "ev.c"
4579 4639
4580=head1 INTERACTION WITH OTHER PROGRAMS OR LIBRARIES 4640=head1 INTERACTION WITH OTHER PROGRAMS, LIBRARIES OR THE ENVIRONMENT
4581 4641
4582=head2 THREADS AND COROUTINES 4642=head2 THREADS AND COROUTINES
4583 4643
4584=head3 THREADS 4644=head3 THREADS
4585 4645
4636default loop and triggering an C<ev_async> watcher from the default loop 4696default loop and triggering an C<ev_async> watcher from the default loop
4637watcher callback into the event loop interested in the signal. 4697watcher callback into the event loop interested in the signal.
4638 4698
4639=back 4699=back
4640 4700
4641See also L<Thread locking example>. 4701See also L<THREAD LOCKING EXAMPLE>.
4642 4702
4643=head3 COROUTINES 4703=head3 COROUTINES
4644 4704
4645Libev is very accommodating to coroutines ("cooperative threads"): 4705Libev is very accommodating to coroutines ("cooperative threads"):
4646libev fully supports nesting calls to its functions from different 4706libev fully supports nesting calls to its functions from different

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines