1 | /* |
1 | /* |
2 | * libev native API header |
2 | * libev native API header |
3 | * |
3 | * |
4 | * Copyright (c) 2007-2018 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007-2020 Marc Alexander Lehmann <libev@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * tion, are permitted provided that the following conditions are met: |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * |
9 | * |
… | |
… | |
49 | # endif |
49 | # endif |
50 | #else |
50 | #else |
51 | # define EV_CPP(x) |
51 | # define EV_CPP(x) |
52 | # define EV_NOEXCEPT |
52 | # define EV_NOEXCEPT |
53 | #endif |
53 | #endif |
|
|
54 | #define EV_THROW EV_NOEXCEPT /* pre-4.25, do not use in new code */ |
54 | |
55 | |
55 | EV_CPP(extern "C" {) |
56 | EV_CPP(extern "C" {) |
56 | |
57 | |
57 | /*****************************************************************************/ |
58 | /*****************************************************************************/ |
58 | |
59 | |
… | |
… | |
148 | # define EV_SIGNAL_ENABLE 1 |
149 | # define EV_SIGNAL_ENABLE 1 |
149 | #endif |
150 | #endif |
150 | |
151 | |
151 | /*****************************************************************************/ |
152 | /*****************************************************************************/ |
152 | |
153 | |
153 | typedef double ev_tstamp; |
154 | #ifndef EV_TSTAMP_T |
|
|
155 | # define EV_TSTAMP_T double |
|
|
156 | #endif |
|
|
157 | typedef EV_TSTAMP_T ev_tstamp; |
154 | |
158 | |
155 | #include <string.h> /* for memmove */ |
159 | #include <string.h> /* for memmove */ |
156 | |
160 | |
157 | #ifndef EV_ATOMIC_T |
161 | #ifndef EV_ATOMIC_T |
158 | # include <signal.h> |
162 | # include <signal.h> |
… | |
… | |
209 | #endif |
213 | #endif |
210 | |
214 | |
211 | /*****************************************************************************/ |
215 | /*****************************************************************************/ |
212 | |
216 | |
213 | #define EV_VERSION_MAJOR 4 |
217 | #define EV_VERSION_MAJOR 4 |
214 | #define EV_VERSION_MINOR 24 |
218 | #define EV_VERSION_MINOR 33 |
215 | |
219 | |
216 | /* eventmask, revents, events... */ |
220 | /* eventmask, revents, events... */ |
217 | enum { |
221 | enum { |
218 | EV_UNDEF = (int)0xFFFFFFFF, /* guaranteed to be invalid */ |
222 | EV_UNDEF = (int)0xFFFFFFFF, /* guaranteed to be invalid */ |
219 | EV_NONE = 0x00, /* no events */ |
223 | EV_NONE = 0x00, /* no events */ |
… | |
… | |
386 | |
390 | |
387 | int wd; /* wd for inotify, fd for kqueue */ |
391 | int wd; /* wd for inotify, fd for kqueue */ |
388 | } ev_stat; |
392 | } ev_stat; |
389 | #endif |
393 | #endif |
390 | |
394 | |
391 | #if EV_IDLE_ENABLE |
|
|
392 | /* invoked when the nothing else needs to be done, keeps the process from blocking */ |
395 | /* invoked when the nothing else needs to be done, keeps the process from blocking */ |
393 | /* revent EV_IDLE */ |
396 | /* revent EV_IDLE */ |
394 | typedef struct ev_idle |
397 | typedef struct ev_idle |
395 | { |
398 | { |
396 | EV_WATCHER (ev_idle) |
399 | EV_WATCHER (ev_idle) |
397 | } ev_idle; |
400 | } ev_idle; |
398 | #endif |
|
|
399 | |
401 | |
400 | /* invoked for each run of the mainloop, just before the blocking call */ |
402 | /* invoked for each run of the mainloop, just before the blocking call */ |
401 | /* you can still change events in any way you like */ |
403 | /* you can still change events in any way you like */ |
402 | /* revent EV_PREPARE */ |
404 | /* revent EV_PREPARE */ |
403 | typedef struct ev_prepare |
405 | typedef struct ev_prepare |
… | |
… | |
410 | typedef struct ev_check |
412 | typedef struct ev_check |
411 | { |
413 | { |
412 | EV_WATCHER (ev_check) |
414 | EV_WATCHER (ev_check) |
413 | } ev_check; |
415 | } ev_check; |
414 | |
416 | |
415 | #if EV_FORK_ENABLE |
|
|
416 | /* the callback gets invoked before check in the child process when a fork was detected */ |
417 | /* the callback gets invoked before check in the child process when a fork was detected */ |
417 | /* revent EV_FORK */ |
418 | /* revent EV_FORK */ |
418 | typedef struct ev_fork |
419 | typedef struct ev_fork |
419 | { |
420 | { |
420 | EV_WATCHER (ev_fork) |
421 | EV_WATCHER (ev_fork) |
421 | } ev_fork; |
422 | } ev_fork; |
422 | #endif |
|
|
423 | |
423 | |
424 | #if EV_CLEANUP_ENABLE |
|
|
425 | /* is invoked just before the loop gets destroyed */ |
424 | /* is invoked just before the loop gets destroyed */ |
426 | /* revent EV_CLEANUP */ |
425 | /* revent EV_CLEANUP */ |
427 | typedef struct ev_cleanup |
426 | typedef struct ev_cleanup |
428 | { |
427 | { |
429 | EV_WATCHER (ev_cleanup) |
428 | EV_WATCHER (ev_cleanup) |
430 | } ev_cleanup; |
429 | } ev_cleanup; |
431 | #endif |
|
|
432 | |
430 | |
433 | #if EV_EMBED_ENABLE |
431 | #if EV_EMBED_ENABLE |
434 | /* used to embed an event loop inside another */ |
432 | /* used to embed an event loop inside another */ |
435 | /* the callback gets invoked when the event loop has handled events, and can be 0 */ |
433 | /* the callback gets invoked when the event loop has handled events, and can be 0 */ |
436 | typedef struct ev_embed |
434 | typedef struct ev_embed |
437 | { |
435 | { |
438 | EV_WATCHER (ev_embed) |
436 | EV_WATCHER (ev_embed) |
439 | |
437 | |
440 | struct ev_loop *other; /* ro */ |
438 | struct ev_loop *other; /* ro */ |
|
|
439 | #undef EV_IO_ENABLE |
|
|
440 | #define EV_IO_ENABLE 1 |
441 | ev_io io; /* private */ |
441 | ev_io io; /* private */ |
|
|
442 | #undef EV_PREPARE_ENABLE |
|
|
443 | #define EV_PREPARE_ENABLE 1 |
442 | ev_prepare prepare; /* private */ |
444 | ev_prepare prepare; /* private */ |
443 | ev_check check; /* unused */ |
445 | ev_check check; /* unused */ |
444 | ev_timer timer; /* unused */ |
446 | ev_timer timer; /* unused */ |
445 | ev_periodic periodic; /* unused */ |
447 | ev_periodic periodic; /* unused */ |
446 | ev_idle idle; /* unused */ |
448 | ev_idle idle; /* unused */ |
447 | ev_fork fork; /* private */ |
449 | ev_fork fork; /* private */ |
448 | #if EV_CLEANUP_ENABLE |
|
|
449 | ev_cleanup cleanup; /* unused */ |
450 | ev_cleanup cleanup; /* unused */ |
450 | #endif |
|
|
451 | } ev_embed; |
451 | } ev_embed; |
452 | #endif |
452 | #endif |
453 | |
453 | |
454 | #if EV_ASYNC_ENABLE |
454 | #if EV_ASYNC_ENABLE |
455 | /* invoked when somebody calls ev_async_send on the watcher */ |
455 | /* invoked when somebody calls ev_async_send on the watcher */ |
… | |
… | |
498 | }; |
498 | }; |
499 | |
499 | |
500 | /* flag bits for ev_default_loop and ev_loop_new */ |
500 | /* flag bits for ev_default_loop and ev_loop_new */ |
501 | enum { |
501 | enum { |
502 | /* the default */ |
502 | /* the default */ |
503 | EVFLAG_AUTO = 0x00000000U, /* not quite a mask */ |
503 | EVFLAG_AUTO = 0x00000000U, /* not quite a mask */ |
504 | /* flag bits */ |
504 | /* flag bits */ |
505 | EVFLAG_NOENV = 0x01000000U, /* do NOT consult environment */ |
505 | EVFLAG_NOENV = 0x01000000U, /* do NOT consult environment */ |
506 | EVFLAG_FORKCHECK = 0x02000000U, /* check for a fork in each iteration */ |
506 | EVFLAG_FORKCHECK = 0x02000000U, /* check for a fork in each iteration */ |
507 | /* debugging/feature disable */ |
507 | /* debugging/feature disable */ |
508 | EVFLAG_NOINOTIFY = 0x00100000U, /* do not attempt to use inotify */ |
508 | EVFLAG_NOINOTIFY = 0x00100000U, /* do not attempt to use inotify */ |
509 | #if EV_COMPAT3 |
509 | #if EV_COMPAT3 |
510 | EVFLAG_NOSIGFD = 0, /* compatibility to pre-3.9 */ |
510 | EVFLAG_NOSIGFD = 0, /* compatibility to pre-3.9 */ |
511 | #endif |
511 | #endif |
512 | EVFLAG_SIGNALFD = 0x00200000U, /* attempt to use signalfd */ |
512 | EVFLAG_SIGNALFD = 0x00200000U, /* attempt to use signalfd */ |
513 | EVFLAG_NOSIGMASK = 0x00400000U /* avoid modifying the signal mask */ |
513 | EVFLAG_NOSIGMASK = 0x00400000U, /* avoid modifying the signal mask */ |
|
|
514 | EVFLAG_NOTIMERFD = 0x00800000U /* avoid creating a timerfd */ |
514 | }; |
515 | }; |
515 | |
516 | |
516 | /* method bits to be ored together */ |
517 | /* method bits to be ored together */ |
517 | enum { |
518 | enum { |
518 | EVBACKEND_SELECT = 0x00000001U, /* available just about anywhere */ |
519 | EVBACKEND_SELECT = 0x00000001U, /* available just about anywhere */ |
519 | EVBACKEND_POLL = 0x00000002U, /* !win, !aix, broken on osx */ |
520 | EVBACKEND_POLL = 0x00000002U, /* !win, !aix, broken on osx */ |
520 | EVBACKEND_EPOLL = 0x00000004U, /* linux */ |
521 | EVBACKEND_EPOLL = 0x00000004U, /* linux */ |
521 | EVBACKEND_KQUEUE = 0x00000008U, /* bsd, broken on osx */ |
522 | EVBACKEND_KQUEUE = 0x00000008U, /* bsd, broken on osx */ |
522 | EVBACKEND_DEVPOLL = 0x00000010U, /* solaris 8 */ /* NYI */ |
523 | EVBACKEND_DEVPOLL = 0x00000010U, /* solaris 8 */ /* NYI */ |
523 | EVBACKEND_PORT = 0x00000020U, /* solaris 10 */ |
524 | EVBACKEND_PORT = 0x00000020U, /* solaris 10 */ |
|
|
525 | EVBACKEND_LINUXAIO = 0x00000040U, /* linux AIO, 4.19+ */ |
|
|
526 | EVBACKEND_IOURING = 0x00000080U, /* linux io_uring, 5.1+ */ |
524 | EVBACKEND_ALL = 0x0000003FU, /* all known backends */ |
527 | EVBACKEND_ALL = 0x000000FFU, /* all known backends */ |
525 | EVBACKEND_MASK = 0x0000FFFFU /* all future backends */ |
528 | EVBACKEND_MASK = 0x0000FFFFU /* all future backends */ |
526 | }; |
529 | }; |
527 | |
530 | |
528 | #if EV_PROTOTYPES |
531 | #if EV_PROTOTYPES |
529 | EV_API_DECL int ev_version_major (void) EV_NOEXCEPT; |
532 | EV_API_DECL int ev_version_major (void) EV_NOEXCEPT; |
530 | EV_API_DECL int ev_version_minor (void) EV_NOEXCEPT; |
533 | EV_API_DECL int ev_version_minor (void) EV_NOEXCEPT; |
… | |
… | |
651 | * convenience function, wait for a single event, without registering an event watcher |
654 | * convenience function, wait for a single event, without registering an event watcher |
652 | * if timeout is < 0, do wait indefinitely |
655 | * if timeout is < 0, do wait indefinitely |
653 | */ |
656 | */ |
654 | EV_API_DECL void ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) EV_NOEXCEPT; |
657 | EV_API_DECL void ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) EV_NOEXCEPT; |
655 | |
658 | |
|
|
659 | EV_API_DECL void ev_invoke_pending (EV_P); /* invoke all pending watchers */ |
|
|
660 | |
656 | # if EV_FEATURE_API |
661 | # if EV_FEATURE_API |
657 | EV_API_DECL unsigned int ev_iteration (EV_P) EV_NOEXCEPT; /* number of loop iterations */ |
662 | EV_API_DECL unsigned int ev_iteration (EV_P) EV_NOEXCEPT; /* number of loop iterations */ |
658 | EV_API_DECL unsigned int ev_depth (EV_P) EV_NOEXCEPT; /* #ev_loop enters - #ev_loop leaves */ |
663 | EV_API_DECL unsigned int ev_depth (EV_P) EV_NOEXCEPT; /* #ev_loop enters - #ev_loop leaves */ |
659 | EV_API_DECL void ev_verify (EV_P) EV_NOEXCEPT; /* abort if loop data corrupted */ |
664 | EV_API_DECL void ev_verify (EV_P) EV_NOEXCEPT; /* abort if loop data corrupted */ |
660 | |
665 | |
… | |
… | |
668 | EV_API_DECL void ev_set_invoke_pending_cb (EV_P_ ev_loop_callback invoke_pending_cb) EV_NOEXCEPT; |
673 | EV_API_DECL void ev_set_invoke_pending_cb (EV_P_ ev_loop_callback invoke_pending_cb) EV_NOEXCEPT; |
669 | /* C++ doesn't allow the use of the ev_loop_callback typedef here, so we need to spell it out */ |
674 | /* C++ doesn't allow the use of the ev_loop_callback typedef here, so we need to spell it out */ |
670 | EV_API_DECL void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P) EV_NOEXCEPT, void (*acquire)(EV_P) EV_NOEXCEPT) EV_NOEXCEPT; |
675 | EV_API_DECL void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P) EV_NOEXCEPT, void (*acquire)(EV_P) EV_NOEXCEPT) EV_NOEXCEPT; |
671 | |
676 | |
672 | EV_API_DECL unsigned int ev_pending_count (EV_P) EV_NOEXCEPT; /* number of pending events, if any */ |
677 | EV_API_DECL unsigned int ev_pending_count (EV_P) EV_NOEXCEPT; /* number of pending events, if any */ |
673 | EV_API_DECL void ev_invoke_pending (EV_P); /* invoke all pending watchers */ |
|
|
674 | |
678 | |
675 | /* |
679 | /* |
676 | * stop/start the timer handling. |
680 | * stop/start the timer handling. |
677 | */ |
681 | */ |
678 | EV_API_DECL void ev_suspend (EV_P) EV_NOEXCEPT; |
682 | EV_API_DECL void ev_suspend (EV_P) EV_NOEXCEPT; |
… | |
… | |
688 | ((ev_watcher *)(void *)(ev))->pending = 0; \ |
692 | ((ev_watcher *)(void *)(ev))->pending = 0; \ |
689 | ev_set_priority ((ev), 0); \ |
693 | ev_set_priority ((ev), 0); \ |
690 | ev_set_cb ((ev), cb_); \ |
694 | ev_set_cb ((ev), cb_); \ |
691 | } while (0) |
695 | } while (0) |
692 | |
696 | |
|
|
697 | #define ev_io_modify(ev,events_) do { (ev)->events = (ev)->events & EV__IOFDSET | (events_); } while (0) |
693 | #define ev_io_set(ev,fd_,events_) do { (ev)->fd = (fd_); (ev)->events = (events_) | EV__IOFDSET; } while (0) |
698 | #define ev_io_set(ev,fd_,events_) do { (ev)->fd = (fd_); (ev)->events = (events_) | EV__IOFDSET; } while (0) |
694 | #define ev_timer_set(ev,after_,repeat_) do { ((ev_watcher_time *)(ev))->at = (after_); (ev)->repeat = (repeat_); } while (0) |
699 | #define ev_timer_set(ev,after_,repeat_) do { ((ev_watcher_time *)(ev))->at = (after_); (ev)->repeat = (repeat_); } while (0) |
695 | #define ev_periodic_set(ev,ofs_,ival_,rcb_) do { (ev)->offset = (ofs_); (ev)->interval = (ival_); (ev)->reschedule_cb = (rcb_); } while (0) |
700 | #define ev_periodic_set(ev,ofs_,ival_,rcb_) do { (ev)->offset = (ofs_); (ev)->interval = (ival_); (ev)->reschedule_cb = (rcb_); } while (0) |
696 | #define ev_signal_set(ev,signum_) do { (ev)->signum = (signum_); } while (0) |
701 | #define ev_signal_set(ev,signum_) do { (ev)->signum = (signum_); } while (0) |
697 | #define ev_child_set(ev,pid_,trace_) do { (ev)->pid = (pid_); (ev)->flags = !!(trace_); } while (0) |
702 | #define ev_child_set(ev,pid_,trace_) do { (ev)->pid = (pid_); (ev)->flags = !!(trace_); } while (0) |
… | |
… | |
733 | #endif |
738 | #endif |
734 | |
739 | |
735 | #define ev_periodic_at(ev) (+((ev_watcher_time *)(ev))->at) |
740 | #define ev_periodic_at(ev) (+((ev_watcher_time *)(ev))->at) |
736 | |
741 | |
737 | #ifndef ev_set_cb |
742 | #ifndef ev_set_cb |
|
|
743 | /* memmove is used here to avoid strict aliasing violations, and hopefully is optimized out by any reasonable compiler */ |
738 | # define ev_set_cb(ev,cb_) (ev_cb_ (ev) = (cb_), memmove (&((ev_watcher *)(ev))->cb, &ev_cb_ (ev), sizeof (ev_cb_ (ev)))) |
744 | # define ev_set_cb(ev,cb_) (ev_cb_ (ev) = (cb_), memmove (&((ev_watcher *)(ev))->cb, &ev_cb_ (ev), sizeof (ev_cb_ (ev)))) |
739 | #endif |
745 | #endif |
740 | |
746 | |
741 | /* stopping (enabling, adding) a watcher does nothing if it is already running */ |
747 | /* starting (enabling, adding) a watcher does nothing if it is already running */ |
742 | /* stopping (disabling, deleting) a watcher does nothing unless it's already running */ |
748 | /* stopping (disabling, deleting) a watcher does nothing unless it's already running */ |
743 | #if EV_PROTOTYPES |
749 | #if EV_PROTOTYPES |
744 | |
750 | |
745 | /* feeds an event into a watcher as if the event actually occurred */ |
751 | /* feeds an event into a watcher as if the event actually occurred */ |
746 | /* accepts any ev_watcher type */ |
752 | /* accepts any ev_watcher type */ |