… | |
… | |
299 | } |
299 | } |
300 | |
300 | |
301 | ... |
301 | ... |
302 | ev_set_syserr_cb (fatal_error); |
302 | ev_set_syserr_cb (fatal_error); |
303 | |
303 | |
|
|
304 | =item ev_feed_signal (int signum) |
|
|
305 | |
|
|
306 | This function can be used to "simulate" a signal receive. It is completely |
|
|
307 | safe to call this function at any time, from any context, including signal |
|
|
308 | handlers or random threads. |
|
|
309 | |
|
|
310 | Its main use is to customise signal handling in your process, especially |
|
|
311 | in the presence of threads. For example, you could block signals |
|
|
312 | by default in all threads (and specifying C<EVFLAG_NOSIGMASK> when |
|
|
313 | creating any loops), and in one thread, use C<sigwait> or any other |
|
|
314 | mechanism to wait for signals, then "deliver" them to libev by calling |
|
|
315 | C<ev_feed_signal>. |
|
|
316 | |
304 | =back |
317 | =back |
305 | |
318 | |
306 | =head1 FUNCTIONS CONTROLLING EVENT LOOPS |
319 | =head1 FUNCTIONS CONTROLLING EVENT LOOPS |
307 | |
320 | |
308 | An event loop is described by a C<struct ev_loop *> (the C<struct> is |
321 | An event loop is described by a C<struct ev_loop *> (the C<struct> is |
… | |
… | |
418 | threads that are not interested in handling them. |
431 | threads that are not interested in handling them. |
419 | |
432 | |
420 | Signalfd will not be used by default as this changes your signal mask, and |
433 | Signalfd will not be used by default as this changes your signal mask, and |
421 | there are a lot of shoddy libraries and programs (glib's threadpool for |
434 | there are a lot of shoddy libraries and programs (glib's threadpool for |
422 | example) that can't properly initialise their signal masks. |
435 | example) that can't properly initialise their signal masks. |
|
|
436 | |
|
|
437 | =item C<EVFLAG_NOSIGMASK> |
|
|
438 | |
|
|
439 | When this flag is specified, then libev will avoid to modify the signal |
|
|
440 | mask. Specifically, this means you ahve to make sure signals are unblocked |
|
|
441 | when you want to receive them. |
|
|
442 | |
|
|
443 | This behaviour is useful when you want to do your own signal handling, or |
|
|
444 | want to handle signals only in specific threads and want to avoid libev |
|
|
445 | unblocking the signals. |
|
|
446 | |
|
|
447 | This flag's behaviour will become the default in future versions of libev. |
423 | |
448 | |
424 | =item C<EVBACKEND_SELECT> (value 1, portable select backend) |
449 | =item C<EVBACKEND_SELECT> (value 1, portable select backend) |
425 | |
450 | |
426 | This is your standard select(2) backend. Not I<completely> standard, as |
451 | This is your standard select(2) backend. Not I<completely> standard, as |
427 | libev tries to roll its own fd_set with no limits on the number of fds, |
452 | libev tries to roll its own fd_set with no limits on the number of fds, |
… | |
… | |
481 | employing an additional generation counter and comparing that against the |
506 | employing an additional generation counter and comparing that against the |
482 | events to filter out spurious ones, recreating the set when required. Last |
507 | events to filter out spurious ones, recreating the set when required. Last |
483 | not least, it also refuses to work with some file descriptors which work |
508 | not least, it also refuses to work with some file descriptors which work |
484 | perfectly fine with C<select> (files, many character devices...). |
509 | perfectly fine with C<select> (files, many character devices...). |
485 | |
510 | |
486 | Epoll is truly the train wreck analog among event poll mechanisms. |
511 | Epoll is truly the train wreck analog among event poll mechanisms, |
|
|
512 | a frankenpoll, cobbled together in a hurry, no thought to design or |
|
|
513 | interaction with others. |
487 | |
514 | |
488 | While stopping, setting and starting an I/O watcher in the same iteration |
515 | While stopping, setting and starting an I/O watcher in the same iteration |
489 | will result in some caching, there is still a system call per such |
516 | will result in some caching, there is still a system call per such |
490 | incident (because the same I<file descriptor> could point to a different |
517 | incident (because the same I<file descriptor> could point to a different |
491 | I<file description> now), so its best to avoid that. Also, C<dup ()>'ed |
518 | I<file description> now), so its best to avoid that. Also, C<dup ()>'ed |
… | |
… | |
557 | =item C<EVBACKEND_PORT> (value 32, Solaris 10) |
584 | =item C<EVBACKEND_PORT> (value 32, Solaris 10) |
558 | |
585 | |
559 | This uses the Solaris 10 event port mechanism. As with everything on Solaris, |
586 | This uses the Solaris 10 event port mechanism. As with everything on Solaris, |
560 | it's really slow, but it still scales very well (O(active_fds)). |
587 | it's really slow, but it still scales very well (O(active_fds)). |
561 | |
588 | |
562 | Please note that Solaris event ports can deliver a lot of spurious |
|
|
563 | notifications, so you need to use non-blocking I/O or other means to avoid |
|
|
564 | blocking when no data (or space) is available. |
|
|
565 | |
|
|
566 | While this backend scales well, it requires one system call per active |
589 | While this backend scales well, it requires one system call per active |
567 | file descriptor per loop iteration. For small and medium numbers of file |
590 | file descriptor per loop iteration. For small and medium numbers of file |
568 | descriptors a "slow" C<EVBACKEND_SELECT> or C<EVBACKEND_POLL> backend |
591 | descriptors a "slow" C<EVBACKEND_SELECT> or C<EVBACKEND_POLL> backend |
569 | might perform better. |
592 | might perform better. |
570 | |
593 | |
571 | On the positive side, with the exception of the spurious readiness |
594 | On the positive side, this backend actually performed fully to |
572 | notifications, this backend actually performed fully to specification |
|
|
573 | in all tests and is fully embeddable, which is a rare feat among the |
595 | specification in all tests and is fully embeddable, which is a rare feat |
574 | OS-specific backends (I vastly prefer correctness over speed hacks). |
596 | among the OS-specific backends (I vastly prefer correctness over speed |
|
|
597 | hacks). |
|
|
598 | |
|
|
599 | On the negative side, the interface is I<bizarre> - so bizarre that |
|
|
600 | even sun itself gets it wrong in their code examples: The event polling |
|
|
601 | function sometimes returning events to the caller even though an error |
|
|
602 | occurred, but with no indication whether it has done so or not (yes, it's |
|
|
603 | even documented that way) - deadly for edge-triggered interfaces where |
|
|
604 | you absolutely have to know whether an event occurred or not because you |
|
|
605 | have to re-arm the watcher. |
|
|
606 | |
|
|
607 | Fortunately libev seems to be able to work around these idiocies. |
575 | |
608 | |
576 | This backend maps C<EV_READ> and C<EV_WRITE> in the same way as |
609 | This backend maps C<EV_READ> and C<EV_WRITE> in the same way as |
577 | C<EVBACKEND_POLL>. |
610 | C<EVBACKEND_POLL>. |
578 | |
611 | |
579 | =item C<EVBACKEND_ALL> |
612 | =item C<EVBACKEND_ALL> |
580 | |
613 | |
581 | Try all backends (even potentially broken ones that wouldn't be tried |
614 | Try all backends (even potentially broken ones that wouldn't be tried |
582 | with C<EVFLAG_AUTO>). Since this is a mask, you can do stuff such as |
615 | with C<EVFLAG_AUTO>). Since this is a mask, you can do stuff such as |
583 | C<EVBACKEND_ALL & ~EVBACKEND_KQUEUE>. |
616 | C<EVBACKEND_ALL & ~EVBACKEND_KQUEUE>. |
584 | |
617 | |
585 | It is definitely not recommended to use this flag. |
618 | It is definitely not recommended to use this flag, use whatever |
|
|
619 | C<ev_recommended_backends ()> returns, or simply do not specify a backend |
|
|
620 | at all. |
|
|
621 | |
|
|
622 | =item C<EVBACKEND_MASK> |
|
|
623 | |
|
|
624 | Not a backend at all, but a mask to select all backend bits from a |
|
|
625 | C<flags> value, in case you want to mask out any backends from a flags |
|
|
626 | value (e.g. when modifying the C<LIBEV_FLAGS> environment variable). |
586 | |
627 | |
587 | =back |
628 | =back |
588 | |
629 | |
589 | If one or more of the backend flags are or'ed into the flags value, |
630 | If one or more of the backend flags are or'ed into the flags value, |
590 | then only these backends will be tried (in the reverse order as listed |
631 | then only these backends will be tried (in the reverse order as listed |
… | |
… | |
867 | running when nothing else is active. |
908 | running when nothing else is active. |
868 | |
909 | |
869 | ev_signal exitsig; |
910 | ev_signal exitsig; |
870 | ev_signal_init (&exitsig, sig_cb, SIGINT); |
911 | ev_signal_init (&exitsig, sig_cb, SIGINT); |
871 | ev_signal_start (loop, &exitsig); |
912 | ev_signal_start (loop, &exitsig); |
872 | evf_unref (loop); |
913 | ev_unref (loop); |
873 | |
914 | |
874 | Example: For some weird reason, unregister the above signal handler again. |
915 | Example: For some weird reason, unregister the above signal handler again. |
875 | |
916 | |
876 | ev_ref (loop); |
917 | ev_ref (loop); |
877 | ev_signal_stop (loop, &exitsig); |
918 | ev_signal_stop (loop, &exitsig); |
… | |
… | |
1575 | In general you can register as many read and/or write event watchers per |
1616 | In general you can register as many read and/or write event watchers per |
1576 | fd as you want (as long as you don't confuse yourself). Setting all file |
1617 | fd as you want (as long as you don't confuse yourself). Setting all file |
1577 | descriptors to non-blocking mode is also usually a good idea (but not |
1618 | descriptors to non-blocking mode is also usually a good idea (but not |
1578 | required if you know what you are doing). |
1619 | required if you know what you are doing). |
1579 | |
1620 | |
1580 | If you cannot use non-blocking mode, then force the use of a |
|
|
1581 | known-to-be-good backend (at the time of this writing, this includes only |
|
|
1582 | C<EVBACKEND_SELECT> and C<EVBACKEND_POLL>). The same applies to file |
|
|
1583 | descriptors for which non-blocking operation makes no sense (such as |
|
|
1584 | files) - libev doesn't guarantee any specific behaviour in that case. |
|
|
1585 | |
|
|
1586 | Another thing you have to watch out for is that it is quite easy to |
1621 | Another thing you have to watch out for is that it is quite easy to |
1587 | receive "spurious" readiness notifications, that is your callback might |
1622 | receive "spurious" readiness notifications, that is, your callback might |
1588 | be called with C<EV_READ> but a subsequent C<read>(2) will actually block |
1623 | be called with C<EV_READ> but a subsequent C<read>(2) will actually block |
1589 | because there is no data. Not only are some backends known to create a |
1624 | because there is no data. It is very easy to get into this situation even |
1590 | lot of those (for example Solaris ports), it is very easy to get into |
1625 | with a relatively standard program structure. Thus it is best to always |
1591 | this situation even with a relatively standard program structure. Thus |
1626 | use non-blocking I/O: An extra C<read>(2) returning C<EAGAIN> is far |
1592 | it is best to always use non-blocking I/O: An extra C<read>(2) returning |
|
|
1593 | C<EAGAIN> is far preferable to a program hanging until some data arrives. |
1627 | preferable to a program hanging until some data arrives. |
1594 | |
1628 | |
1595 | If you cannot run the fd in non-blocking mode (for example you should |
1629 | If you cannot run the fd in non-blocking mode (for example you should |
1596 | not play around with an Xlib connection), then you have to separately |
1630 | not play around with an Xlib connection), then you have to separately |
1597 | re-test whether a file descriptor is really ready with a known-to-be good |
1631 | re-test whether a file descriptor is really ready with a known-to-be good |
1598 | interface such as poll (fortunately in our Xlib example, Xlib already |
1632 | interface such as poll (fortunately in the case of Xlib, it already does |
1599 | does this on its own, so its quite safe to use). Some people additionally |
1633 | this on its own, so its quite safe to use). Some people additionally |
1600 | use C<SIGALRM> and an interval timer, just to be sure you won't block |
1634 | use C<SIGALRM> and an interval timer, just to be sure you won't block |
1601 | indefinitely. |
1635 | indefinitely. |
1602 | |
1636 | |
1603 | But really, best use non-blocking mode. |
1637 | But really, best use non-blocking mode. |
1604 | |
1638 | |
… | |
… | |
1632 | |
1666 | |
1633 | There is no workaround possible except not registering events |
1667 | There is no workaround possible except not registering events |
1634 | for potentially C<dup ()>'ed file descriptors, or to resort to |
1668 | for potentially C<dup ()>'ed file descriptors, or to resort to |
1635 | C<EVBACKEND_SELECT> or C<EVBACKEND_POLL>. |
1669 | C<EVBACKEND_SELECT> or C<EVBACKEND_POLL>. |
1636 | |
1670 | |
|
|
1671 | =head3 The special problem of files |
|
|
1672 | |
|
|
1673 | Many people try to use C<select> (or libev) on file descriptors |
|
|
1674 | representing files, and expect it to become ready when their program |
|
|
1675 | doesn't block on disk accesses (which can take a long time on their own). |
|
|
1676 | |
|
|
1677 | However, this cannot ever work in the "expected" way - you get a readiness |
|
|
1678 | notification as soon as the kernel knows whether and how much data is |
|
|
1679 | there, and in the case of open files, that's always the case, so you |
|
|
1680 | always get a readiness notification instantly, and your read (or possibly |
|
|
1681 | write) will still block on the disk I/O. |
|
|
1682 | |
|
|
1683 | 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 |
|
|
1685 | on it's 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 |
|
|
1687 | wish to read - you would first have to request some data. |
|
|
1688 | |
|
|
1689 | Since files are typically not-so-well supported by advanced notification |
|
|
1690 | 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 |
|
|
1692 | convenience: sometimes you want to watch STDIN or STDOUT, which is |
|
|
1693 | usually a tty, often a pipe, but also sometimes files or special devices |
|
|
1694 | (for example, C<epoll> on Linux works with F</dev/random> but not with |
|
|
1695 | F</dev/urandom>), and even though the file might better be served with |
|
|
1696 | asynchronous I/O instead of with non-blocking I/O, it is still useful when |
|
|
1697 | it "just works" instead of freezing. |
|
|
1698 | |
|
|
1699 | So avoid file descriptors pointing to files when you know it (e.g. use |
|
|
1700 | libeio), but use them when it is convenient, e.g. for STDIN/STDOUT, or |
|
|
1701 | when you rarely read from a file instead of from a socket, and want to |
|
|
1702 | reuse the same code path. |
|
|
1703 | |
1637 | =head3 The special problem of fork |
1704 | =head3 The special problem of fork |
1638 | |
1705 | |
1639 | Some backends (epoll, kqueue) do not support C<fork ()> at all or exhibit |
1706 | Some backends (epoll, kqueue) do not support C<fork ()> at all or exhibit |
1640 | useless behaviour. Libev fully supports fork, but needs to be told about |
1707 | useless behaviour. Libev fully supports fork, but needs to be told about |
1641 | it in the child. |
1708 | it in the child if you want to continue to use it in the child. |
1642 | |
1709 | |
1643 | To support fork in your programs, you either have to call |
1710 | To support fork in your child processes, you have to call C<ev_loop_fork |
1644 | C<ev_default_fork ()> or C<ev_loop_fork ()> after a fork in the child, |
1711 | ()> after a fork in the child, enable C<EVFLAG_FORKCHECK>, or resort to |
1645 | enable C<EVFLAG_FORKCHECK>, or resort to C<EVBACKEND_SELECT> or |
1712 | C<EVBACKEND_SELECT> or C<EVBACKEND_POLL>. |
1646 | C<EVBACKEND_POLL>. |
|
|
1647 | |
1713 | |
1648 | =head3 The special problem of SIGPIPE |
1714 | =head3 The special problem of SIGPIPE |
1649 | |
1715 | |
1650 | While not really specific to libev, it is easy to forget about C<SIGPIPE>: |
1716 | While not really specific to libev, it is easy to forget about C<SIGPIPE>: |
1651 | when writing to a pipe whose other end has been closed, your program gets |
1717 | when writing to a pipe whose other end has been closed, your program gets |
… | |
… | |
2319 | I<has> to modify the signal mask, at least temporarily. |
2385 | I<has> to modify the signal mask, at least temporarily. |
2320 | |
2386 | |
2321 | So I can't stress this enough: I<If you do not reset your signal mask when |
2387 | So I can't stress this enough: I<If you do not reset your signal mask when |
2322 | you expect it to be empty, you have a race condition in your code>. This |
2388 | you expect it to be empty, you have a race condition in your code>. This |
2323 | is not a libev-specific thing, this is true for most event libraries. |
2389 | is not a libev-specific thing, this is true for most event libraries. |
|
|
2390 | |
|
|
2391 | =head3 The special problem of threads signal handling |
|
|
2392 | |
|
|
2393 | POSIX threads has problematic signal handling semantics, specifically, |
|
|
2394 | a lot of functionality (sigfd, sigwait etc.) only really works if all |
|
|
2395 | threads in a process block signals, which is hard to achieve. |
|
|
2396 | |
|
|
2397 | When you want to use sigwait (or mix libev signal handling with your own |
|
|
2398 | for the same signals), you can tackle this problem by globally blocking |
|
|
2399 | all signals before creating any threads (or creating them with a fully set |
|
|
2400 | sigprocmask) and also specifying the C<EVFLAG_NOSIGMASK> when creating |
|
|
2401 | loops. Then designate one thread as "signal receiver thread" which handles |
|
|
2402 | these signals. You can pass on any signals that libev might be interested |
|
|
2403 | in by calling C<ev_feed_signal>. |
2324 | |
2404 | |
2325 | =head3 Watcher-Specific Functions and Data Members |
2405 | =head3 Watcher-Specific Functions and Data Members |
2326 | |
2406 | |
2327 | =over 4 |
2407 | =over 4 |
2328 | |
2408 | |
… | |
… | |
3175 | it by calling C<ev_async_send>, which is thread- and signal safe. |
3255 | it by calling C<ev_async_send>, which is thread- and signal safe. |
3176 | |
3256 | |
3177 | This functionality is very similar to C<ev_signal> watchers, as signals, |
3257 | This functionality is very similar to C<ev_signal> watchers, as signals, |
3178 | too, are asynchronous in nature, and signals, too, will be compressed |
3258 | too, are asynchronous in nature, and signals, too, will be compressed |
3179 | (i.e. the number of callback invocations may be less than the number of |
3259 | (i.e. the number of callback invocations may be less than the number of |
3180 | C<ev_async_sent> calls). |
3260 | C<ev_async_sent> calls). In fact, you could use signal watchers as a kind |
|
|
3261 | of "global async watchers" by using a watcher on an otherwise unused |
|
|
3262 | signal, and C<ev_feed_signal> to signal this watcher from another thread, |
|
|
3263 | even without knowing which loop owns the signal. |
3181 | |
3264 | |
3182 | Unlike C<ev_signal> watchers, C<ev_async> works with any event loop, not |
3265 | Unlike C<ev_signal> watchers, C<ev_async> works with any event loop, not |
3183 | just the default loop. |
3266 | just the default loop. |
3184 | |
3267 | |
3185 | =head3 Queueing |
3268 | =head3 Queueing |
… | |
… | |
3361 | Feed an event on the given fd, as if a file descriptor backend detected |
3444 | Feed an event on the given fd, as if a file descriptor backend detected |
3362 | the given events it. |
3445 | the given events it. |
3363 | |
3446 | |
3364 | =item ev_feed_signal_event (loop, int signum) |
3447 | =item ev_feed_signal_event (loop, int signum) |
3365 | |
3448 | |
3366 | Feed an event as if the given signal occurred (C<loop> must be the default |
3449 | Feed an event as if the given signal occurred. See also C<ev_feed_signal>, |
3367 | loop!). |
3450 | which is async-safe. |
3368 | |
3451 | |
3369 | =back |
3452 | =back |
|
|
3453 | |
|
|
3454 | |
|
|
3455 | =head1 COMMON OR USEFUL IDIOMS (OR BOTH) |
|
|
3456 | |
|
|
3457 | This section explains some common idioms that are not immediately |
|
|
3458 | obvious. Note that examples are sprinkled over the whole manual, and this |
|
|
3459 | section only contains stuff that wouldn't fit anywhere else. |
|
|
3460 | |
|
|
3461 | =head2 MODEL/NESTED EVENT LOOP INVOCATIONS AND EXIT CONDITIONS |
|
|
3462 | |
|
|
3463 | Often (especially in GUI toolkits) there are places where you have |
|
|
3464 | I<modal> interaction, which is most easily implemented by recursively |
|
|
3465 | invoking C<ev_run>. |
|
|
3466 | |
|
|
3467 | This brings the problem of exiting - a callback might want to finish the |
|
|
3468 | main C<ev_run> call, but not the nested one (e.g. user clicked "Quit", but |
|
|
3469 | a modal "Are you sure?" dialog is still waiting), or just the nested one |
|
|
3470 | and not the main one (e.g. user clocked "Ok" in a modal dialog), or some |
|
|
3471 | other combination: In these cases, C<ev_break> will not work alone. |
|
|
3472 | |
|
|
3473 | The solution is to maintain "break this loop" variable for each C<ev_run> |
|
|
3474 | invocation, and use a loop around C<ev_run> until the condition is |
|
|
3475 | triggered, using C<EVRUN_ONCE>: |
|
|
3476 | |
|
|
3477 | // main loop |
|
|
3478 | int exit_main_loop = 0; |
|
|
3479 | |
|
|
3480 | while (!exit_main_loop) |
|
|
3481 | ev_run (EV_DEFAULT_ EVRUN_ONCE); |
|
|
3482 | |
|
|
3483 | // in a model watcher |
|
|
3484 | int exit_nested_loop = 0; |
|
|
3485 | |
|
|
3486 | while (!exit_nested_loop) |
|
|
3487 | ev_run (EV_A_ EVRUN_ONCE); |
|
|
3488 | |
|
|
3489 | To exit from any of these loops, just set the corresponding exit variable: |
|
|
3490 | |
|
|
3491 | // exit modal loop |
|
|
3492 | exit_nested_loop = 1; |
|
|
3493 | |
|
|
3494 | // exit main program, after modal loop is finished |
|
|
3495 | exit_main_loop = 1; |
|
|
3496 | |
|
|
3497 | // exit both |
|
|
3498 | exit_main_loop = exit_nested_loop = 1; |
|
|
3499 | |
|
|
3500 | =head2 THREAD LOCKING EXAMPLE |
|
|
3501 | |
|
|
3502 | 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 |
|
|
3504 | created/added/removed. |
|
|
3505 | |
|
|
3506 | 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 |
|
|
3508 | languages). |
|
|
3509 | |
|
|
3510 | The example uses a pthread mutex to protect the loop data, a condition |
|
|
3511 | variable to wait for callback invocations, an async watcher to notify the |
|
|
3512 | event loop thread and an unspecified mechanism to wake up the main thread. |
|
|
3513 | |
|
|
3514 | First, you need to associate some data with the event loop: |
|
|
3515 | |
|
|
3516 | typedef struct { |
|
|
3517 | mutex_t lock; /* global loop lock */ |
|
|
3518 | ev_async async_w; |
|
|
3519 | thread_t tid; |
|
|
3520 | cond_t invoke_cv; |
|
|
3521 | } userdata; |
|
|
3522 | |
|
|
3523 | void prepare_loop (EV_P) |
|
|
3524 | { |
|
|
3525 | // for simplicity, we use a static userdata struct. |
|
|
3526 | static userdata u; |
|
|
3527 | |
|
|
3528 | ev_async_init (&u->async_w, async_cb); |
|
|
3529 | ev_async_start (EV_A_ &u->async_w); |
|
|
3530 | |
|
|
3531 | pthread_mutex_init (&u->lock, 0); |
|
|
3532 | pthread_cond_init (&u->invoke_cv, 0); |
|
|
3533 | |
|
|
3534 | // now associate this with the loop |
|
|
3535 | ev_set_userdata (EV_A_ u); |
|
|
3536 | ev_set_invoke_pending_cb (EV_A_ l_invoke); |
|
|
3537 | ev_set_loop_release_cb (EV_A_ l_release, l_acquire); |
|
|
3538 | |
|
|
3539 | // then create the thread running ev_loop |
|
|
3540 | pthread_create (&u->tid, 0, l_run, EV_A); |
|
|
3541 | } |
|
|
3542 | |
|
|
3543 | The callback for the C<ev_async> watcher does nothing: the watcher is used |
|
|
3544 | solely to wake up the event loop so it takes notice of any new watchers |
|
|
3545 | that might have been added: |
|
|
3546 | |
|
|
3547 | static void |
|
|
3548 | async_cb (EV_P_ ev_async *w, int revents) |
|
|
3549 | { |
|
|
3550 | // just used for the side effects |
|
|
3551 | } |
|
|
3552 | |
|
|
3553 | The C<l_release> and C<l_acquire> callbacks simply unlock/lock the mutex |
|
|
3554 | protecting the loop data, respectively. |
|
|
3555 | |
|
|
3556 | static void |
|
|
3557 | l_release (EV_P) |
|
|
3558 | { |
|
|
3559 | userdata *u = ev_userdata (EV_A); |
|
|
3560 | pthread_mutex_unlock (&u->lock); |
|
|
3561 | } |
|
|
3562 | |
|
|
3563 | static void |
|
|
3564 | l_acquire (EV_P) |
|
|
3565 | { |
|
|
3566 | userdata *u = ev_userdata (EV_A); |
|
|
3567 | pthread_mutex_lock (&u->lock); |
|
|
3568 | } |
|
|
3569 | |
|
|
3570 | The event loop thread first acquires the mutex, and then jumps straight |
|
|
3571 | into C<ev_run>: |
|
|
3572 | |
|
|
3573 | void * |
|
|
3574 | l_run (void *thr_arg) |
|
|
3575 | { |
|
|
3576 | struct ev_loop *loop = (struct ev_loop *)thr_arg; |
|
|
3577 | |
|
|
3578 | l_acquire (EV_A); |
|
|
3579 | pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0); |
|
|
3580 | ev_run (EV_A_ 0); |
|
|
3581 | l_release (EV_A); |
|
|
3582 | |
|
|
3583 | return 0; |
|
|
3584 | } |
|
|
3585 | |
|
|
3586 | Instead of invoking all pending watchers, the C<l_invoke> callback will |
|
|
3587 | signal the main thread via some unspecified mechanism (signals? pipe |
|
|
3588 | writes? C<Async::Interrupt>?) and then waits until all pending watchers |
|
|
3589 | have been called (in a while loop because a) spurious wakeups are possible |
|
|
3590 | and b) skipping inter-thread-communication when there are no pending |
|
|
3591 | watchers is very beneficial): |
|
|
3592 | |
|
|
3593 | static void |
|
|
3594 | l_invoke (EV_P) |
|
|
3595 | { |
|
|
3596 | userdata *u = ev_userdata (EV_A); |
|
|
3597 | |
|
|
3598 | while (ev_pending_count (EV_A)) |
|
|
3599 | { |
|
|
3600 | wake_up_other_thread_in_some_magic_or_not_so_magic_way (); |
|
|
3601 | pthread_cond_wait (&u->invoke_cv, &u->lock); |
|
|
3602 | } |
|
|
3603 | } |
|
|
3604 | |
|
|
3605 | Now, whenever the main thread gets told to invoke pending watchers, it |
|
|
3606 | will grab the lock, call C<ev_invoke_pending> and then signal the loop |
|
|
3607 | thread to continue: |
|
|
3608 | |
|
|
3609 | static void |
|
|
3610 | real_invoke_pending (EV_P) |
|
|
3611 | { |
|
|
3612 | userdata *u = ev_userdata (EV_A); |
|
|
3613 | |
|
|
3614 | pthread_mutex_lock (&u->lock); |
|
|
3615 | ev_invoke_pending (EV_A); |
|
|
3616 | pthread_cond_signal (&u->invoke_cv); |
|
|
3617 | pthread_mutex_unlock (&u->lock); |
|
|
3618 | } |
|
|
3619 | |
|
|
3620 | Whenever you want to start/stop a watcher or do other modifications to an |
|
|
3621 | event loop, you will now have to lock: |
|
|
3622 | |
|
|
3623 | ev_timer timeout_watcher; |
|
|
3624 | userdata *u = ev_userdata (EV_A); |
|
|
3625 | |
|
|
3626 | ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); |
|
|
3627 | |
|
|
3628 | pthread_mutex_lock (&u->lock); |
|
|
3629 | ev_timer_start (EV_A_ &timeout_watcher); |
|
|
3630 | ev_async_send (EV_A_ &u->async_w); |
|
|
3631 | pthread_mutex_unlock (&u->lock); |
|
|
3632 | |
|
|
3633 | 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 |
|
|
3635 | about the newly added timer. By waking up the loop it will pick up any new |
|
|
3636 | watchers in the next event loop iteration. |
3370 | |
3637 | |
3371 | |
3638 | |
3372 | =head1 LIBEVENT EMULATION |
3639 | =head1 LIBEVENT EMULATION |
3373 | |
3640 | |
3374 | Libev offers a compatibility emulation layer for libevent. It cannot |
3641 | Libev offers a compatibility emulation layer for libevent. It cannot |
3375 | emulate the internals of libevent, so here are some usage hints: |
3642 | emulate the internals of libevent, so here are some usage hints: |
3376 | |
3643 | |
3377 | =over 4 |
3644 | =over 4 |
|
|
3645 | |
|
|
3646 | =item * Only the libevent-1.4.1-beta API is being emulated. |
|
|
3647 | |
|
|
3648 | This was the newest libevent version available when libev was implemented, |
|
|
3649 | and is still mostly unchanged in 2010. |
3378 | |
3650 | |
3379 | =item * Use it by including <event.h>, as usual. |
3651 | =item * Use it by including <event.h>, as usual. |
3380 | |
3652 | |
3381 | =item * The following members are fully supported: ev_base, ev_callback, |
3653 | =item * The following members are fully supported: ev_base, ev_callback, |
3382 | ev_arg, ev_fd, ev_res, ev_events. |
3654 | ev_arg, ev_fd, ev_res, ev_events. |
… | |
… | |
3417 | Care has been taken to keep the overhead low. The only data member the C++ |
3689 | Care has been taken to keep the overhead low. The only data member the C++ |
3418 | classes add (compared to plain C-style watchers) is the event loop pointer |
3690 | classes add (compared to plain C-style watchers) is the event loop pointer |
3419 | that the watcher is associated with (or no additional members at all if |
3691 | that the watcher is associated with (or no additional members at all if |
3420 | you disable C<EV_MULTIPLICITY> when embedding libev). |
3692 | you disable C<EV_MULTIPLICITY> when embedding libev). |
3421 | |
3693 | |
3422 | Currently, functions, and static and non-static member functions can be |
3694 | Currently, functions, static and non-static member functions and classes |
3423 | used as callbacks. Other types should be easy to add as long as they only |
3695 | with C<operator ()> can be used as callbacks. Other types should be easy |
3424 | need one additional pointer for context. If you need support for other |
3696 | to add as long as they only need one additional pointer for context. If |
3425 | types of functors please contact the author (preferably after implementing |
3697 | you need support for other types of functors please contact the author |
3426 | it). |
3698 | (preferably after implementing it). |
3427 | |
3699 | |
3428 | Here is a list of things available in the C<ev> namespace: |
3700 | Here is a list of things available in the C<ev> namespace: |
3429 | |
3701 | |
3430 | =over 4 |
3702 | =over 4 |
3431 | |
3703 | |
… | |
… | |
4299 | And a F<ev_cpp.C> implementation file that contains libev proper and is compiled: |
4571 | And a F<ev_cpp.C> implementation file that contains libev proper and is compiled: |
4300 | |
4572 | |
4301 | #include "ev_cpp.h" |
4573 | #include "ev_cpp.h" |
4302 | #include "ev.c" |
4574 | #include "ev.c" |
4303 | |
4575 | |
4304 | =head1 INTERACTION WITH OTHER PROGRAMS OR LIBRARIES |
4576 | =head1 INTERACTION WITH OTHER PROGRAMS, LIBRARIES OR THE ENVIRONMENT |
4305 | |
4577 | |
4306 | =head2 THREADS AND COROUTINES |
4578 | =head2 THREADS AND COROUTINES |
4307 | |
4579 | |
4308 | =head3 THREADS |
4580 | =head3 THREADS |
4309 | |
4581 | |
… | |
… | |
4360 | default loop and triggering an C<ev_async> watcher from the default loop |
4632 | default loop and triggering an C<ev_async> watcher from the default loop |
4361 | watcher callback into the event loop interested in the signal. |
4633 | watcher callback into the event loop interested in the signal. |
4362 | |
4634 | |
4363 | =back |
4635 | =back |
4364 | |
4636 | |
4365 | =head4 THREAD LOCKING EXAMPLE |
4637 | See also L<THREAD LOCKING EXAMPLE>. |
4366 | |
|
|
4367 | Here is a fictitious example of how to run an event loop in a different |
|
|
4368 | thread than where callbacks are being invoked and watchers are |
|
|
4369 | created/added/removed. |
|
|
4370 | |
|
|
4371 | For a real-world example, see the C<EV::Loop::Async> perl module, |
|
|
4372 | which uses exactly this technique (which is suited for many high-level |
|
|
4373 | languages). |
|
|
4374 | |
|
|
4375 | The example uses a pthread mutex to protect the loop data, a condition |
|
|
4376 | variable to wait for callback invocations, an async watcher to notify the |
|
|
4377 | event loop thread and an unspecified mechanism to wake up the main thread. |
|
|
4378 | |
|
|
4379 | First, you need to associate some data with the event loop: |
|
|
4380 | |
|
|
4381 | typedef struct { |
|
|
4382 | mutex_t lock; /* global loop lock */ |
|
|
4383 | ev_async async_w; |
|
|
4384 | thread_t tid; |
|
|
4385 | cond_t invoke_cv; |
|
|
4386 | } userdata; |
|
|
4387 | |
|
|
4388 | void prepare_loop (EV_P) |
|
|
4389 | { |
|
|
4390 | // for simplicity, we use a static userdata struct. |
|
|
4391 | static userdata u; |
|
|
4392 | |
|
|
4393 | ev_async_init (&u->async_w, async_cb); |
|
|
4394 | ev_async_start (EV_A_ &u->async_w); |
|
|
4395 | |
|
|
4396 | pthread_mutex_init (&u->lock, 0); |
|
|
4397 | pthread_cond_init (&u->invoke_cv, 0); |
|
|
4398 | |
|
|
4399 | // now associate this with the loop |
|
|
4400 | ev_set_userdata (EV_A_ u); |
|
|
4401 | ev_set_invoke_pending_cb (EV_A_ l_invoke); |
|
|
4402 | ev_set_loop_release_cb (EV_A_ l_release, l_acquire); |
|
|
4403 | |
|
|
4404 | // then create the thread running ev_loop |
|
|
4405 | pthread_create (&u->tid, 0, l_run, EV_A); |
|
|
4406 | } |
|
|
4407 | |
|
|
4408 | The callback for the C<ev_async> watcher does nothing: the watcher is used |
|
|
4409 | solely to wake up the event loop so it takes notice of any new watchers |
|
|
4410 | that might have been added: |
|
|
4411 | |
|
|
4412 | static void |
|
|
4413 | async_cb (EV_P_ ev_async *w, int revents) |
|
|
4414 | { |
|
|
4415 | // just used for the side effects |
|
|
4416 | } |
|
|
4417 | |
|
|
4418 | The C<l_release> and C<l_acquire> callbacks simply unlock/lock the mutex |
|
|
4419 | protecting the loop data, respectively. |
|
|
4420 | |
|
|
4421 | static void |
|
|
4422 | l_release (EV_P) |
|
|
4423 | { |
|
|
4424 | userdata *u = ev_userdata (EV_A); |
|
|
4425 | pthread_mutex_unlock (&u->lock); |
|
|
4426 | } |
|
|
4427 | |
|
|
4428 | static void |
|
|
4429 | l_acquire (EV_P) |
|
|
4430 | { |
|
|
4431 | userdata *u = ev_userdata (EV_A); |
|
|
4432 | pthread_mutex_lock (&u->lock); |
|
|
4433 | } |
|
|
4434 | |
|
|
4435 | The event loop thread first acquires the mutex, and then jumps straight |
|
|
4436 | into C<ev_run>: |
|
|
4437 | |
|
|
4438 | void * |
|
|
4439 | l_run (void *thr_arg) |
|
|
4440 | { |
|
|
4441 | struct ev_loop *loop = (struct ev_loop *)thr_arg; |
|
|
4442 | |
|
|
4443 | l_acquire (EV_A); |
|
|
4444 | pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0); |
|
|
4445 | ev_run (EV_A_ 0); |
|
|
4446 | l_release (EV_A); |
|
|
4447 | |
|
|
4448 | return 0; |
|
|
4449 | } |
|
|
4450 | |
|
|
4451 | Instead of invoking all pending watchers, the C<l_invoke> callback will |
|
|
4452 | signal the main thread via some unspecified mechanism (signals? pipe |
|
|
4453 | writes? C<Async::Interrupt>?) and then waits until all pending watchers |
|
|
4454 | have been called (in a while loop because a) spurious wakeups are possible |
|
|
4455 | and b) skipping inter-thread-communication when there are no pending |
|
|
4456 | watchers is very beneficial): |
|
|
4457 | |
|
|
4458 | static void |
|
|
4459 | l_invoke (EV_P) |
|
|
4460 | { |
|
|
4461 | userdata *u = ev_userdata (EV_A); |
|
|
4462 | |
|
|
4463 | while (ev_pending_count (EV_A)) |
|
|
4464 | { |
|
|
4465 | wake_up_other_thread_in_some_magic_or_not_so_magic_way (); |
|
|
4466 | pthread_cond_wait (&u->invoke_cv, &u->lock); |
|
|
4467 | } |
|
|
4468 | } |
|
|
4469 | |
|
|
4470 | Now, whenever the main thread gets told to invoke pending watchers, it |
|
|
4471 | will grab the lock, call C<ev_invoke_pending> and then signal the loop |
|
|
4472 | thread to continue: |
|
|
4473 | |
|
|
4474 | static void |
|
|
4475 | real_invoke_pending (EV_P) |
|
|
4476 | { |
|
|
4477 | userdata *u = ev_userdata (EV_A); |
|
|
4478 | |
|
|
4479 | pthread_mutex_lock (&u->lock); |
|
|
4480 | ev_invoke_pending (EV_A); |
|
|
4481 | pthread_cond_signal (&u->invoke_cv); |
|
|
4482 | pthread_mutex_unlock (&u->lock); |
|
|
4483 | } |
|
|
4484 | |
|
|
4485 | Whenever you want to start/stop a watcher or do other modifications to an |
|
|
4486 | event loop, you will now have to lock: |
|
|
4487 | |
|
|
4488 | ev_timer timeout_watcher; |
|
|
4489 | userdata *u = ev_userdata (EV_A); |
|
|
4490 | |
|
|
4491 | ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); |
|
|
4492 | |
|
|
4493 | pthread_mutex_lock (&u->lock); |
|
|
4494 | ev_timer_start (EV_A_ &timeout_watcher); |
|
|
4495 | ev_async_send (EV_A_ &u->async_w); |
|
|
4496 | pthread_mutex_unlock (&u->lock); |
|
|
4497 | |
|
|
4498 | Note that sending the C<ev_async> watcher is required because otherwise |
|
|
4499 | an event loop currently blocking in the kernel will have no knowledge |
|
|
4500 | about the newly added timer. By waking up the loop it will pick up any new |
|
|
4501 | watchers in the next event loop iteration. |
|
|
4502 | |
4638 | |
4503 | =head3 COROUTINES |
4639 | =head3 COROUTINES |
4504 | |
4640 | |
4505 | Libev is very accommodating to coroutines ("cooperative threads"): |
4641 | Libev is very accommodating to coroutines ("cooperative threads"): |
4506 | libev fully supports nesting calls to its functions from different |
4642 | libev fully supports nesting calls to its functions from different |