verto.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. #include <sys/types.h>
  34. typedef pid_t verto_proc;
  35. typedef int verto_proc_status;
  36. #endif
  37. #define VERTO_SIG_IGN ((verto_callback *) 1)
  38. #ifdef __cplusplus
  39. extern "C"
  40. {
  41. #endif /* __cplusplus */
  42. typedef struct verto_ctx verto_ctx;
  43. typedef struct verto_ev verto_ev;
  44. typedef enum {
  45. VERTO_EV_TYPE_NONE = 0,
  46. VERTO_EV_TYPE_IO = 1,
  47. VERTO_EV_TYPE_TIMEOUT = 1 << 1,
  48. VERTO_EV_TYPE_IDLE = 1 << 2,
  49. VERTO_EV_TYPE_SIGNAL = 1 << 3,
  50. VERTO_EV_TYPE_CHILD = 1 << 4
  51. } verto_ev_type;
  52. typedef enum {
  53. VERTO_EV_FLAG_NONE = 0,
  54. VERTO_EV_FLAG_PERSIST = 1,
  55. VERTO_EV_FLAG_PRIORITY_LOW = 1 << 1,
  56. VERTO_EV_FLAG_PRIORITY_MEDIUM = 1 << 2,
  57. VERTO_EV_FLAG_PRIORITY_HIGH = 1 << 3,
  58. VERTO_EV_FLAG_IO_READ = 1 << 4,
  59. VERTO_EV_FLAG_IO_WRITE = 1 << 5,
  60. VERTO_EV_FLAG_IO_ERROR = 1 << 7,
  61. VERTO_EV_FLAG_IO_CLOSE_FD = 1 << 8,
  62. VERTO_EV_FLAG_REINITIABLE = 1 << 6,
  63. _VERTO_EV_FLAG_MUTABLE_MASK = VERTO_EV_FLAG_PRIORITY_LOW
  64. | VERTO_EV_FLAG_PRIORITY_MEDIUM
  65. | VERTO_EV_FLAG_PRIORITY_HIGH
  66. | VERTO_EV_FLAG_IO_READ
  67. | VERTO_EV_FLAG_IO_WRITE,
  68. _VERTO_EV_FLAG_MAX = VERTO_EV_FLAG_IO_CLOSE_FD
  69. } verto_ev_flag;
  70. typedef void (verto_callback)(verto_ctx *ctx, verto_ev *ev);
  71. /**
  72. * Creates a new event context using an optionally specified implementation
  73. * and/or optionally specified required features.
  74. *
  75. * If you are an application that has already decided on using a particular
  76. * event loop implementation, you should not call this function, but instead
  77. * import the verto-NAME.h header and link against the verto-NAME.so, where
  78. * NAME is the implementation you wish to use.
  79. *
  80. * If you are a library, you should generally avoid creating event contexts
  81. * on your own but allow applications to pass in a verto_ctx you can use.
  82. *
  83. * There are two cases where you should use this function. The first is
  84. * where you have a need to choose an implementation at run time, usually
  85. * for testing purposes. The second and more common is when you simply
  86. * wish to remain implementation agnostic. In this later case, you should
  87. * always call like this: verto_new(NULL, ...). This lets verto choose the best
  88. * implementation to use.
  89. *
  90. * If impl is not NULL, a new context is returned which is backed by the
  91. * implementation specified. If the implementation specified is not
  92. * available or if the required types (reqtypes) are not provided by the
  93. * named implementation, NULL is returned. The parameter 'impl' can specify:
  94. * * The full path to an implementation library
  95. * * The name of the implementation library (i.e. - "glib" or "libev")
  96. *
  97. * If impl is NULL, verto will attempt to automatically determine the
  98. * best implementation to use.
  99. *
  100. * First, verto will attempt to use an existing, previously loaded
  101. * implementation. This is handled automatically by internal caching of either
  102. * the first implementation loaded or the one specified by verto_set_default().
  103. *
  104. * Second, verto will attempt to discern if you are already linked to any
  105. * of the supported implementations (to avoid wasting memory by loading
  106. * extra unnecessary libraries). If you are linked to one supported
  107. * implementation, that implementation will be chosen. If you are linked
  108. * to more than one supported implementation one of the ones linked to
  109. * will be chosen, but the order of the particular choice is undefined.
  110. *
  111. * Third, verto will attempt to load the compile-time default, if defined at
  112. * build time and available at runtime.
  113. *
  114. * Last, verto will attempt to load any implementation installed. The specific
  115. * order of this step is undefined.
  116. *
  117. * In all cases above, if the implementation does not support all the specified
  118. * features (reqtypes), it will be skipped and processing will continue from
  119. * where it left off. This means that if verto_new() returns non-NULL it is
  120. * guaranteed to support the features you specified.
  121. *
  122. * @see verto_set_default()
  123. * @param impl The implementation to use, or NULL.
  124. * @param reqtypes A bitwise or'd list of required event type features.
  125. * @return A new verto_ctx, or NULL on error. Call verto_free() when done.
  126. */
  127. verto_ctx *
  128. verto_new(const char *impl, verto_ev_type reqtypes);
  129. /**
  130. * Gets the default event context using an optionally specified implementation.
  131. *
  132. * This function is essentially a singleton version of verto_new(). However,
  133. * since this function must return the same loop as the *_default() call of
  134. * the underlying implementation (if such a function exists), it is NOT a
  135. * global singleton, but a per-implementation singleton. For this reason, you
  136. * must call verto_free() when you are done with this loop. Even after calling
  137. * verto_free() on the default verto_ctx, you can safely call verto_default()
  138. * again and receive a new reference to the same (internally default) loop.
  139. *
  140. * In all other respects, verto_default() acts exactly like verto_new().
  141. *
  142. * @see verto_new()
  143. * @see verto_free()
  144. * @param impl The implementation to use, or NULL.
  145. * @param reqtypes A bitwise or'd list of required event type features.
  146. * @return The default verto_ctx, or NULL on error. Call verto_free() when done.
  147. */
  148. verto_ctx *
  149. verto_default(const char *impl, verto_ev_type reqtypes);
  150. /**
  151. * Sets the default implementation to use by its name.
  152. *
  153. * This function returns 1 on success and 0 on failure. It can fail for the
  154. * following reasons:
  155. * 1. The default implementation was already set via verto_set_default().
  156. * 2. The implementation specified could not be found.
  157. * 3. The implementation specified didn't support the features specified.
  158. * 4. The impl argument was NULL.
  159. * 5. verto_new() was already called.
  160. * 6. verto_default() was already called.
  161. * 7. verto_new_NAME() was already called.
  162. * 8. verto_default_NAME() was already called.
  163. * 9. verto_convert_NAME() was already called.
  164. *
  165. * @see verto_new()
  166. * @see verto_default()
  167. * @param impl The implementation to use.
  168. * @param reqtypes A bitwise or'd list of required event type features.
  169. * @return The default verto_ctx, or NULL on error. Call verto_free() when done.
  170. */
  171. int
  172. verto_set_default(const char *impl, verto_ev_type reqtypes);
  173. /**
  174. * Sets the allocator to use for verto_ctx and verto_ev objects.
  175. *
  176. * If you plan to set the allocator, you MUST call this function before any
  177. * other verto_*() calls.
  178. *
  179. * @see verto_new()
  180. * @see verto_default()
  181. * @see verto_add_io()
  182. * @see verto_add_timeout()
  183. * @see verto_add_idle()
  184. * @see verto_add_signal()
  185. * @see verto_add_child()
  186. * @param resize The allocator to use (behaves like realloc();
  187. * resize(ptr, 0) must free memory at ptr.)
  188. * @param hierarchical Zero if the allocator is not hierarchical
  189. */
  190. int
  191. verto_set_allocator(void *(*resize)(void *mem, size_t size), int hierarchical);
  192. /**
  193. * Frees a verto_ctx.
  194. *
  195. * When called on a default verto_ctx, the reference will be freed but the
  196. * internal default loop will still be available via another call to
  197. * verto_default().
  198. *
  199. * @see verto_new()
  200. * @see verto_default()
  201. * @param ctx The verto_ctx to free.
  202. */
  203. void
  204. verto_free(verto_ctx *ctx);
  205. /**
  206. * Frees global state.
  207. *
  208. * Remove and free all allocated global state. Call only when no further
  209. * contexts exist and all threads have exited.
  210. *
  211. * @see verto_new()
  212. * @see verto_free()
  213. * @see verto_default()
  214. */
  215. void
  216. verto_cleanup(void);
  217. /**
  218. * Run the verto_ctx forever, or at least until verto_break() is called.
  219. *
  220. * @see verto_break()
  221. * @param ctx The verto_ctx to run.
  222. */
  223. void
  224. verto_run(verto_ctx *ctx);
  225. /**
  226. * Run the verto_ctx once. May block.
  227. *
  228. * @param ctx The verto_ctx to run once.
  229. */
  230. void
  231. verto_run_once(verto_ctx *ctx);
  232. /**
  233. * Exits the currently running verto_ctx.
  234. *
  235. * @see verto_run()
  236. * @param ctx The verto_ctx to exit.
  237. */
  238. void
  239. verto_break(verto_ctx *ctx);
  240. /**
  241. * Re-initializes the verto_ctx.
  242. *
  243. * This function deletes all events, except those which have set the
  244. * VERTO_EV_FLAG_REINITIABLE flag. If you fork(), you MUST call this in the
  245. * child process after the fork!
  246. *
  247. * If this function fails it indicates that at least one
  248. * VERTO_EV_FLAG_REINITIABLE event was not rearmed or that ctx was NULL.
  249. *
  250. * @see verto_new()
  251. * @see verto_default()
  252. * @param ctx The verto_ctx to re-initialize.
  253. * @return Non-zero on success, 0 on error.
  254. */
  255. int
  256. verto_reinitialize(verto_ctx *ctx);
  257. /**
  258. * Adds a callback executed when a file descriptor is ready to be read/written.
  259. *
  260. * All verto_ev events are automatically freed when their parent verto_ctx is
  261. * freed. You do not need to free them manually. If VERTO_EV_FLAG_PERSIST is
  262. * provided, the event will repeat until verto_del() is called. If
  263. * VERTO_EV_FLAG_PERSIST is not provided, the event will be freed automatically
  264. * after its execution. In either case, you may call verto_del() at any time
  265. * to prevent the event from executing.
  266. * If VERTO_EV_FLAG_IO_CLOSE_FD is provided the passed in fd is automatically
  267. * closed when the event is freed with verto_del()
  268. *
  269. * NOTE: On Windows, the underlying select() only works with sockets. As such,
  270. * any attempt to add a non-socket io event on Windows will produce undefined
  271. * results and may even crash.
  272. *
  273. * @see verto_del()
  274. * @param ctx The verto_ctx which will fire the callback.
  275. * @param flags The flags to set (at least one VERTO_EV_FLAG_IO* required).
  276. * @param callback The callback to fire.
  277. * @param fd The file descriptor to watch for reads.
  278. * @return The verto_ev registered with the event context or NULL on error.
  279. */
  280. verto_ev *
  281. verto_add_io(verto_ctx *ctx, verto_ev_flag flags,
  282. verto_callback *callback, int fd);
  283. /**
  284. * Adds a callback executed after a period of time.
  285. *
  286. * All verto_ev events are automatically freed when their parent verto_ctx is
  287. * freed. You do not need to free them manually. If VERTO_EV_FLAG_PERSIST is
  288. * provided, the event will repeat until verto_del() is called. If
  289. * VERTO_EV_FLAG_PERSIST is not provided, the event will be freed automatically
  290. * after its execution. In either case, you may call verto_del() at any time
  291. * to prevent the event from executing.
  292. *
  293. * @see verto_del()
  294. * @param ctx The verto_ctx which will fire the callback.
  295. * @param flags The flags to set.
  296. * @param callback The callback to fire.
  297. * @param interval Time period to wait before firing (in milliseconds).
  298. * @return The verto_ev registered with the event context.
  299. */
  300. verto_ev *
  301. verto_add_timeout(verto_ctx *ctx, verto_ev_flag flags,
  302. verto_callback *callback, time_t interval);
  303. /**
  304. * Adds a callback executed when there is nothing else to do.
  305. *
  306. * All verto_ev events are automatically freed when their parent verto_ctx is
  307. * freed. You do not need to free them manually. If VERTO_EV_FLAG_PERSIST is
  308. * provided, the event will repeat until verto_del() is called. If
  309. * VERTO_EV_FLAG_PERSIST is not provided, the event will be freed automatically
  310. * after its execution. In either case, you may call verto_del() at any time
  311. * to prevent the event from executing.
  312. *
  313. * @see verto_del()
  314. * @param ctx The verto_ctx which will fire the callback.
  315. * @param flags The flags to set.
  316. * @param callback The callback to fire.
  317. * @return The verto_ev registered with the event context.
  318. */
  319. verto_ev *
  320. verto_add_idle(verto_ctx *ctx, verto_ev_flag flags,
  321. verto_callback *callback);
  322. /**
  323. * Adds a callback executed when a signal is received.
  324. *
  325. * All verto_ev events are automatically freed when their parent verto_ctx is
  326. * freed. You do not need to free them manually. If VERTO_EV_FLAG_PERSIST is
  327. * provided, the event will repeat until verto_del() is called. If
  328. * VERTO_EV_FLAG_PERSIST is not provided, the event will be freed automatically
  329. * after its execution. In either case, you may call verto_del() at any time
  330. * to prevent the event from executing.
  331. *
  332. * NOTE: If you attempt to ignore a signal without the VERTO_EV_FLAG_PERSIST
  333. * flag, this function fails.
  334. *
  335. * NOTE: SIGCHLD is expressly not supported. If you want this notification,
  336. * please use verto_add_child().
  337. *
  338. * WARNNIG: Signal events can only be reliably received in the default verto_ctx
  339. * in some implementations. Attempting to receive signal events in non-default
  340. * loops may result in assert() failures.
  341. *
  342. * WARNING: While verto does its best to protect you from crashes, there is
  343. * essentially no way to do signal events if you mix multiple implementations in
  344. * a single process. Attempting to do so will result in undefined behavior,
  345. * and potentially even a crash. You have been warned.
  346. *
  347. * @see verto_add_child()
  348. * @see verto_repeat()
  349. * @see verto_del()
  350. * @param ctx The verto_ctx which will fire the callback.
  351. * @param flags The flags to set.
  352. * @param callback The callback to fire.
  353. * @param signal The signal to watch for.
  354. * @return The verto_ev registered with the event context.
  355. */
  356. verto_ev *
  357. verto_add_signal(verto_ctx *ctx, verto_ev_flag flags,
  358. verto_callback *callback, int signal);
  359. /**
  360. * Adds a callback executed when a child process exits.
  361. *
  362. * This event will be freed automatically after its execution. Due to the
  363. * nature of a process' life-cycle, child events cannot persist (processes only
  364. * exit once). This function returns NULL if you attempt to use
  365. * VERTO_EV_FLAG_PERSIST. You may, of course, call verto_del() at any time to
  366. * prevent the callback from firing.
  367. *
  368. * @see verto_del()
  369. * @param ctx The verto_ctx which will fire the callback.
  370. * @param flags The flags to set.
  371. * @param callback The callback to fire.
  372. * @param child The pid (POSIX) or handle (Win32) of the child to watch for.
  373. * @return The verto_ev registered with the event context.
  374. */
  375. verto_ev *
  376. verto_add_child(verto_ctx *ctx, verto_ev_flag flags,
  377. verto_callback *callback, verto_proc proc);
  378. /**
  379. * Sets the private pointer of the verto_ev.
  380. *
  381. * The free callback will be called in two cases:
  382. * 1. When the event is deleted (manually or automatically)
  383. * 2. When verto_set_private() is called again, unless
  384. * free is NULL.
  385. *
  386. * @see verto_get_private()
  387. * @param ev The verto_ev
  388. * @param priv The private value to store
  389. * @param free The callback used to free the data or NULL
  390. */
  391. void
  392. verto_set_private(verto_ev *ev, void *priv, verto_callback *free);
  393. /**
  394. * Gets the private pointer of the verto_ev.
  395. *
  396. * @see verto_set_private()
  397. * @param ev The verto_ev
  398. * @return The verto_ev private pointer
  399. */
  400. void *
  401. verto_get_private(const verto_ev *ev);
  402. /**
  403. * Gets the type of the verto_ev.
  404. *
  405. * @see verto_add_io()
  406. * @see verto_add_timeout()
  407. * @see verto_add_idle()
  408. * @see verto_add_signal()
  409. * @see verto_add_child()
  410. * @param ev The verto_ev
  411. * @return The verto_ev type
  412. */
  413. verto_ev_type
  414. verto_get_type(const verto_ev *ev);
  415. /**
  416. * Gets the flags associated with the given verto_ev.
  417. *
  418. * @see verto_add_io()
  419. * @see verto_add_timeout()
  420. * @see verto_add_idle()
  421. * @see verto_add_signal()
  422. * @see verto_add_child()
  423. * @see verto_set_flags()
  424. * @param ev The verto_ev
  425. * @return The verto_ev type
  426. */
  427. verto_ev_flag
  428. verto_get_flags(const verto_ev *ev);
  429. /**
  430. * Sets the flags associated with the given verto_ev.
  431. *
  432. * See _VERTO_EV_FLAG_MUTABLE_MASK for the flags that can be changed
  433. * with this function. All others will be ignored. If the flags specified
  434. * are the same as the flags the event already has, this function is a no-op.
  435. *
  436. * @see verto_add_io()
  437. * @see verto_add_timeout()
  438. * @see verto_add_idle()
  439. * @see verto_add_signal()
  440. * @see verto_add_child()
  441. * @see verto_get_flags()
  442. * @param ev The verto_ev
  443. * @param flags The flags for the event
  444. */
  445. void
  446. verto_set_flags(verto_ev *ev, verto_ev_flag flags);
  447. /**
  448. * Gets the file descriptor associated with a read/write verto_ev.
  449. *
  450. * @see verto_add_io()
  451. * @param ev The verto_ev to retrieve the file descriptor from.
  452. * @return The file descriptor, or -1 if not a read/write event.
  453. */
  454. int
  455. verto_get_fd(const verto_ev *ev);
  456. /**
  457. * Gets the file descriptor state from when the event fires.
  458. *
  459. * @see verto_add_io()
  460. * @param ev The verto_ev to retrieve the fd state from.
  461. * @return The fd state.
  462. */
  463. verto_ev_flag
  464. verto_get_fd_state(const verto_ev *ev);
  465. /**
  466. * Gets the interval associated with a timeout verto_ev.
  467. *
  468. * @see verto_add_timeout()
  469. * @param ev The verto_ev to retrieve the interval from.
  470. * @return The interval, or 0 if not a timeout event.
  471. */
  472. time_t
  473. verto_get_interval(const verto_ev *ev);
  474. /**
  475. * Gets the signal associated with a signal verto_ev.
  476. *
  477. * @see verto_add_signal()
  478. * @param ev The verto_ev to retrieve the signal from.
  479. * @return The signal, or -1 if not a signal event.
  480. */
  481. int
  482. verto_get_signal(const verto_ev *ev);
  483. /**
  484. * Gets the process associated with a child verto_ev.
  485. *
  486. * @see verto_add_child()
  487. * @param ev The verto_ev to retrieve the process from.
  488. * @return The pid/handle, or 0/NULL if not a child event (POSIX/Win32).
  489. */
  490. verto_proc
  491. verto_get_proc(const verto_ev *ev);
  492. /**
  493. * Gets the status of the process which caused this event to fire.
  494. *
  495. * @see verto_add_child()
  496. * @param ev The verto_ev to retrieve the status from.
  497. * @return The pid/handle status.
  498. */
  499. verto_proc_status
  500. verto_get_proc_status(const verto_ev *ev);
  501. /**
  502. * Gets the verto_ctx associated with a verto_ev.
  503. *
  504. * This is a borrowed reference, don't attempt to free it!
  505. *
  506. * @param ev The verto_ev to retrieve the verto_ctx from.
  507. * @return The verto_ctx.
  508. */
  509. verto_ctx *
  510. verto_get_ctx(const verto_ev *ev);
  511. /**
  512. * Removes an event from from the event context and frees it.
  513. *
  514. * The event and its contents cannot be used after this call.
  515. *
  516. * @see verto_add_io()
  517. * @see verto_add_timeout()
  518. * @see verto_add_idle()
  519. * @see verto_add_signal()
  520. * @see verto_add_child()
  521. * @param ev The event to delete.
  522. */
  523. void
  524. verto_del(verto_ev *ev);
  525. /**
  526. * Returns the event types supported by this implementation.
  527. *
  528. * @param ctx The verto_ctx to query.
  529. * @return The event types supported.
  530. */
  531. verto_ev_type
  532. verto_get_supported_types(verto_ctx *ctx);
  533. #ifdef __cplusplus
  534. } /* extern "C" */
  535. #endif /* __cplusplus */
  536. #endif /* VERTO_H_ */