verto-module.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 2
  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_add, \
  45. name ## _ctx_del \
  46. }; \
  47. verto_module VERTO_MODULE_TABLE(name) = { \
  48. VERTO_MODULE_VERSION, \
  49. # name, \
  50. # symb, \
  51. types, \
  52. &name ## _funcs, \
  53. }; \
  54. verto_ctx * \
  55. verto_new_ ## name() \
  56. { \
  57. return verto_convert(name, 0, NULL); \
  58. } \
  59. verto_ctx * \
  60. verto_default_ ## name() \
  61. { \
  62. return verto_convert(name, 1, NULL); \
  63. }
  64. typedef struct {
  65. /* Required */ verto_mod_ctx *(*ctx_new)();
  66. /* Optional */ verto_mod_ctx *(*ctx_default)();
  67. /* Required */ void (*ctx_free)(verto_mod_ctx *ctx);
  68. /* Optional */ void (*ctx_run)(verto_mod_ctx *ctx);
  69. /* Required */ void (*ctx_run_once)(verto_mod_ctx *ctx);
  70. /* Optional */ void (*ctx_break)(verto_mod_ctx *ctx);
  71. /* Optional */ void (*ctx_reinitialize)(verto_mod_ctx *ctx);
  72. /* Required */ verto_mod_ev *(*ctx_add)(verto_mod_ctx *ctx,
  73. const verto_ev *ev,
  74. verto_ev_flag *flags);
  75. /* Required */ void (*ctx_del)(verto_mod_ctx *ctx,
  76. const verto_ev *ev,
  77. verto_mod_ev *modev);
  78. } verto_ctx_funcs;
  79. typedef struct {
  80. unsigned int vers;
  81. const char *name;
  82. const char *symb;
  83. verto_ev_type types;
  84. verto_ctx_funcs *funcs;
  85. } verto_module;
  86. /**
  87. * Converts an existing implementation specific loop to a verto_ctx.
  88. *
  89. * This function also sets the internal default implementation so that future
  90. * calls to verto_new(NULL) or verto_default(NULL) will use this specific
  91. * implementation if it was not already set.
  92. *
  93. * @param name The name of the module (unquoted)
  94. * @param deflt Whether the ctx is the default context or not
  95. * @param ctx The context to store
  96. * @return A new verto_ctx, or NULL on error. Call verto_free() when done.
  97. */
  98. #define verto_convert(name, deflt, ctx) \
  99. verto_convert_module(&VERTO_MODULE_TABLE(name), deflt, ctx)
  100. /**
  101. * Converts an existing implementation specific loop to a verto_ctx.
  102. *
  103. * If you are a module implementation, you probably want the macro above. This
  104. * function is generally used directly only when an application is attempting
  105. * to expose a home-grown event loop to verto.
  106. *
  107. * If deflt is non-zero and a default ctx was already defined for this module
  108. * and ctx is not NULL, than ctx will be free'd and the previously defined
  109. * default will be returned.
  110. *
  111. * If ctx is non-NULL, than the pre-existing verto_mod_ctx will be converted to
  112. * to a verto_ctx; if deflt is non-zero than this verto_mod_ctx will also be
  113. * marked as the default loop for this process. If ctx is NULL, than the
  114. * appropriate constructor will be called: either module->ctx_new() or
  115. * module->ctx_default() depending on the boolean value of deflt. If
  116. * module->ctx_default is NULL and deflt is non-zero, than module->ctx_new()
  117. * will be called and the resulting verto_mod_ctx will be utilized as the
  118. * default.
  119. *
  120. * This function also sets the internal default implementation so that future
  121. * calls to verto_new(NULL) or verto_default(NULL) will use this specific
  122. * implementation if it was not already set.
  123. *
  124. * @param name The name of the module (unquoted)
  125. * @param ctx The context private to store
  126. * @return A new verto_ctx, or NULL on error. Call verto_free() when done.
  127. */
  128. verto_ctx *
  129. verto_convert_module(const verto_module *module, int deflt, verto_mod_ctx *ctx);
  130. /**
  131. * Calls the callback of the verto_ev and then frees it via verto_del().
  132. *
  133. * The verto_ev is not freed (verto_del() is not called) if it is a signal event.
  134. *
  135. * @see verto_add_read()
  136. * @see verto_add_write()
  137. * @see verto_add_timeout()
  138. * @see verto_add_idle()
  139. * @see verto_add_signal()
  140. * @see verto_add_child()
  141. * @see verto_del()
  142. * @param ev The verto_ev
  143. */
  144. void
  145. verto_fire(verto_ev *ev);
  146. /**
  147. * Sets the status of the pid/handle which caused this event to fire.
  148. *
  149. * This function does nothing if the verto_ev is not a child type.
  150. *
  151. * @see verto_add_child()
  152. * @param ev The verto_ev to set the status in.
  153. * @param status The pid/handle status.
  154. */
  155. void
  156. verto_set_proc_status(verto_ev *ev, verto_proc_status status);
  157. #endif /* VERTO_MODULE_H_ */