Mirror of :pserver:anonymous@cvs.schmorp.de/schmorpforge libev http://software.schmorp.de/pkg/libev.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

450 lines
15 KiB

14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
  1. /*
  2. * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  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.
  16. *
  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.
  28. */
  29. #ifndef EV_H__
  30. #define EV_H__
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. typedef double ev_tstamp;
  35. /* these priorities are inclusive, higher priorities will be called earlier */
  36. #ifndef EV_MINPRI
  37. # define EV_MINPRI -2
  38. #endif
  39. #ifndef EV_MAXPRI
  40. # define EV_MAXPRI +2
  41. #endif
  42. #ifndef EV_MULTIPLICITY
  43. # define EV_MULTIPLICITY 1
  44. #endif
  45. #ifndef EV_PERIODICS
  46. # define EV_PERIODICS 1
  47. #endif
  48. /* support multiple event loops? */
  49. #if EV_MULTIPLICITY
  50. struct ev_loop;
  51. # define EV_P struct ev_loop *loop
  52. # define EV_P_ EV_P,
  53. # define EV_A loop
  54. # define EV_A_ EV_A,
  55. # define EV_DEFAULT_A ev_default_loop (0)
  56. # define EV_DEFAULT_A_ EV_DEFAULT_A,
  57. #else
  58. # define EV_P void
  59. # define EV_P_
  60. # define EV_A
  61. # define EV_A_
  62. # define EV_DEFAULT_A
  63. # define EV_DEFAULT_A_
  64. #endif
  65. /* eventmask, revents, events... */
  66. #define EV_UNDEF -1L /* guaranteed to be invalid */
  67. #define EV_NONE 0x00L
  68. #define EV_READ 0x01L /* io only */
  69. #define EV_WRITE 0x02L /* io only */
  70. #define EV_TIMEOUT 0x000100L /* timer only */
  71. #define EV_PERIODIC 0x000200L /* periodic timer only */
  72. #define EV_SIGNAL 0x000400L /* signal only */
  73. #define EV_IDLE 0x000800L /* idle only */
  74. #define EV_CHECK 0x001000L /* check only */
  75. #define EV_PREPARE 0x002000L /* prepare only */
  76. #define EV_CHILD 0x004000L /* child/pid only */
  77. #define EV_EMBED 0x008000L /* embedded event loop */
  78. #define EV_ERROR 0x800000L /* sent when an error occurs */
  79. /* can be used to add custom fields to all watchers, while losing binary compatibility */
  80. #ifndef EV_COMMON
  81. # define EV_COMMON void *data;
  82. #endif
  83. #ifndef EV_PROTOTYPES
  84. # define EV_PROTOTYPES 1
  85. #endif
  86. #define EV_VERSION_MAJOR 1
  87. #define EV_VERSION_MINOR 1
  88. #ifndef EV_CB_DECLARE
  89. # define EV_CB_DECLARE(type) void (*cb)(EV_P_ struct type *w, int revents);
  90. #endif
  91. #ifndef EV_CB_INVOKE
  92. # define EV_CB_INVOKE(watcher,revents) (watcher)->cb (EV_A_ (watcher), (revents))
  93. #endif
  94. /*
  95. * struct member types:
  96. * private: you can look at them, but not change them, and they might not mean anything to you.
  97. * ro: can be read anytime, but only changed when the watcher isn't active
  98. * rw: can be read and modified anytime, even when the watcher is active
  99. */
  100. /* shared by all watchers */
  101. #define EV_WATCHER(type) \
  102. int active; /* private */ \
  103. int pending; /* private */ \
  104. int priority; /* private */ \
  105. EV_COMMON /* rw */ \
  106. EV_CB_DECLARE (type) /* private */
  107. #define EV_WATCHER_LIST(type) \
  108. EV_WATCHER (type) \
  109. struct ev_watcher_list *next; /* private */
  110. #define EV_WATCHER_TIME(type) \
  111. EV_WATCHER (type) \
  112. ev_tstamp at; /* private */
  113. /* base class, nothing to see here unless you subclass */
  114. typedef struct ev_watcher
  115. {
  116. EV_WATCHER (ev_watcher)
  117. } ev_watcher;
  118. /* base class, nothing to see here unless you subclass */
  119. typedef struct ev_watcher_list
  120. {
  121. EV_WATCHER_LIST (ev_watcher_list)
  122. } ev_watcher_list;
  123. /* base class, nothing to see here unless you subclass */
  124. typedef struct ev_watcher_time
  125. {
  126. EV_WATCHER_TIME (ev_watcher_time)
  127. } ev_watcher_time;
  128. /* invoked after a specific time, repeatable (based on monotonic clock) */
  129. /* revent EV_TIMEOUT */
  130. typedef struct ev_timer
  131. {
  132. EV_WATCHER_TIME (ev_timer)
  133. ev_tstamp repeat; /* rw */
  134. } ev_timer;
  135. /* invoked at some specific time, possibly repeating at regular intervals (based on UTC) */
  136. /* revent EV_PERIODIC */
  137. typedef struct ev_periodic
  138. {
  139. EV_WATCHER_TIME (ev_periodic)
  140. ev_tstamp interval; /* rw */
  141. ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now); /* rw */
  142. } ev_periodic;
  143. /* invoked when fd is either EV_READable or EV_WRITEable */
  144. /* revent EV_READ, EV_WRITE */
  145. typedef struct ev_io
  146. {
  147. EV_WATCHER_LIST (ev_io)
  148. int fd; /* ro */
  149. int events; /* ro */
  150. } ev_io;
  151. /* invoked when the given signal has been received */
  152. /* revent EV_SIGNAL */
  153. typedef struct ev_signal
  154. {
  155. EV_WATCHER_LIST (ev_signal)
  156. int signum; /* ro */
  157. } ev_signal;
  158. /* invoked when the nothing else needs to be done, keeps the process from blocking */
  159. /* revent EV_IDLE */
  160. typedef struct ev_idle
  161. {
  162. EV_WATCHER (ev_idle)
  163. } ev_idle;
  164. /* invoked for each run of the mainloop, just before the blocking call */
  165. /* you can still change events in any way you like */
  166. /* revent EV_PREPARE */
  167. typedef struct ev_prepare
  168. {
  169. EV_WATCHER (ev_prepare)
  170. } ev_prepare;
  171. /* invoked for each run of the mainloop, just after the blocking call */
  172. /* revent EV_CHECK */
  173. typedef struct ev_check
  174. {
  175. EV_WATCHER (ev_check)
  176. } ev_check;
  177. /* invoked when sigchld is received and waitpid indicates the given pid */
  178. /* revent EV_CHILD */
  179. /* does not support priorities */
  180. typedef struct ev_child
  181. {
  182. EV_WATCHER_LIST (ev_child)
  183. int pid; /* ro */
  184. int rpid; /* rw, holds the received pid */
  185. int rstatus; /* rw, holds the exit status, use the macros from sys/wait.h */
  186. } ev_child;
  187. #if EV_MULTIPLICITY
  188. /* used to embed an event loop inside another */
  189. /* the callback gets invoked when the event loop has handled events, and can be 0 */
  190. typedef struct ev_embed
  191. {
  192. EV_WATCHER (ev_embed)
  193. ev_io io; /* private */
  194. struct ev_loop *loop; /* ro */
  195. } ev_embed;
  196. #endif
  197. /* the presence of this union forces similar struct layout */
  198. union ev_any_watcher
  199. {
  200. struct ev_watcher w;
  201. struct ev_watcher_list wl;
  202. struct ev_io io;
  203. struct ev_timer timer;
  204. struct ev_periodic periodic;
  205. struct ev_idle idle;
  206. struct ev_prepare prepare;
  207. struct ev_check check;
  208. struct ev_signal signal;
  209. struct ev_child child;
  210. #if EV_MULTIPLICITY
  211. struct ev_embed embed;
  212. #endif
  213. };
  214. /* bits for ev_default_loop and ev_loop_new */
  215. /* the default */
  216. #define EVFLAG_AUTO 0x00000000UL /* not quite a mask */
  217. /* flag bits */
  218. #define EVFLAG_NOENV 0x01000000UL /* do NOT consult environment */
  219. /* method bits to be ored together */
  220. #define EVBACKEND_SELECT 0x00000001UL /* about anywhere */
  221. #define EVBACKEND_POLL 0x00000002UL /* !win */
  222. #define EVBACKEND_EPOLL 0x00000004UL /* linux */
  223. #define EVBACKEND_KQUEUE 0x00000008UL /* bsd */
  224. #define EVBACKEND_DEVPOLL 0x00000010UL /* solaris 8 */ /* NYI */
  225. #define EVBACKEND_PORT 0x00000020UL /* solaris 10 */
  226. #if EV_PROTOTYPES
  227. int ev_version_major (void);
  228. int ev_version_minor (void);
  229. unsigned int ev_supported_backends (void);
  230. unsigned int ev_recommended_backends (void);
  231. unsigned int ev_embeddable_backends (void);
  232. ev_tstamp ev_time (void);
  233. /* Sets the allocation function to use, works like realloc.
  234. * It is used to allocate and free memory.
  235. * If it returns zero when memory needs to be allocated, the library might abort
  236. * or take some potentially destructive action.
  237. * The default is your system realloc function.
  238. */
  239. void ev_set_allocator (void *(*cb)(void *ptr, long size));
  240. /* set the callback function to call on a
  241. * retryable syscall error
  242. * (such as failed select, poll, epoll_wait)
  243. */
  244. void ev_set_syserr_cb (void (*cb)(const char *msg));
  245. # if EV_MULTIPLICITY
  246. /* the default loop is the only one that handles signals and child watchers */
  247. /* you can call this as often as you like */
  248. static struct ev_loop *
  249. ev_default_loop (unsigned int flags)
  250. {
  251. extern struct ev_loop *ev_default_loop_ptr;
  252. extern struct ev_loop *ev_default_loop_init (unsigned int flags);
  253. if (!ev_default_loop_ptr)
  254. ev_default_loop_init (flags);
  255. return ev_default_loop_ptr;
  256. }
  257. /* create and destroy alternative loops that don't handle signals */
  258. struct ev_loop *ev_loop_new (unsigned int flags);
  259. void ev_loop_destroy (EV_P);
  260. void ev_loop_fork (EV_P);
  261. ev_tstamp ev_now (EV_P); /* time w.r.t. timers and the eventloop, updated after each poll */
  262. # else
  263. int ev_default_loop (unsigned int flags); /* returns true when successful */
  264. static ev_tstamp
  265. ev_now (void)
  266. {
  267. extern ev_tstamp ev_rt_now;
  268. return ev_rt_now;
  269. }
  270. # endif
  271. void ev_default_destroy (void); /* destroy the default loop */
  272. /* this needs to be called after fork, to duplicate the default loop */
  273. /* if you create alternative loops you have to call ev_loop_fork on them */
  274. /* you can call it in either the parent or the child */
  275. /* you can actually call it at any time, anywhere :) */
  276. void ev_default_fork (void);
  277. unsigned int ev_backend (EV_P);
  278. #endif
  279. #define EVLOOP_NONBLOCK 1 /* do not block/wait */
  280. #define EVLOOP_ONESHOT 2 /* block *once* only */
  281. #define EVUNLOOP_CANCEL 0 /* undo unloop */
  282. #define EVUNLOOP_ONE 1 /* unloop once */
  283. #define EVUNLOOP_ALL 2 /* unloop all loops */
  284. #if EV_PROTOTYPES
  285. void ev_loop (EV_P_ int flags);
  286. 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 */
  287. /*
  288. * ref/unref can be used to add or remove a refcount on the mainloop. every watcher
  289. * keeps one reference. if you have a long-runing watcher you never unregister that
  290. * should not keep ev_loop from running, unref() after starting, and ref() before stopping.
  291. */
  292. void ev_ref (EV_P);
  293. void ev_unref (EV_P);
  294. /* convinience function, wait for a single event, without registering an event watcher */
  295. /* if timeout is < 0, do wait indefinitely */
  296. void ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg);
  297. #endif
  298. /* these may evaluate ev multiple times, and the other arguments at most once */
  299. /* either use ev_init + ev_TYPE_set, or the ev_TYPE_init macro, below, to first initialise a watcher */
  300. #define ev_init(ev,cb_) do { \
  301. ((ev_watcher *)(void *)(ev))->active = \
  302. ((ev_watcher *)(void *)(ev))->pending = \
  303. ((ev_watcher *)(void *)(ev))->priority = 0; \
  304. ev_set_cb ((ev), cb_); \
  305. } while (0)
  306. #define ev_io_set(ev,fd_,events_) do { (ev)->fd = (fd_); (ev)->events = (events_); } while (0)
  307. #define ev_timer_set(ev,after_,repeat_) do { (ev)->at = (after_); (ev)->repeat = (repeat_); } while (0)
  308. #define ev_periodic_set(ev,at_,ival_,res_) do { (ev)->at = (at_); (ev)->interval = (ival_); (ev)->reschedule_cb= (res_); } while (0)
  309. #define ev_signal_set(ev,signum_) do { (ev)->signum = (signum_); } while (0)
  310. #define ev_idle_set(ev) /* nop, yes, this is a serious in-joke */
  311. #define ev_prepare_set(ev) /* nop, yes, this is a serious in-joke */
  312. #define ev_check_set(ev) /* nop, yes, this is a serious in-joke */
  313. #define ev_child_set(ev,pid_) do { (ev)->pid = (pid_); } while (0)
  314. #define ev_embed_set(ev,loop_) do { (ev)->loop = (loop_); } while (0)
  315. #define ev_io_init(ev,cb,fd,events) do { ev_init ((ev), (cb)); ev_io_set ((ev),(fd),(events)); } while (0)
  316. #define ev_timer_init(ev,cb,after,repeat) do { ev_init ((ev), (cb)); ev_timer_set ((ev),(after),(repeat)); } while (0)
  317. #define ev_periodic_init(ev,cb,at,ival,res) do { ev_init ((ev), (cb)); ev_periodic_set ((ev),(at),(ival),(res)); } while (0)
  318. #define ev_signal_init(ev,cb,signum) do { ev_init ((ev), (cb)); ev_signal_set ((ev), (signum)); } while (0)
  319. #define ev_idle_init(ev,cb) do { ev_init ((ev), (cb)); ev_idle_set ((ev)); } while (0)
  320. #define ev_prepare_init(ev,cb) do { ev_init ((ev), (cb)); ev_prepare_set ((ev)); } while (0)
  321. #define ev_check_init(ev,cb) do { ev_init ((ev), (cb)); ev_check_set ((ev)); } while (0)
  322. #define ev_child_init(ev,cb,pid) do { ev_init ((ev), (cb)); ev_child_set ((ev),(pid)); } while (0)
  323. #define ev_embed_init(ev,cb,loop) do { ev_init ((ev), (cb)); ev_embed_set ((ev),(loop)); } while (0)
  324. #define ev_is_pending(ev) (0 + ((ev_watcher *)(void *)(ev))->pending) /* ro, true when watcher is waiting for callback invocation */
  325. #define ev_is_active(ev) (0 + ((ev_watcher *)(void *)(ev))->active) /* ro, true when the watcher has been started */
  326. #define ev_priority(ev) ((ev_watcher *)(void *)(ev))->priority /* rw */
  327. #define ev_cb(ev) (ev)->cb /* rw */
  328. #define ev_set_priority(ev,pri) ev_priority (ev) = (pri)
  329. #ifndef ev_set_cb
  330. # define ev_set_cb(ev,cb_) ev_cb (ev) = (cb_)
  331. #endif
  332. /* stopping (enabling, adding) a watcher does nothing if it is already running */
  333. /* stopping (disabling, deleting) a watcher does nothing unless its already running */
  334. #if EV_PROTOTYPES
  335. /* feeds an event into a watcher as if the event actually occured */
  336. /* accepts any ev_watcher type */
  337. void ev_feed_event (EV_P_ void *w, int revents);
  338. void ev_feed_fd_event (EV_P_ int fd, int revents);
  339. void ev_feed_signal_event (EV_P_ int signum);
  340. void ev_io_start (EV_P_ ev_io *w);
  341. void ev_io_stop (EV_P_ ev_io *w);
  342. void ev_timer_start (EV_P_ ev_timer *w);
  343. void ev_timer_stop (EV_P_ ev_timer *w);
  344. /* stops if active and no repeat, restarts if active and repeating, starts if inactive and repeating */
  345. void ev_timer_again (EV_P_ ev_timer *w);
  346. #if EV_PERIODICS
  347. void ev_periodic_start (EV_P_ ev_periodic *w);
  348. void ev_periodic_stop (EV_P_ ev_periodic *w);
  349. void ev_periodic_again (EV_P_ ev_periodic *w);
  350. #endif
  351. void ev_idle_start (EV_P_ ev_idle *w);
  352. void ev_idle_stop (EV_P_ ev_idle *w);
  353. void ev_prepare_start (EV_P_ ev_prepare *w);
  354. void ev_prepare_stop (EV_P_ ev_prepare *w);
  355. void ev_check_start (EV_P_ ev_check *w);
  356. void ev_check_stop (EV_P_ ev_check *w);
  357. /* only supported in the default loop */
  358. void ev_signal_start (EV_P_ ev_signal *w);
  359. void ev_signal_stop (EV_P_ ev_signal *w);
  360. /* only supported in the default loop */
  361. void ev_child_start (EV_P_ ev_child *w);
  362. void ev_child_stop (EV_P_ ev_child *w);
  363. # if EV_MULTIPLICITY
  364. /* only supported when loop to be embedded is in fact embeddable */
  365. void ev_embed_start (EV_P_ ev_embed *w);
  366. void ev_embed_stop (EV_P_ ev_embed *w);
  367. void ev_embed_sweep (EV_P_ ev_embed *w);
  368. # endif
  369. #endif
  370. #ifdef __cplusplus
  371. }
  372. #endif
  373. #endif