1 | /* |
1 | /* |
2 | * libev native API header |
2 | * libev native API header |
3 | * |
3 | * |
4 | * Copyright (c) 2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007,2008,2009,2010,2011,2012,2015 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 | * |
… | |
… | |
40 | #ifndef EV_H_ |
40 | #ifndef EV_H_ |
41 | #define EV_H_ |
41 | #define EV_H_ |
42 | |
42 | |
43 | #ifdef __cplusplus |
43 | #ifdef __cplusplus |
44 | # define EV_CPP(x) x |
44 | # define EV_CPP(x) x |
|
|
45 | # if __cplusplus >= 201103L |
|
|
46 | # define EV_THROW noexcept |
|
|
47 | # else |
|
|
48 | # define EV_THROW throw () |
|
|
49 | # endif |
45 | #else |
50 | #else |
46 | # define EV_CPP(x) |
51 | # define EV_CPP(x) |
|
|
52 | # define EV_THROW |
47 | #endif |
53 | #endif |
48 | |
|
|
49 | #define EV_THROW EV_CPP(throw()) |
|
|
50 | |
54 | |
51 | EV_CPP(extern "C" {) |
55 | EV_CPP(extern "C" {) |
52 | |
56 | |
53 | /*****************************************************************************/ |
57 | /*****************************************************************************/ |
54 | |
58 | |
… | |
… | |
145 | #endif |
149 | #endif |
146 | |
150 | |
147 | /*****************************************************************************/ |
151 | /*****************************************************************************/ |
148 | |
152 | |
149 | typedef double ev_tstamp; |
153 | typedef double ev_tstamp; |
|
|
154 | |
|
|
155 | #include <string.h> /* for memmove */ |
150 | |
156 | |
151 | #ifndef EV_ATOMIC_T |
157 | #ifndef EV_ATOMIC_T |
152 | # include <signal.h> |
158 | # include <signal.h> |
153 | # define EV_ATOMIC_T sig_atomic_t volatile |
159 | # define EV_ATOMIC_T sig_atomic_t volatile |
154 | #endif |
160 | #endif |
… | |
… | |
203 | #endif |
209 | #endif |
204 | |
210 | |
205 | /*****************************************************************************/ |
211 | /*****************************************************************************/ |
206 | |
212 | |
207 | #define EV_VERSION_MAJOR 4 |
213 | #define EV_VERSION_MAJOR 4 |
208 | #define EV_VERSION_MINOR 11 |
214 | #define EV_VERSION_MINOR 22 |
209 | |
215 | |
210 | /* eventmask, revents, events... */ |
216 | /* eventmask, revents, events... */ |
211 | enum { |
217 | enum { |
212 | EV_UNDEF = 0xFFFFFFFF, /* guaranteed to be invalid */ |
218 | EV_UNDEF = (int)0xFFFFFFFF, /* guaranteed to be invalid */ |
213 | EV_NONE = 0x00, /* no events */ |
219 | EV_NONE = 0x00, /* no events */ |
214 | EV_READ = 0x01, /* ev_io detected read will not block */ |
220 | EV_READ = 0x01, /* ev_io detected read will not block */ |
215 | EV_WRITE = 0x02, /* ev_io detected write will not block */ |
221 | EV_WRITE = 0x02, /* ev_io detected write will not block */ |
216 | EV__IOFDSET = 0x80, /* internal use only */ |
222 | EV__IOFDSET = 0x80, /* internal use only */ |
217 | EV_IO = EV_READ, /* alias for type-detection */ |
223 | EV_IO = EV_READ, /* alias for type-detection */ |
218 | EV_TIMER = 0x00000100, /* timer timed out */ |
224 | EV_TIMER = 0x00000100, /* timer timed out */ |
219 | #if EV_COMPAT3 |
225 | #if EV_COMPAT3 |
220 | EV_TIMEOUT = EV_TIMER, /* pre 4.0 API compatibility */ |
226 | EV_TIMEOUT = EV_TIMER, /* pre 4.0 API compatibility */ |
221 | #endif |
227 | #endif |
222 | EV_PERIODIC = 0x00000200, /* periodic timer timed out */ |
228 | EV_PERIODIC = 0x00000200, /* periodic timer timed out */ |
223 | EV_SIGNAL = 0x00000400, /* signal was received */ |
229 | EV_SIGNAL = 0x00000400, /* signal was received */ |
224 | EV_CHILD = 0x00000800, /* child/pid had status change */ |
230 | EV_CHILD = 0x00000800, /* child/pid had status change */ |
225 | EV_STAT = 0x00001000, /* stat data changed */ |
231 | EV_STAT = 0x00001000, /* stat data changed */ |
226 | EV_IDLE = 0x00002000, /* event loop is idling */ |
232 | EV_IDLE = 0x00002000, /* event loop is idling */ |
227 | EV_PREPARE = 0x00004000, /* event loop about to poll */ |
233 | EV_PREPARE = 0x00004000, /* event loop about to poll */ |
228 | EV_CHECK = 0x00008000, /* event loop finished poll */ |
234 | EV_CHECK = 0x00008000, /* event loop finished poll */ |
229 | EV_EMBED = 0x00010000, /* embedded event loop needs sweep */ |
235 | EV_EMBED = 0x00010000, /* embedded event loop needs sweep */ |
230 | EV_FORK = 0x00020000, /* event loop resumed in child */ |
236 | EV_FORK = 0x00020000, /* event loop resumed in child */ |
231 | EV_CLEANUP = 0x00040000, /* event loop resumed in child */ |
237 | EV_CLEANUP = 0x00040000, /* event loop resumed in child */ |
232 | EV_ASYNC = 0x00080000, /* async intra-loop signal */ |
238 | EV_ASYNC = 0x00080000, /* async intra-loop signal */ |
233 | EV_CUSTOM = 0x01000000, /* for use by user code */ |
239 | EV_CUSTOM = 0x01000000, /* for use by user code */ |
234 | EV_ERROR = 0x80000000 /* sent when an error occurs */ |
240 | EV_ERROR = (int)0x80000000 /* sent when an error occurs */ |
235 | }; |
241 | }; |
236 | |
242 | |
237 | /* can be used to add custom fields to all watchers, while losing binary compatibility */ |
243 | /* can be used to add custom fields to all watchers, while losing binary compatibility */ |
238 | #ifndef EV_COMMON |
244 | #ifndef EV_COMMON |
239 | # define EV_COMMON void *data; |
245 | # define EV_COMMON void *data; |
… | |
… | |
331 | { |
337 | { |
332 | EV_WATCHER_TIME (ev_periodic) |
338 | EV_WATCHER_TIME (ev_periodic) |
333 | |
339 | |
334 | ev_tstamp offset; /* rw */ |
340 | ev_tstamp offset; /* rw */ |
335 | ev_tstamp interval; /* rw */ |
341 | ev_tstamp interval; /* rw */ |
336 | ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now); /* rw */ |
342 | ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) EV_THROW; /* rw */ |
337 | } ev_periodic; |
343 | } ev_periodic; |
338 | |
344 | |
339 | /* invoked when the given signal has been received */ |
345 | /* invoked when the given signal has been received */ |
340 | /* revent EV_SIGNAL */ |
346 | /* revent EV_SIGNAL */ |
341 | typedef struct ev_signal |
347 | typedef struct ev_signal |
… | |
… | |
507 | EVFLAG_NOSIGMASK = 0x00400000U /* avoid modifying the signal mask */ |
513 | EVFLAG_NOSIGMASK = 0x00400000U /* avoid modifying the signal mask */ |
508 | }; |
514 | }; |
509 | |
515 | |
510 | /* method bits to be ored together */ |
516 | /* method bits to be ored together */ |
511 | enum { |
517 | enum { |
512 | EVBACKEND_SELECT = 0x00000001U, /* about anywhere */ |
518 | EVBACKEND_SELECT = 0x00000001U, /* available just about anywhere */ |
513 | EVBACKEND_POLL = 0x00000002U, /* !win */ |
519 | EVBACKEND_POLL = 0x00000002U, /* !win, !aix, broken on osx */ |
514 | EVBACKEND_EPOLL = 0x00000004U, /* linux */ |
520 | EVBACKEND_EPOLL = 0x00000004U, /* linux */ |
515 | EVBACKEND_KQUEUE = 0x00000008U, /* bsd */ |
521 | EVBACKEND_KQUEUE = 0x00000008U, /* bsd, broken on osx */ |
516 | EVBACKEND_DEVPOLL = 0x00000010U, /* solaris 8 */ /* NYI */ |
522 | EVBACKEND_DEVPOLL = 0x00000010U, /* solaris 8 */ /* NYI */ |
517 | EVBACKEND_PORT = 0x00000020U, /* solaris 10 */ |
523 | EVBACKEND_PORT = 0x00000020U, /* solaris 10 */ |
518 | EVBACKEND_ALL = 0x0000003FU, /* all known backends */ |
524 | EVBACKEND_ALL = 0x0000003FU, /* all known backends */ |
519 | EVBACKEND_MASK = 0x0000FFFFU /* all future backends */ |
525 | EVBACKEND_MASK = 0x0000FFFFU /* all future backends */ |
520 | }; |
526 | }; |
… | |
… | |
593 | } |
599 | } |
594 | |
600 | |
595 | #endif /* multiplicity */ |
601 | #endif /* multiplicity */ |
596 | |
602 | |
597 | /* destroy event loops, also works for the default loop */ |
603 | /* destroy event loops, also works for the default loop */ |
598 | EV_API_DECL void ev_loop_destroy (EV_P) EV_THROW; |
604 | EV_API_DECL void ev_loop_destroy (EV_P); |
599 | |
605 | |
600 | /* this needs to be called after fork, to duplicate the loop */ |
606 | /* this needs to be called after fork, to duplicate the loop */ |
601 | /* when you want to re-use it in the child */ |
607 | /* when you want to re-use it in the child */ |
602 | /* you can call it in either the parent or the child */ |
608 | /* you can call it in either the parent or the child */ |
603 | /* you can actually call it at any time, anywhere :) */ |
609 | /* you can actually call it at any time, anywhere :) */ |
… | |
… | |
656 | EV_API_DECL void ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) EV_THROW; /* sleep at least this time, default 0 */ |
662 | EV_API_DECL void ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval) EV_THROW; /* sleep at least this time, default 0 */ |
657 | |
663 | |
658 | /* advanced stuff for threading etc. support, see docs */ |
664 | /* advanced stuff for threading etc. support, see docs */ |
659 | EV_API_DECL void ev_set_userdata (EV_P_ void *data) EV_THROW; |
665 | EV_API_DECL void ev_set_userdata (EV_P_ void *data) EV_THROW; |
660 | EV_API_DECL void *ev_userdata (EV_P) EV_THROW; |
666 | EV_API_DECL void *ev_userdata (EV_P) EV_THROW; |
|
|
667 | typedef void (*ev_loop_callback)(EV_P); |
661 | EV_API_DECL void ev_set_invoke_pending_cb (EV_P_ void (*invoke_pending_cb)(EV_P)) EV_THROW; |
668 | EV_API_DECL void ev_set_invoke_pending_cb (EV_P_ ev_loop_callback invoke_pending_cb) EV_THROW; |
|
|
669 | /* C++ doesn't allow the use of the ev_loop_callback typedef here, so we need to spell it out */ |
662 | EV_API_DECL void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P), void (*acquire)(EV_P)) EV_THROW; |
670 | EV_API_DECL void ev_set_loop_release_cb (EV_P_ void (*release)(EV_P) EV_THROW, void (*acquire)(EV_P) EV_THROW) EV_THROW; |
663 | |
671 | |
664 | EV_API_DECL unsigned int ev_pending_count (EV_P) EV_THROW; /* number of pending events, if any */ |
672 | EV_API_DECL unsigned int ev_pending_count (EV_P) EV_THROW; /* number of pending events, if any */ |
665 | EV_API_DECL void ev_invoke_pending (EV_P); /* invoke all pending watchers */ |
673 | EV_API_DECL void ev_invoke_pending (EV_P); /* invoke all pending watchers */ |
666 | |
674 | |
667 | /* |
675 | /* |
… | |
… | |
711 | #define ev_async_init(ev,cb) do { ev_init ((ev), (cb)); ev_async_set ((ev)); } while (0) |
719 | #define ev_async_init(ev,cb) do { ev_init ((ev), (cb)); ev_async_set ((ev)); } while (0) |
712 | |
720 | |
713 | #define ev_is_pending(ev) (0 + ((ev_watcher *)(void *)(ev))->pending) /* ro, true when watcher is waiting for callback invocation */ |
721 | #define ev_is_pending(ev) (0 + ((ev_watcher *)(void *)(ev))->pending) /* ro, true when watcher is waiting for callback invocation */ |
714 | #define ev_is_active(ev) (0 + ((ev_watcher *)(void *)(ev))->active) /* ro, true when the watcher has been started */ |
722 | #define ev_is_active(ev) (0 + ((ev_watcher *)(void *)(ev))->active) /* ro, true when the watcher has been started */ |
715 | |
723 | |
716 | #define ev_cb(ev) (ev)->cb /* rw */ |
724 | #define ev_cb_(ev) (ev)->cb /* rw */ |
|
|
725 | #define ev_cb(ev) (memmove (&ev_cb_ (ev), &((ev_watcher *)(ev))->cb, sizeof (ev_cb_ (ev))), (ev)->cb) |
717 | |
726 | |
718 | #if EV_MINPRI == EV_MAXPRI |
727 | #if EV_MINPRI == EV_MAXPRI |
719 | # define ev_priority(ev) ((ev), EV_MINPRI) |
728 | # define ev_priority(ev) ((ev), EV_MINPRI) |
720 | # define ev_set_priority(ev,pri) ((ev), (pri)) |
729 | # define ev_set_priority(ev,pri) ((ev), (pri)) |
721 | #else |
730 | #else |
… | |
… | |
724 | #endif |
733 | #endif |
725 | |
734 | |
726 | #define ev_periodic_at(ev) (+((ev_watcher_time *)(ev))->at) |
735 | #define ev_periodic_at(ev) (+((ev_watcher_time *)(ev))->at) |
727 | |
736 | |
728 | #ifndef ev_set_cb |
737 | #ifndef ev_set_cb |
729 | # define ev_set_cb(ev,cb_) ev_cb (ev) = (cb_) |
738 | # define ev_set_cb(ev,cb_) (ev_cb_ (ev) = (cb_), memmove (&((ev_watcher *)(ev))->cb, &ev_cb_ (ev), sizeof (ev_cb_ (ev)))) |
730 | #endif |
739 | #endif |
731 | |
740 | |
732 | /* stopping (enabling, adding) a watcher does nothing if it is already running */ |
741 | /* stopping (enabling, adding) a watcher does nothing if it is already running */ |
733 | /* stopping (disabling, deleting) a watcher does nothing unless its already running */ |
742 | /* stopping (disabling, deleting) a watcher does nothing unless it's already running */ |
734 | #if EV_PROTOTYPES |
743 | #if EV_PROTOTYPES |
735 | |
744 | |
736 | /* feeds an event into a watcher as if the event actually occurred */ |
745 | /* feeds an event into a watcher as if the event actually occurred */ |
737 | /* accepts any ev_watcher type */ |
746 | /* accepts any ev_watcher type */ |
738 | EV_API_DECL void ev_feed_event (EV_P_ void *w, int revents) EV_THROW; |
747 | EV_API_DECL void ev_feed_event (EV_P_ void *w, int revents) EV_THROW; |