… | |
… | |
1357 | See also C<ev_feed_fd_event> and C<ev_feed_signal_event> for related |
1357 | See also C<ev_feed_fd_event> and C<ev_feed_signal_event> for related |
1358 | functions that do not need a watcher. |
1358 | functions that do not need a watcher. |
1359 | |
1359 | |
1360 | =back |
1360 | =back |
1361 | |
1361 | |
1362 | =head2 ASSOCIATING CUSTOM DATA WITH A WATCHER |
1362 | See also the L<ASSOCIATING CUSTOM DATA WITH A WATCHER> and L<BUILDING YOUR |
1363 | |
1363 | OWN COMPOSITE WATCHERS> idioms. |
1364 | Each watcher has, by default, a member C<void *data> that you can change |
|
|
1365 | and read at any time: libev will completely ignore it. This can be used |
|
|
1366 | to associate arbitrary data with your watcher. If you need more data and |
|
|
1367 | don't want to allocate memory and store a pointer to it in that data |
|
|
1368 | member, you can also "subclass" the watcher type and provide your own |
|
|
1369 | data: |
|
|
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 | |
|
|
1383 | And since your callback will be called with a pointer to the watcher, you |
|
|
1384 | can 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 | |
|
|
1392 | More interesting and less C-conformant ways of casting your callback type |
|
|
1393 | instead have been omitted. |
|
|
1394 | |
|
|
1395 | Another common scenario is to use some data structure with multiple |
|
|
1396 | embedded watchers: |
|
|
1397 | |
|
|
1398 | struct my_biggy |
|
|
1399 | { |
|
|
1400 | int some_data; |
|
|
1401 | ev_timer t1; |
|
|
1402 | ev_timer t2; |
|
|
1403 | } |
|
|
1404 | |
|
|
1405 | In this case getting the pointer to C<my_biggy> is a bit more |
|
|
1406 | complicated: Either you store the address of your C<my_biggy> struct |
|
|
1407 | in the C<data> member of the watcher (for woozies), or you need to use |
|
|
1408 | some pointer arithmetic using C<offsetof> inside your watchers (for real |
|
|
1409 | programmers): |
|
|
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 | |
1429 | There are various watcher states mentioned throughout this manual - |
1367 | There are various watcher states mentioned throughout this manual - |
1430 | active, pending and so on. In this section these states and the rules to |
1368 | active, pending and so on. In this section these states and the rules to |
… | |
… | |
1680 | always get a readiness notification instantly, and your read (or possibly |
1618 | always get a readiness notification instantly, and your read (or possibly |
1681 | write) will still block on the disk I/O. |
1619 | write) will still block on the disk I/O. |
1682 | |
1620 | |
1683 | Another way to view it is that in the case of sockets, pipes, character |
1621 | Another way to view it is that in the case of sockets, pipes, character |
1684 | devices and so on, there is another party (the sender) that delivers data |
1622 | devices and so on, there is another party (the sender) that delivers data |
1685 | on it's own, but in the case of files, there is no such thing: the disk |
1623 | on its own, but in the case of files, there is no such thing: the disk |
1686 | will not send data on it's own, simply because it doesn't know what you |
1624 | will not send data on its own, simply because it doesn't know what you |
1687 | wish to read - you would first have to request some data. |
1625 | wish to read - you would first have to request some data. |
1688 | |
1626 | |
1689 | Since files are typically not-so-well supported by advanced notification |
1627 | Since files are typically not-so-well supported by advanced notification |
1690 | mechanism, libev tries hard to emulate POSIX behaviour with respect |
1628 | mechanism, libev tries hard to emulate POSIX behaviour with respect |
1691 | to files, even though you should not use it. The reason for this is |
1629 | to files, even though you should not use it. The reason for this is |
… | |
… | |
3456 | |
3394 | |
3457 | This section explains some common idioms that are not immediately |
3395 | This section explains some common idioms that are not immediately |
3458 | obvious. Note that examples are sprinkled over the whole manual, and this |
3396 | obvious. Note that examples are sprinkled over the whole manual, and this |
3459 | section only contains stuff that wouldn't fit anywhere else. |
3397 | section only contains stuff that wouldn't fit anywhere else. |
3460 | |
3398 | |
|
|
3399 | =head2 ASSOCIATING CUSTOM DATA WITH A WATCHER |
|
|
3400 | |
|
|
3401 | Each watcher has, by default, a C<void *data> member that you can read |
|
|
3402 | or modify at any time: libev will completely ignore it. This can be used |
|
|
3403 | to associate arbitrary data with your watcher. If you need more data and |
|
|
3404 | don't want to allocate memory separately and store a pointer to it in that |
|
|
3405 | data member, you can also "subclass" the watcher type and provide your own |
|
|
3406 | data: |
|
|
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 | |
|
|
3420 | And since your callback will be called with a pointer to the watcher, you |
|
|
3421 | can 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 | |
|
|
3429 | More interesting and less C-conformant ways of casting your callback |
|
|
3430 | function type instead have been omitted. |
|
|
3431 | |
|
|
3432 | =head2 BUILDING YOUR OWN COMPOSITE WATCHERS |
|
|
3433 | |
|
|
3434 | Another common scenario is to use some data structure with multiple |
|
|
3435 | embedded watchers, in effect creating your own watcher that combines |
|
|
3436 | multiple 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 | |
|
|
3445 | In this case getting the pointer to C<my_biggy> is a bit more |
|
|
3446 | complicated: Either you store the address of your C<my_biggy> struct in |
|
|
3447 | the C<data> member of the watcher (for woozies or C++ coders), or you need |
|
|
3448 | to use some pointer arithmetic using C<offsetof> inside your watchers (for |
|
|
3449 | real 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 | |
3461 | =head2 MODEL/NESTED EVENT LOOP INVOCATIONS AND EXIT CONDITIONS |
3467 | =head2 MODEL/NESTED EVENT LOOP INVOCATIONS AND EXIT CONDITIONS |
3462 | |
3468 | |
3463 | Often (especially in GUI toolkits) there are places where you have |
3469 | Often (especially in GUI toolkits) there are places where you have |
3464 | I<modal> interaction, which is most easily implemented by recursively |
3470 | I<modal> interaction, which is most easily implemented by recursively |
3465 | invoking C<ev_run>. |
3471 | invoking C<ev_run>. |
… | |
… | |
3498 | exit_main_loop = exit_nested_loop = 1; |
3504 | exit_main_loop = exit_nested_loop = 1; |
3499 | |
3505 | |
3500 | =head2 THREAD LOCKING EXAMPLE |
3506 | =head2 THREAD LOCKING EXAMPLE |
3501 | |
3507 | |
3502 | Here is a fictitious example of how to run an event loop in a different |
3508 | Here is a fictitious example of how to run an event loop in a different |
3503 | thread than where callbacks are being invoked and watchers are |
3509 | thread from where callbacks are being invoked and watchers are |
3504 | created/added/removed. |
3510 | created/added/removed. |
3505 | |
3511 | |
3506 | For a real-world example, see the C<EV::Loop::Async> perl module, |
3512 | For a real-world example, see the C<EV::Loop::Async> perl module, |
3507 | which uses exactly this technique (which is suited for many high-level |
3513 | which uses exactly this technique (which is suited for many high-level |
3508 | languages). |
3514 | languages). |
… | |
… | |
3633 | Note that sending the C<ev_async> watcher is required because otherwise |
3639 | Note that sending the C<ev_async> watcher is required because otherwise |
3634 | an event loop currently blocking in the kernel will have no knowledge |
3640 | an event loop currently blocking in the kernel will have no knowledge |
3635 | about the newly added timer. By waking up the loop it will pick up any new |
3641 | about the newly added timer. By waking up the loop it will pick up any new |
3636 | watchers in the next event loop iteration. |
3642 | watchers in the next event loop iteration. |
3637 | |
3643 | |
3638 | =back |
3644 | =head2 THREADS, COROUTINES, CONTINUATIONS, QUEUES... INSTEAD OF CALLBACKS |
|
|
3645 | |
|
|
3646 | While the overhead of a callback that e.g. schedules a thread is small, it |
|
|
3647 | is still an overhead. If you embed libev, and your main usage is with some |
|
|
3648 | kind of threads or coroutines, you might want to customise libev so that |
|
|
3649 | doesn't need callbacks anymore. |
|
|
3650 | |
|
|
3651 | Imagine you have coroutines that you can switch to using a function |
|
|
3652 | C<switch_to (coro)>, that libev runs in a coroutine called C<libev_coro> |
|
|
3653 | and that due to some magic, the currently active coroutine is stored in a |
|
|
3654 | global called C<current_coro>. Then you can build your own "wait for libev |
|
|
3655 | event" primitive by changing C<EV_CB_DECLARE> and C<EV_CB_INVOKE> (note |
|
|
3656 | the 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 | |
|
|
3661 | That means instead of having a C callback function, you store the |
|
|
3662 | coroutine to switch to in each watcher, and instead of having libev call |
|
|
3663 | your callback, you instead have it switch to that coroutine. |
|
|
3664 | |
|
|
3665 | A coroutine might now wait for an event with a function called |
|
|
3666 | C<wait_for_event>. (the watcher needs to be started, as always, but it doesn't |
|
|
3667 | matter when, or whether the watcher is active or not when this function is |
|
|
3668 | called): |
|
|
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 | |
|
|
3677 | That basically suspends the coroutine inside C<wait_for_event> and |
|
|
3678 | continues the libev coroutine, which, when appropriate, switches back to |
|
|
3679 | this or any other coroutine. I am sure if you sue this your own :) |
|
|
3680 | |
|
|
3681 | You can do similar tricks if you have, say, threads with an event queue - |
|
|
3682 | instead of storing a coroutine, you store the queue object and instead of |
|
|
3683 | switching to a coroutine, you push the watcher onto the queue and notify |
|
|
3684 | any waiters. |
|
|
3685 | |
|
|
3686 | To embed libev, see L<EMBEDDING>, but in short, it's easiest to create two |
|
|
3687 | files, 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 | |
|
|
3698 | And then use F<my_ev.h> when you would normally use F<ev.h>, and compile |
|
|
3699 | F<my_ev.c> into your project. When properly specifying include paths, you |
|
|
3700 | can even use F<ev.h> as header file name directly. |
3639 | |
3701 | |
3640 | |
3702 | |
3641 | =head1 LIBEVENT EMULATION |
3703 | =head1 LIBEVENT EMULATION |
3642 | |
3704 | |
3643 | Libev offers a compatibility emulation layer for libevent. It cannot |
3705 | Libev offers a compatibility emulation layer for libevent. It cannot |
… | |
… | |
4573 | And a F<ev_cpp.C> implementation file that contains libev proper and is compiled: |
4635 | And a F<ev_cpp.C> implementation file that contains libev proper and is compiled: |
4574 | |
4636 | |
4575 | #include "ev_cpp.h" |
4637 | #include "ev_cpp.h" |
4576 | #include "ev.c" |
4638 | #include "ev.c" |
4577 | |
4639 | |
4578 | =head1 INTERACTION WITH OTHER PROGRAMS OR LIBRARIES |
4640 | =head1 INTERACTION WITH OTHER PROGRAMS, LIBRARIES OR THE ENVIRONMENT |
4579 | |
4641 | |
4580 | =head2 THREADS AND COROUTINES |
4642 | =head2 THREADS AND COROUTINES |
4581 | |
4643 | |
4582 | =head3 THREADS |
4644 | =head3 THREADS |
4583 | |
4645 | |