verto.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * Copyright 2011 Red Hat, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #ifndef VERTO_H_
  25. #define VERTO_H_
  26. #include <time.h> /* For time_t */
  27. #include <unistd.h> /* For pid_t */
  28. #ifdef WIN32
  29. #include <windows.h>
  30. typedef HANDLE verto_proc;
  31. typedef DWORD verto_proc_status;
  32. #else
  33. typedef pid_t verto_proc;
  34. typedef int verto_proc_status;
  35. #endif
  36. #define VERTO_SIG_IGN ((verto_callback *) 1)
  37. typedef struct verto_ctx verto_ctx;
  38. typedef struct verto_ev verto_ev;
  39. typedef enum {
  40. VERTO_EV_TYPE_NONE = 0,
  41. VERTO_EV_TYPE_IO = 1,
  42. VERTO_EV_TYPE_TIMEOUT = 1 << 1,
  43. VERTO_EV_TYPE_IDLE = 1 << 2,
  44. VERTO_EV_TYPE_SIGNAL = 1 << 3,
  45. VERTO_EV_TYPE_CHILD = 1 << 4
  46. } verto_ev_type;
  47. typedef enum {
  48. VERTO_EV_FLAG_NONE = 0,
  49. VERTO_EV_FLAG_PERSIST = 1,
  50. VERTO_EV_FLAG_PRIORITY_LOW = 1 << 1,
  51. VERTO_EV_FLAG_PRIORITY_MEDIUM = 1 << 2,
  52. VERTO_EV_FLAG_PRIORITY_HIGH = 1 << 3,
  53. VERTO_EV_FLAG_IO_READ = 1 << 4,
  54. VERTO_EV_FLAG_IO_WRITE = 1 << 5,
  55. VERTO_EV_FLAG_REINITIABLE = 1 << 6,
  56. _VERTO_EV_FLAG_MAX = VERTO_EV_FLAG_REINITIABLE
  57. } verto_ev_flag;
  58. typedef void (verto_callback)(verto_ctx *ctx, verto_ev *ev);
  59. /**
  60. * Creates a new event context using an optionally specified implementation
  61. * and/or optionally specified required features.
  62. *
  63. * If you are an application that has already decided on using a particular
  64. * event loop implementation, you should not call this function, but instead
  65. * import the verto-NAME.h header and link against the verto-NAME.so, where
  66. * NAME is the implementation you wish to use.
  67. *
  68. * If you are a library, you should generally avoid creating event contexts
  69. * on your own but allow applications to pass in a verto_ctx you can use.
  70. *
  71. * There are two cases where you should use this function. The first is
  72. * where you have a need to choose an implementation at run time, usually
  73. * for testing purposes. The second and more common is when you simply
  74. * wish to remain implementation agnostic. In this later case, you should
  75. * always call like this: verto_new(NULL, ...). This lets verto choose the best
  76. * implementation to use.
  77. *
  78. * If impl is not NULL, a new context is returned which is backed by the
  79. * implementation specified. If the implementation specified is not
  80. * available or if the required types (reqtypes) are not provided by the
  81. * named implementation, NULL is returned. The parameter 'impl' can specify:
  82. * * The full path to an implementation library
  83. * * The name of the implementation library (i.e. - "glib" or "libev")
  84. *
  85. * If impl is NULL, verto will attempt to automatically determine the
  86. * best implementation to use.
  87. *
  88. * First, verto will attempt to use an existing, previously loaded
  89. * implementation. This is handled automatically by internal caching of either
  90. * the first implementation loaded or the one specified by verto_set_default().
  91. *
  92. * Second, verto will attempt to discern if you are already linked to any
  93. * of the supported implementations (to avoid wasting memory by loading
  94. * extra unnecessary libraries). If you are linked to one supported
  95. * implementation, that implementation will be chosen. If you are linked
  96. * to more than one supported implementation one of the ones linked to
  97. * will be chosen, but the order of the particular choice is undefined.
  98. *
  99. * Third, verto will attempt to load the compile-time default, if defined at
  100. * build time and available at runtime.
  101. *
  102. * Last, verto will attempt to load any implementation installed. The specific
  103. * order of this step is undefined.
  104. *
  105. * In all cases above, if the implementation does not support all the specified
  106. * features (reqtypes), it will be skipped and processing will continue from
  107. * where it left off. This means that if verto_new() returns non-NULL it is
  108. * guaranteed to support the features you specified.
  109. *
  110. * @see verto_set_default()
  111. * @param impl The implementation to use, or NULL.
  112. * @param reqtypes A bitwise or'd list of required event type features.
  113. * @return A new verto_ctx, or NULL on error. Call verto_free() when done.
  114. */
  115. verto_ctx *
  116. verto_new(const char *impl, verto_ev_type reqtypes);
  117. /**
  118. * Gets the default event context using an optionally specified implementation.
  119. *
  120. * This function is essentially a singleton version of verto_new(). However,
  121. * since this function must return the same loop as the *_default() call of
  122. * the underlying implementation (if such a function exists), it is NOT a
  123. * global singleton, but a per-implementation singleton. For this reason, you
  124. * must call verto_free() when you are done with this loop. Even after calling
  125. * verto_free() on the default verto_ctx, you can safely call verto_default()
  126. * again and receive a new reference to the same (internally default) loop.
  127. *
  128. * In all other respects, verto_default() acts exactly like verto_new().
  129. *
  130. * @see verto_new()
  131. * @see verto_free()
  132. * @param impl The implementation to use, or NULL.
  133. * @param reqtypes A bitwise or'd list of required event type features.
  134. * @return The default verto_ctx, or NULL on error. Call verto_free() when done.
  135. */
  136. verto_ctx *
  137. verto_default(const char *impl, verto_ev_type reqtypes);
  138. /**
  139. * Sets the default implementation to use by its name.
  140. *
  141. * This function returns 1 on success and 0 on failure. It can fail for the
  142. * following reasons:
  143. * 1. The default implementation was already set via verto_set_default().
  144. * 2. The implementation specified could not be found.
  145. * 3. The implementation specified didn't support the features specified.
  146. * 4. The impl argument was NULL.
  147. * 5. verto_new() was already called.
  148. * 6. verto_default() was already called.
  149. * 7. verto_new_NAME() was already called.
  150. * 8. verto_default_NAME() was already called.
  151. * 9. verto_convert_NAME() was already called.
  152. *
  153. * @see verto_new()
  154. * @see verto_default()
  155. * @param impl The implementation to use.
  156. * @param reqtypes A bitwise or'd list of required event type features.
  157. * @return The default verto_ctx, or NULL on error. Call verto_free() when done.
  158. */
  159. int
  160. verto_set_default(const char *impl, verto_ev_type reqtypes);
  161. /**
  162. * Frees a verto_ctx.
  163. *
  164. * When called on a default verto_ctx, the reference will be freed but the
  165. * internal default loop will still be available via another call to
  166. * verto_default().
  167. *
  168. * @see verto_new()
  169. * @see verto_default()
  170. * @param ctx The verto_ctx to free.
  171. */
  172. void
  173. verto_free(verto_ctx *ctx);
  174. /**
  175. * Run the verto_ctx forever, or at least until verto_break() is called.
  176. *
  177. * @see verto_break()
  178. * @param ctx The verto_ctx to run.
  179. */
  180. void
  181. verto_run(verto_ctx *ctx);
  182. /**
  183. * Run the verto_ctx once. May block.
  184. *
  185. * @param ctx The verto_ctx to run once.
  186. */
  187. void
  188. verto_run_once(verto_ctx *ctx);
  189. /**
  190. * Exits the currently running verto_ctx.
  191. *
  192. * @see verto_run()
  193. * @param ctx The verto_ctx to exit.
  194. */
  195. void
  196. verto_break(verto_ctx *ctx);
  197. /**
  198. * Re-initializes the verto_ctx.
  199. *
  200. * This function deletes all events, except those which have set the
  201. * VERTO_EV_FLAG_REINITIABLE flag. If you fork(), you MUST call this in the
  202. * child process after the fork!
  203. *
  204. * If this function fails it indicates that at least one
  205. * VERTO_EV_FLAG_REINITIABLE event was not rearmed or that ctx was NULL.
  206. *
  207. * @see verto_new()
  208. * @see verto_default()
  209. * @param ctx The verto_ctx to re-initialize.
  210. * @return Non-zero on success, 0 on error.
  211. */
  212. int
  213. verto_reinitialize(verto_ctx *ctx);
  214. /**
  215. * Adds a callback executed when a file descriptor is ready to be read/written.
  216. *
  217. * All verto_ev events are automatically freed when their parent verto_ctx is
  218. * freed. You do not need to free them manually. If VERTO_EV_FLAG_PERSIST is
  219. * provided, the event will repeat until verto_del() is called. If
  220. * VERTO_EV_FLAG_PERSIST is not provided, the event will be freed automatically
  221. * after its execution. In either case, you may call verto_del() at any time
  222. * to prevent the event from executing.
  223. *
  224. * NOTE: On Windows, the underlying select() only works with sockets. As such,
  225. * any attempt to add a non-socket io event on Windows will produce undefined
  226. * results and may even crash.
  227. *
  228. * @see verto_del()
  229. * @param ctx The verto_ctx which will fire the callback.
  230. * @param flags The flags to set (at least one VERTO_EV_FLAG_IO* required).
  231. * @param callback The callback to fire.
  232. * @param fd The file descriptor to watch for reads.
  233. * @return The verto_ev registered with the event context or NULL on error.
  234. */
  235. verto_ev *
  236. verto_add_io(verto_ctx *ctx, verto_ev_flag flags,
  237. verto_callback *callback, int fd);
  238. /**
  239. * Adds a callback executed after a period of time.
  240. *
  241. * All verto_ev events are automatically freed when their parent verto_ctx is
  242. * freed. You do not need to free them manually. If VERTO_EV_FLAG_PERSIST is
  243. * provided, the event will repeat until verto_del() is called. If
  244. * VERTO_EV_FLAG_PERSIST is not provided, the event will be freed automatically
  245. * after its execution. In either case, you may call verto_del() at any time
  246. * to prevent the event from executing.
  247. *
  248. * @see verto_del()
  249. * @param ctx The verto_ctx which will fire the callback.
  250. * @param flags The flags to set.
  251. * @param callback The callback to fire.
  252. * @param interval Time period to wait before firing (in milliseconds).
  253. * @return The verto_ev registered with the event context.
  254. */
  255. verto_ev *
  256. verto_add_timeout(verto_ctx *ctx, verto_ev_flag flags,
  257. verto_callback *callback, time_t interval);
  258. /**
  259. * Adds a callback executed when there is nothing else to do.
  260. *
  261. * All verto_ev events are automatically freed when their parent verto_ctx is
  262. * freed. You do not need to free them manually. If VERTO_EV_FLAG_PERSIST is
  263. * provided, the event will repeat until verto_del() is called. If
  264. * VERTO_EV_FLAG_PERSIST is not provided, the event will be freed automatically
  265. * after its execution. In either case, you may call verto_del() at any time
  266. * to prevent the event from executing.
  267. *
  268. * @see verto_del()
  269. * @param ctx The verto_ctx which will fire the callback.
  270. * @param flags The flags to set.
  271. * @param callback The callback to fire.
  272. * @return The verto_ev registered with the event context.
  273. */
  274. verto_ev *
  275. verto_add_idle(verto_ctx *ctx, verto_ev_flag flags,
  276. verto_callback *callback);
  277. /**
  278. * Adds a callback executed when a signal is received.
  279. *
  280. * All verto_ev events are automatically freed when their parent verto_ctx is
  281. * freed. You do not need to free them manually. If VERTO_EV_FLAG_PERSIST is
  282. * provided, the event will repeat until verto_del() is called. If
  283. * VERTO_EV_FLAG_PERSIST is not provided, the event will be freed automatically
  284. * after its execution. In either case, you may call verto_del() at any time
  285. * to prevent the event from executing.
  286. *
  287. * NOTE: If you attempt to ignore a signal without the VERTO_EV_FLAG_PERSIST
  288. * flag, this function fails.
  289. *
  290. * NOTE: SIGCHLD is expressly not supported. If you want this notification,
  291. * please use verto_add_child().
  292. *
  293. * WARNNIG: Signal events can only be reliably received in the default verto_ctx
  294. * in some implementations. Attempting to receive signal events in non-default
  295. * loops may result in assert() failures.
  296. *
  297. * WARNING: While verto does its best to protect you from crashes, there is
  298. * essentially no way to do signal events if you mix multiple implementations in
  299. * a single process. Attempting to do so will result in undefined behavior,
  300. * and potentially even a crash. You have been warned.
  301. *
  302. * @see verto_add_child()
  303. * @see verto_repeat()
  304. * @see verto_del()
  305. * @param ctx The verto_ctx which will fire the callback.
  306. * @param flags The flags to set.
  307. * @param callback The callback to fire.
  308. * @param signal The signal to watch for.
  309. * @return The verto_ev registered with the event context.
  310. */
  311. verto_ev *
  312. verto_add_signal(verto_ctx *ctx, verto_ev_flag flags,
  313. verto_callback *callback, int signal);
  314. /**
  315. * Adds a callback executed when a child process exits.
  316. *
  317. * This event will be freed automatically after its execution. Due to the
  318. * nature of a process' life-cycle, child events cannot persist (processes only
  319. * exit once). This function returns NULL if you attempt to use
  320. * VERTO_EV_FLAG_PERSIST. You may, of course, call verto_del() at any time to
  321. * prevent the callback from firing.
  322. *
  323. * @see verto_del()
  324. * @param ctx The verto_ctx which will fire the callback.
  325. * @param flags The flags to set.
  326. * @param callback The callback to fire.
  327. * @param child The pid (POSIX) or handle (Win32) of the child to watch for.
  328. * @return The verto_ev registered with the event context.
  329. */
  330. verto_ev *
  331. verto_add_child(verto_ctx *ctx, verto_ev_flag flags,
  332. verto_callback *callback, verto_proc proc);
  333. /**
  334. * Sets the private pointer of the verto_ev.
  335. *
  336. * The free callback will be called in two cases:
  337. * 1. When the event is deleted (manually or automatically)
  338. * 2. When verto_set_private() is called again, unless
  339. * free is NULL.
  340. *
  341. * @see verto_get_private()
  342. * @param ev The verto_ev
  343. * @param priv The private value to store
  344. * @param free The callback used to free the data or NULL
  345. */
  346. void
  347. verto_set_private(verto_ev *ev, void *priv, verto_callback *free);
  348. /**
  349. * Gets the private pointer of the verto_ev.
  350. *
  351. * @see verto_set_private()
  352. * @param ev The verto_ev
  353. * @return The verto_ev private pointer
  354. */
  355. void *
  356. verto_get_private(const verto_ev *ev);
  357. /**
  358. * Gets the type of the verto_ev.
  359. *
  360. * @see verto_add_io()
  361. * @see verto_add_timeout()
  362. * @see verto_add_idle()
  363. * @see verto_add_signal()
  364. * @see verto_add_child()
  365. * @param ev The verto_ev
  366. * @return The verto_ev type
  367. */
  368. verto_ev_type
  369. verto_get_type(const verto_ev *ev);
  370. /**
  371. * Gets the flags associated with the given verto_ev.
  372. *
  373. * @see verto_add_io()
  374. * @see verto_add_timeout()
  375. * @see verto_add_idle()
  376. * @see verto_add_signal()
  377. * @see verto_add_child()
  378. * @param ev The verto_ev
  379. * @return The verto_ev type
  380. */
  381. verto_ev_flag
  382. verto_get_flags(const verto_ev *ev);
  383. /**
  384. * Gets the file descriptor associated with a read/write verto_ev.
  385. *
  386. * @see verto_add_io()
  387. * @param ev The verto_ev to retrieve the file descriptor from.
  388. * @return The file descriptor, or -1 if not a read/write event.
  389. */
  390. int
  391. verto_get_fd(const verto_ev *ev);
  392. /**
  393. * Gets the interval associated with a timeout verto_ev.
  394. *
  395. * @see verto_add_timeout()
  396. * @param ev The verto_ev to retrieve the interval from.
  397. * @return The interval, or 0 if not a timeout event.
  398. */
  399. time_t
  400. verto_get_interval(const verto_ev *ev);
  401. /**
  402. * Gets the signal associated with a signal verto_ev.
  403. *
  404. * @see verto_add_signal()
  405. * @param ev The verto_ev to retrieve the signal from.
  406. * @return The signal, or -1 if not a signal event.
  407. */
  408. int
  409. verto_get_signal(const verto_ev *ev);
  410. /**
  411. * Gets the process associated with a child verto_ev.
  412. *
  413. * @see verto_add_child()
  414. * @param ev The verto_ev to retrieve the process from.
  415. * @return The pid/handle, or 0/NULL if not a child event (POSIX/Win32).
  416. */
  417. verto_proc
  418. verto_get_proc(const verto_ev *ev);
  419. /**
  420. * Gets the status of the process which caused this event to fire.
  421. *
  422. * @see verto_add_child()
  423. * @param ev The verto_ev to retrieve the status from.
  424. * @return The pid/handle status.
  425. */
  426. verto_proc_status
  427. verto_get_proc_status(const verto_ev *ev);
  428. /**
  429. * Removes an event from from the event context and frees it.
  430. *
  431. * The event and its contents cannot be used after this call.
  432. *
  433. * @see verto_add_io()
  434. * @see verto_add_timeout()
  435. * @see verto_add_idle()
  436. * @see verto_add_signal()
  437. * @see verto_add_child()
  438. * @param ev The event to delete.
  439. */
  440. void
  441. verto_del(verto_ev *ev);
  442. /**
  443. * Returns the event types supported by this implementation.
  444. *
  445. * @param ctx The verto_ctx to query.
  446. * @return The event types supported.
  447. */
  448. verto_ev_type
  449. verto_get_supported_types(verto_ctx *ctx);
  450. #endif /* VERTO_H_ */