2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 typedef double ev_tstamp;
39 /* these priorities are inclusive, higher priorities will be called earlier */
47 #ifndef EV_MULTIPLICITY
48 # define EV_MULTIPLICITY 1
51 #ifndef EV_PERIODIC_ENABLE
52 # define EV_PERIODIC_ENABLE 1
55 #ifndef EV_STAT_ENABLE
56 # define EV_STAT_ENABLE 1
59 #ifndef EV_IDLE_ENABLE
60 # define EV_IDLE_ENABLE 1
63 #ifndef EV_FORK_ENABLE
64 # define EV_FORK_ENABLE 1
67 #ifndef EV_EMBED_ENABLE
68 # define EV_EMBED_ENABLE 1
71 /*****************************************************************************/
74 # include <sys/stat.h>
77 /* support multiple event loops? */
80 # define EV_P struct ev_loop *loop
84 # define EV_DEFAULT ev_default_loop (0)
85 # define EV_DEFAULT_ EV_DEFAULT,
94 # undef EV_EMBED_ENABLE
97 /* eventmask, revents, events... */
98 #define EV_UNDEF -1L /* guaranteed to be invalid */
99 #define EV_NONE 0x00L /* no events */
100 #define EV_READ 0x01L /* ev_io detected read will not block */
101 #define EV_WRITE 0x02L /* ev_io detected write will not block */
102 #define EV_IOFDSET 0x80L /* internal use only */
103 #define EV_TIMEOUT 0x00000100L /* timer timed out */
104 #define EV_PERIODIC 0x00000200L /* periodic timer timed out */
105 #define EV_SIGNAL 0x00000400L /* signal was received */
106 #define EV_CHILD 0x00000800L /* child/pid had status change */
107 #define EV_STAT 0x00001000L /* stat data changed */
108 #define EV_IDLE 0x00002000L /* event loop is idling */
109 #define EV_PREPARE 0x00004000L /* event loop about to poll */
110 #define EV_CHECK 0x00008000L /* event loop finished poll */
111 #define EV_EMBED 0x00010000L /* embedded event loop needs sweep */
112 #define EV_FORK 0x00020000L /* event loop resumed in child */
113 #define EV_ERROR 0x80000000L /* sent when an error occurs */
115 /* can be used to add custom fields to all watchers, while losing binary compatibility */
117 # define EV_COMMON void *data;
119 #ifndef EV_PROTOTYPES
120 # define EV_PROTOTYPES 1
123 #define EV_VERSION_MAJOR 2
124 #define EV_VERSION_MINOR 0
126 #ifndef EV_CB_DECLARE
127 # define EV_CB_DECLARE(type) void (*cb)(EV_P_ struct type *w, int revents);
130 # define EV_CB_INVOKE(watcher,revents) (watcher)->cb (EV_A_ (watcher), (revents))
134 * struct member types:
135 * private: you can look at them, but not change them, and they might not mean anything to you.
136 * ro: can be read anytime, but only changed when the watcher isn't active
137 * rw: can be read and modified anytime, even when the watcher is active
140 /* shared by all watchers */
141 #define EV_WATCHER(type) \
142 int active; /* private */ \
143 int pending; /* private */ \
144 int priority; /* private */ \
146 EV_CB_DECLARE (type) /* private */
148 #define EV_WATCHER_LIST(type) \
150 struct ev_watcher_list *next; /* private */
152 #define EV_WATCHER_TIME(type) \
154 ev_tstamp at; /* private */
156 /* base class, nothing to see here unless you subclass */
157 typedef struct ev_watcher
159 EV_WATCHER (ev_watcher)
162 /* base class, nothing to see here unless you subclass */
163 typedef struct ev_watcher_list
165 EV_WATCHER_LIST (ev_watcher_list)
168 /* base class, nothing to see here unless you subclass */
169 typedef struct ev_watcher_time
171 EV_WATCHER_TIME (ev_watcher_time)
174 /* invoked when fd is either EV_READable or EV_WRITEable */
175 /* revent EV_READ, EV_WRITE */
178 EV_WATCHER_LIST (ev_io)
184 /* invoked after a specific time, repeatable (based on monotonic clock) */
185 /* revent EV_TIMEOUT */
186 typedef struct ev_timer
188 EV_WATCHER_TIME (ev_timer)
190 ev_tstamp repeat; /* rw */
193 /* invoked at some specific time, possibly repeating at regular intervals (based on UTC) */
194 /* revent EV_PERIODIC */
195 typedef struct ev_periodic
197 EV_WATCHER_TIME (ev_periodic)
199 ev_tstamp offset; /* rw */
200 ev_tstamp interval; /* rw */
201 ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now); /* rw */
204 /* invoked when the given signal has been received */
205 /* revent EV_SIGNAL */
206 typedef struct ev_signal
208 EV_WATCHER_LIST (ev_signal)
213 /* invoked when sigchld is received and waitpid indicates the given pid */
214 /* revent EV_CHILD */
215 /* does not support priorities */
216 typedef struct ev_child
218 EV_WATCHER_LIST (ev_child)
221 int rpid; /* rw, holds the received pid */
222 int rstatus; /* rw, holds the exit status, use the macros from sys/wait.h */
226 /* st_nlink = 0 means missing file or other error */
228 typedef struct _stati64 ev_statdata;
230 typedef struct stat ev_statdata;
233 /* invoked each time the stat data changes for a given path */
235 typedef struct ev_stat
237 EV_WATCHER_LIST (ev_stat)
239 ev_timer timer; /* private */
240 ev_tstamp interval; /* ro */
241 const char *path; /* ro */
242 ev_statdata prev; /* ro */
243 ev_statdata attr; /* ro */
245 int wd; /* wd for inotify, fd for kqueue */
250 /* invoked when the nothing else needs to be done, keeps the process from blocking */
252 typedef struct ev_idle
258 /* invoked for each run of the mainloop, just before the blocking call */
259 /* you can still change events in any way you like */
260 /* revent EV_PREPARE */
261 typedef struct ev_prepare
263 EV_WATCHER (ev_prepare)
266 /* invoked for each run of the mainloop, just after the blocking call */
267 /* revent EV_CHECK */
268 typedef struct ev_check
270 EV_WATCHER (ev_check)
274 /* the callback gets invoked before check in the child process when a fork was detected */
275 typedef struct ev_fork
282 /* used to embed an event loop inside another */
283 /* the callback gets invoked when the event loop has handled events, and can be 0 */
284 typedef struct ev_embed
286 EV_WATCHER (ev_embed)
288 struct ev_loop *other; /* ro */
289 ev_io io; /* private */
290 ev_prepare prepare; /* private */
291 ev_check check; /* unused */
292 ev_timer timer; /* unused */
293 ev_periodic periodic; /* unused */
294 ev_idle idle; /* unused */
295 ev_fork fork; /* unused */
299 /* the presence of this union forces similar struct layout */
303 struct ev_watcher_list wl;
306 struct ev_timer timer;
307 struct ev_periodic periodic;
308 struct ev_signal signal;
309 struct ev_child child;
316 struct ev_prepare prepare;
317 struct ev_check check;
322 struct ev_embed embed;
326 /* bits for ev_default_loop and ev_loop_new */
328 #define EVFLAG_AUTO 0x00000000UL /* not quite a mask */
330 #define EVFLAG_NOENV 0x01000000UL /* do NOT consult environment */
331 #define EVFLAG_FORKCHECK 0x02000000UL /* check for a fork in each iteration */
332 /* method bits to be ored together */
333 #define EVBACKEND_SELECT 0x00000001UL /* about anywhere */
334 #define EVBACKEND_POLL 0x00000002UL /* !win */
335 #define EVBACKEND_EPOLL 0x00000004UL /* linux */
336 #define EVBACKEND_KQUEUE 0x00000008UL /* bsd */
337 #define EVBACKEND_DEVPOLL 0x00000010UL /* solaris 8 */ /* NYI */
338 #define EVBACKEND_PORT 0x00000020UL /* solaris 10 */
341 int ev_version_major (void);
342 int ev_version_minor (void);
344 unsigned int ev_supported_backends (void);
345 unsigned int ev_recommended_backends (void);
346 unsigned int ev_embeddable_backends (void);
348 ev_tstamp ev_time (void);
349 void ev_sleep (ev_tstamp delay); /* sleep for a while */
351 /* Sets the allocation function to use, works like realloc.
352 * It is used to allocate and free memory.
353 * If it returns zero when memory needs to be allocated, the library might abort
354 * or take some potentially destructive action.
355 * The default is your system realloc function.
357 void ev_set_allocator (void *(*cb)(void *ptr, long size));
359 /* set the callback function to call on a
360 * retryable syscall error
361 * (such as failed select, poll, epoll_wait)
363 void ev_set_syserr_cb (void (*cb)(const char *msg));
366 /* the default loop is the only one that handles signals and child watchers */
367 /* you can call this as often as you like */
368 static struct ev_loop *
369 ev_default_loop (unsigned int flags)
371 extern struct ev_loop *ev_default_loop_ptr;
372 extern struct ev_loop *ev_default_loop_init (unsigned int flags);
374 if (!ev_default_loop_ptr)
375 ev_default_loop_init (flags);
377 return ev_default_loop_ptr;
380 /* create and destroy alternative loops that don't handle signals */
381 struct ev_loop *ev_loop_new (unsigned int flags);
382 void ev_loop_destroy (EV_P);
383 void ev_loop_fork (EV_P);
385 ev_tstamp ev_now (EV_P); /* time w.r.t. timers and the eventloop, updated after each poll */
389 int ev_default_loop (unsigned int flags); /* returns true when successful */
394 extern ev_tstamp ev_rt_now;
400 void ev_default_destroy (void); /* destroy the default loop */
401 /* this needs to be called after fork, to duplicate the default loop */
402 /* if you create alternative loops you have to call ev_loop_fork on them */
403 /* you can call it in either the parent or the child */
404 /* you can actually call it at any time, anywhere :) */
405 void ev_default_fork (void);
407 unsigned int ev_backend (EV_P); /* backend in use by loop */
408 unsigned int ev_loop_count (EV_P); /* number of loop iterations */
411 #define EVLOOP_NONBLOCK 1 /* do not block/wait */
412 #define EVLOOP_ONESHOT 2 /* block *once* only */
413 #define EVUNLOOP_CANCEL 0 /* undo unloop */
414 #define EVUNLOOP_ONE 1 /* unloop once */
415 #define EVUNLOOP_ALL 2 /* unloop all loops */
418 void ev_loop (EV_P_ int flags);
419 void ev_unloop (EV_P_ int how); /* set to 1 to break out of event loop, set to 2 to break out of all event loops */
421 void ev_set_io_collect_interval (EV_P_ ev_tstamp interval); /* sleep at least this time, default 0 */
422 void ev_set_timeout_collect_interval (EV_P_ ev_tstamp interval); /* sleep at least this time, default 0 */
425 * ref/unref can be used to add or remove a refcount on the mainloop. every watcher
426 * keeps one reference. if you have a long-runing watcher you never unregister that
427 * should not keep ev_loop from running, unref() after starting, and ref() before stopping.
430 void ev_unref (EV_P);
432 /* convinience function, wait for a single event, without registering an event watcher */
433 /* if timeout is < 0, do wait indefinitely */
434 void ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg);
437 /* these may evaluate ev multiple times, and the other arguments at most once */
438 /* either use ev_init + ev_TYPE_set, or the ev_TYPE_init macro, below, to first initialise a watcher */
439 #define ev_init(ev,cb_) do { \
440 ((ev_watcher *)(void *)(ev))->active = \
441 ((ev_watcher *)(void *)(ev))->pending = \
442 ((ev_watcher *)(void *)(ev))->priority = 0; \
443 ev_set_cb ((ev), cb_); \
446 #define ev_io_set(ev,fd_,events_) do { (ev)->fd = (fd_); (ev)->events = (events_) | EV_IOFDSET; } while (0)
447 #define ev_timer_set(ev,after_,repeat_) do { (ev)->at = (after_); (ev)->repeat = (repeat_); } while (0)
448 #define ev_periodic_set(ev,ofs_,ival_,res_) do { (ev)->offset = (ofs_); (ev)->interval = (ival_); (ev)->reschedule_cb= (res_); } while (0)
449 #define ev_signal_set(ev,signum_) do { (ev)->signum = (signum_); } while (0)
450 #define ev_child_set(ev,pid_) do { (ev)->pid = (pid_); } while (0)
451 #define ev_stat_set(ev,path_,interval_) do { (ev)->path = (path_); (ev)->interval = (interval_); (ev)->wd = -2; } while (0)
452 #define ev_idle_set(ev) /* nop, yes, this is a serious in-joke */
453 #define ev_prepare_set(ev) /* nop, yes, this is a serious in-joke */
454 #define ev_check_set(ev) /* nop, yes, this is a serious in-joke */
455 #define ev_embed_set(ev,other_) do { (ev)->other = (other_); } while (0)
456 #define ev_fork_set(ev) /* nop, yes, this is a serious in-joke */
458 #define ev_io_init(ev,cb,fd,events) do { ev_init ((ev), (cb)); ev_io_set ((ev),(fd),(events)); } while (0)
459 #define ev_timer_init(ev,cb,after,repeat) do { ev_init ((ev), (cb)); ev_timer_set ((ev),(after),(repeat)); } while (0)
460 #define ev_periodic_init(ev,cb,at,ival,res) do { ev_init ((ev), (cb)); ev_periodic_set ((ev),(at),(ival),(res)); } while (0)
461 #define ev_signal_init(ev,cb,signum) do { ev_init ((ev), (cb)); ev_signal_set ((ev), (signum)); } while (0)
462 #define ev_child_init(ev,cb,pid) do { ev_init ((ev), (cb)); ev_child_set ((ev),(pid)); } while (0)
463 #define ev_stat_init(ev,cb,path,interval) do { ev_init ((ev), (cb)); ev_path_set ((ev),(path),(interval)); } while (0)
464 #define ev_idle_init(ev,cb) do { ev_init ((ev), (cb)); ev_idle_set ((ev)); } while (0)
465 #define ev_prepare_init(ev,cb) do { ev_init ((ev), (cb)); ev_prepare_set ((ev)); } while (0)
466 #define ev_check_init(ev,cb) do { ev_init ((ev), (cb)); ev_check_set ((ev)); } while (0)
467 #define ev_embed_init(ev,cb,other) do { ev_init ((ev), (cb)); ev_embed_set ((ev),(other)); } while (0)
468 #define ev_fork_init(ev,cb) do { ev_init ((ev), (cb)); ev_fork_set ((ev)); } while (0)
470 #define ev_is_pending(ev) (0 + ((ev_watcher *)(void *)(ev))->pending) /* ro, true when watcher is waiting for callback invocation */
471 #define ev_is_active(ev) (0 + ((ev_watcher *)(void *)(ev))->active) /* ro, true when the watcher has been started */
473 #define ev_priority(ev) ((((ev_watcher *)(void *)(ev))->priority) + 0)
474 #define ev_cb(ev) (ev)->cb /* rw */
475 #define ev_set_priority(ev,pri) ((ev_watcher *)(void *)(ev))->priority = (pri)
478 # define ev_set_cb(ev,cb_) ev_cb (ev) = (cb_)
481 /* stopping (enabling, adding) a watcher does nothing if it is already running */
482 /* stopping (disabling, deleting) a watcher does nothing unless its already running */
485 /* feeds an event into a watcher as if the event actually occured */
486 /* accepts any ev_watcher type */
487 void ev_feed_event (EV_P_ void *w, int revents);
488 void ev_feed_fd_event (EV_P_ int fd, int revents);
489 void ev_feed_signal_event (EV_P_ int signum);
490 void ev_invoke (EV_P_ void *w, int revents);
491 int ev_clear_pending (EV_P_ void *w);
493 void ev_io_start (EV_P_ ev_io *w);
494 void ev_io_stop (EV_P_ ev_io *w);
496 void ev_timer_start (EV_P_ ev_timer *w);
497 void ev_timer_stop (EV_P_ ev_timer *w);
498 /* stops if active and no repeat, restarts if active and repeating, starts if inactive and repeating */
499 void ev_timer_again (EV_P_ ev_timer *w);
501 #if EV_PERIODIC_ENABLE
502 void ev_periodic_start (EV_P_ ev_periodic *w);
503 void ev_periodic_stop (EV_P_ ev_periodic *w);
504 void ev_periodic_again (EV_P_ ev_periodic *w);
507 /* only supported in the default loop */
508 void ev_signal_start (EV_P_ ev_signal *w);
509 void ev_signal_stop (EV_P_ ev_signal *w);
511 /* only supported in the default loop */
512 void ev_child_start (EV_P_ ev_child *w);
513 void ev_child_stop (EV_P_ ev_child *w);
516 void ev_stat_start (EV_P_ ev_stat *w);
517 void ev_stat_stop (EV_P_ ev_stat *w);
518 void ev_stat_stat (EV_P_ ev_stat *w);
522 void ev_idle_start (EV_P_ ev_idle *w);
523 void ev_idle_stop (EV_P_ ev_idle *w);
526 void ev_prepare_start (EV_P_ ev_prepare *w);
527 void ev_prepare_stop (EV_P_ ev_prepare *w);
529 void ev_check_start (EV_P_ ev_check *w);
530 void ev_check_stop (EV_P_ ev_check *w);
533 void ev_fork_start (EV_P_ ev_fork *w);
534 void ev_fork_stop (EV_P_ ev_fork *w);
538 /* only supported when loop to be embedded is in fact embeddable */
539 void ev_embed_start (EV_P_ ev_embed *w);
540 void ev_embed_stop (EV_P_ ev_embed *w);
541 void ev_embed_sweep (EV_P_ ev_embed *w);