verto-module.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. /*** THE FOLLOWING ARE FOR IMPLEMENTATION MODULES ONLY ***/
  25. #ifndef VERTO_MODULE_H_
  26. #define VERTO_MODULE_H_
  27. #include <verto.h>
  28. #ifndef VERTO_MODULE_TYPES
  29. #define VERTO_MODULE_TYPES
  30. typedef void verto_mod_ctx;
  31. typedef void verto_mod_ev;
  32. #endif
  33. #define VERTO_MODULE_VERSION 3
  34. #define VERTO_MODULE_TABLE(name) verto_module_table_ ## name
  35. #define VERTO_MODULE(name, symb, types) \
  36. static verto_ctx_funcs name ## _funcs = { \
  37. name ## _ctx_new, \
  38. name ## _ctx_default, \
  39. name ## _ctx_free, \
  40. name ## _ctx_run, \
  41. name ## _ctx_run_once, \
  42. name ## _ctx_break, \
  43. name ## _ctx_reinitialize, \
  44. name ## _ctx_set_flags, \
  45. name ## _ctx_add, \
  46. name ## _ctx_del \
  47. }; \
  48. verto_module VERTO_MODULE_TABLE(name) = { \
  49. VERTO_MODULE_VERSION, \
  50. # name, \
  51. # symb, \
  52. types, \
  53. &name ## _funcs, \
  54. }; \
  55. verto_ctx * \
  56. verto_new_ ## name() \
  57. { \
  58. return verto_convert(name, 0, NULL); \
  59. } \
  60. verto_ctx * \
  61. verto_default_ ## name() \
  62. { \
  63. return verto_convert(name, 1, NULL); \
  64. }
  65. typedef struct {
  66. /* Required */ verto_mod_ctx *(*ctx_new)();
  67. /* Optional */ verto_mod_ctx *(*ctx_default)();
  68. /* Required */ void (*ctx_free)(verto_mod_ctx *ctx);
  69. /* Optional */ void (*ctx_run)(verto_mod_ctx *ctx);
  70. /* Required */ void (*ctx_run_once)(verto_mod_ctx *ctx);
  71. /* Optional */ void (*ctx_break)(verto_mod_ctx *ctx);
  72. /* Optional */ void (*ctx_reinitialize)(verto_mod_ctx *ctx);
  73. /* Optional */ void (*ctx_set_flags)(verto_mod_ctx *ctx,
  74. const verto_ev *ev,
  75. verto_mod_ev *modev);
  76. /* Required */ verto_mod_ev *(*ctx_add)(verto_mod_ctx *ctx,
  77. const verto_ev *ev,
  78. verto_ev_flag *flags);
  79. /* Required */ void (*ctx_del)(verto_mod_ctx *ctx,
  80. const verto_ev *ev,
  81. verto_mod_ev *modev);
  82. } verto_ctx_funcs;
  83. typedef struct {
  84. unsigned int vers;
  85. const char *name;
  86. const char *symb;
  87. verto_ev_type types;
  88. verto_ctx_funcs *funcs;
  89. } verto_module;
  90. /**
  91. * Converts an existing implementation specific loop to a verto_ctx.
  92. *
  93. * This function also sets the internal default implementation so that future
  94. * calls to verto_new(NULL) or verto_default(NULL) will use this specific
  95. * implementation if it was not already set.
  96. *
  97. * @param name The name of the module (unquoted)
  98. * @param deflt Whether the ctx is the default context or not
  99. * @param ctx The context to store
  100. * @return A new verto_ctx, or NULL on error. Call verto_free() when done.
  101. */
  102. #define verto_convert(name, deflt, ctx) \
  103. verto_convert_module(&VERTO_MODULE_TABLE(name), deflt, ctx)
  104. /**
  105. * Converts an existing implementation specific loop to a verto_ctx.
  106. *
  107. * If you are a module implementation, you probably want the macro above. This
  108. * function is generally used directly only when an application is attempting
  109. * to expose a home-grown event loop to verto.
  110. *
  111. * If deflt is non-zero and a default ctx was already defined for this module
  112. * and ctx is not NULL, than ctx will be free'd and the previously defined
  113. * default will be returned.
  114. *
  115. * If ctx is non-NULL, than the pre-existing verto_mod_ctx will be converted to
  116. * to a verto_ctx; if deflt is non-zero than this verto_mod_ctx will also be
  117. * marked as the default loop for this process. If ctx is NULL, than the
  118. * appropriate constructor will be called: either module->ctx_new() or
  119. * module->ctx_default() depending on the boolean value of deflt. If
  120. * module->ctx_default is NULL and deflt is non-zero, than module->ctx_new()
  121. * will be called and the resulting verto_mod_ctx will be utilized as the
  122. * default.
  123. *
  124. * This function also sets the internal default implementation so that future
  125. * calls to verto_new(NULL) or verto_default(NULL) will use this specific
  126. * implementation if it was not already set.
  127. *
  128. * @param name The name of the module (unquoted)
  129. * @param ctx The context private to store
  130. * @return A new verto_ctx, or NULL on error. Call verto_free() when done.
  131. */
  132. verto_ctx *
  133. verto_convert_module(const verto_module *module, int deflt, verto_mod_ctx *ctx);
  134. /**
  135. * Calls the callback of the verto_ev and then frees it via verto_del().
  136. *
  137. * The verto_ev is not freed (verto_del() is not called) if it is a signal event.
  138. *
  139. * @see verto_add_read()
  140. * @see verto_add_write()
  141. * @see verto_add_timeout()
  142. * @see verto_add_idle()
  143. * @see verto_add_signal()
  144. * @see verto_add_child()
  145. * @see verto_del()
  146. * @param ev The verto_ev
  147. */
  148. void
  149. verto_fire(verto_ev *ev);
  150. /**
  151. * Sets the status of the pid/handle which caused this event to fire.
  152. *
  153. * This function does nothing if the verto_ev is not a child type.
  154. *
  155. * @see verto_add_child()
  156. * @param ev The verto_ev to set the status in.
  157. * @param status The pid/handle status.
  158. */
  159. void
  160. verto_set_proc_status(verto_ev *ev, verto_proc_status status);
  161. /**
  162. * Sets the state of the fd which caused this event to fire.
  163. *
  164. * This function does nothing if the verto_ev is not a io type.
  165. *
  166. * Only the flags VERTO_EV_FLAG_IO_(READ|WRITE|ERROR) are supported. All other
  167. * flags are unset.
  168. *
  169. * @see verto_add_io()
  170. * @param ev The verto_ev to set the state in.
  171. * @param state The fd state.
  172. */
  173. void
  174. verto_set_fd_state(verto_ev *ev, verto_ev_flag state);
  175. #endif /* VERTO_MODULE_H_ */