universal.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* Gearman server and library
  2. * Copyright (C) 2008 Brian Aker, Eric Day
  3. * All rights reserved.
  4. *
  5. * Use and distribution licensed under the BSD license. See
  6. * the COPYING file in the parent directory for full text.
  7. */
  8. /**
  9. * @file
  10. * @brief Gearman Declarations
  11. */
  12. #pragma once
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. /**
  17. * @ingroup gearman_universal
  18. */
  19. struct gearman_universal_st
  20. {
  21. struct {
  22. bool dont_track_packets LIBGEARMAN_BITFIELD;
  23. bool non_blocking LIBGEARMAN_BITFIELD;
  24. bool stored_non_blocking LIBGEARMAN_BITFIELD;
  25. } options;
  26. gearman_verbose_t verbose;
  27. uint32_t con_count;
  28. uint32_t packet_count;
  29. uint32_t pfds_size;
  30. uint32_t sending;
  31. int timeout; // Used by poll()
  32. gearman_connection_st *con_list;
  33. gearman_packet_st *packet_list;
  34. struct pollfd *pfds;
  35. gearman_log_fn *log_fn;
  36. void *log_context;
  37. gearman_event_watch_fn *event_watch_fn;
  38. void *event_watch_context;
  39. gearman_malloc_fn *workload_malloc_fn;
  40. void *workload_malloc_context;
  41. gearman_free_fn *workload_free_fn;
  42. void *workload_free_context;
  43. struct {
  44. gearman_return_t rc;
  45. int last_errno;
  46. char last_error[GEARMAN_MAX_ERROR_SIZE];
  47. } error;
  48. };
  49. #ifdef GEARMAN_CORE
  50. /**
  51. * @addtogroup gearman_universal Gearman Declarations
  52. *
  53. * This is a low level interface for gearman library instances. This is used
  54. * internally by both client and worker interfaces, so you probably want to
  55. * look there first.
  56. *
  57. * There is no locking within a single gearman_universal_st structure, so for threaded
  58. * applications you must either ensure isolation in the application or use
  59. * multiple gearman_universal_st structures (for example, one for each thread).
  60. *
  61. * @{
  62. */
  63. /**
  64. * Initialize a gearman_universal_st structure. Always check the return value for failure.
  65. * Some other initialization may have failed. It is not required to memset()
  66. * a structure before providing it. These are for internal use only.
  67. *
  68. * @param[in] source Caller allocated structure.
  69. * @param[in] options gearman_universal_options_t options used to modify creation.
  70. * @return On success, a pointer to the (possibly allocated) structure. On
  71. * failure this will be NULL.
  72. */
  73. GEARMAN_INTERNAL_API
  74. gearman_universal_st *gearman_universal_create(gearman_universal_st *source, const gearman_universal_options_t *options);
  75. /**
  76. * Clone a gearman structure.
  77. *
  78. * @param[in] destination gearman_universal_st.
  79. * @param[in] source gearman_universal_st to clone from.
  80. * @return Same return as gearman_universal_create().
  81. */
  82. GEARMAN_INTERNAL_API
  83. gearman_universal_st *gearman_universal_clone(gearman_universal_st *destination, const gearman_universal_st *source);
  84. /**
  85. * Free a gearman structure.
  86. *
  87. * @param[in] gearman Structure previously initialized with gearman_universal_create() or
  88. * gearman_clone().
  89. */
  90. GEARMAN_INTERNAL_API
  91. void gearman_universal_free(gearman_universal_st *gearman);
  92. /**
  93. * Set the error string.
  94. *
  95. * @param[in] gearman Structure previously initialized with gearman_universal_create() or
  96. * gearman_clone().
  97. * @param[in] function Name of function the error happened in.
  98. * @param[in] format Format and variable argument list of message.
  99. */
  100. GEARMAN_INTERNAL_API
  101. void gearman_universal_set_error(gearman_universal_st *gearman,
  102. gearman_return_t rc,
  103. const char *function,
  104. const char *format, ...);
  105. GEARMAN_INTERNAL_API
  106. void gearman_universal_set_perror(const char *position, gearman_universal_st *universal, const char *message);
  107. #define STRINGIFY(x) #x
  108. #define TOSTRING(x) STRINGIFY(x)
  109. #define AT __FILE__ ":" TOSTRING(__LINE__)
  110. #define gearman_perror(A, B) do { gearman_universal_set_perror(AT, A, B); } while (0)
  111. #define gearman_error(A, B, C) do { gearman_universal_set_error(A, B, AT, C); } while (0)
  112. /**
  113. * Return an error string for last error encountered.
  114. *
  115. * @param[in] gearman Structure previously initialized with gearman_universal_create() or
  116. * gearman_clone().
  117. * @return Pointer to a buffer in the structure that holds an error string.
  118. */
  119. static inline const char *gearman_universal_error(const gearman_universal_st *gearman)
  120. {
  121. if (gearman->error.last_error[0] == 0)
  122. return 0;
  123. return (const char *)(gearman->error.last_error);
  124. }
  125. /**
  126. * Value of errno in the case of a GEARMAN_ERRNO return value.
  127. *
  128. * @param[in] gearman Structure previously initialized with gearman_universal_create() or
  129. * gearman_clone().
  130. * @return An errno value as defined in your system errno.h file.
  131. */
  132. static inline int gearman_universal_errno(const gearman_universal_st *gearman)
  133. {
  134. return gearman->error.last_errno;
  135. }
  136. /**
  137. * Add options for a gearman structure.
  138. *
  139. * @param[in] gearman Structure previously initialized with gearman_universal_create() or
  140. * gearman_clone().
  141. * @param[in] options Available options for gearman structures.
  142. */
  143. GEARMAN_INTERNAL_API
  144. gearman_return_t gearman_universal_set_option(gearman_universal_st *gearman, gearman_universal_options_t option, bool value);
  145. static inline void gearman_universal_add_options(gearman_universal_st *gearman, gearman_universal_options_t options)
  146. {
  147. (void)gearman_universal_set_option(gearman, options, true);
  148. }
  149. static inline void gearman_universal_remove_options(gearman_universal_st *gearman, gearman_universal_options_t options)
  150. {
  151. (void)gearman_universal_set_option(gearman, options, false);
  152. }
  153. static inline bool gearman_universal_is_non_blocking(gearman_universal_st *gearman)
  154. {
  155. return gearman->options.non_blocking;
  156. }
  157. /**
  158. * Get current socket I/O activity timeout value.
  159. *
  160. * @param[in] gearman Structure previously initialized with gearman_universal_create() or
  161. * gearman_clone().
  162. * @return Timeout in milliseconds to wait for I/O activity. A negative value
  163. * means an infinite timeout.
  164. */
  165. GEARMAN_INTERNAL_API
  166. int gearman_universal_timeout(gearman_universal_st *gearman);
  167. /**
  168. * Set socket I/O activity timeout for connections in a Gearman structure.
  169. *
  170. * @param[in] gearman Structure previously initialized with gearman_universal_create() or
  171. * gearman_clone().
  172. * @param[in] timeout Milliseconds to wait for I/O activity. A negative value
  173. * means an infinite timeout.
  174. */
  175. GEARMAN_INTERNAL_API
  176. void gearman_universal_set_timeout(gearman_universal_st *gearman, int timeout);
  177. /**
  178. * Set logging function for a gearman structure.
  179. *
  180. * @param[in] gearman Structure previously initialized with gearman_universal_create() or
  181. * gearman_clone().
  182. * @param[in] function Function to call when there is a logging message.
  183. * @param[in] context Argument to pass into the callback function.
  184. * @param[in] verbose Verbosity level threshold. Only call function when the
  185. * logging message is equal to or less verbose that this.
  186. */
  187. GEARMAN_INTERNAL_API
  188. void gearman_set_log_fn(gearman_universal_st *gearman, gearman_log_fn *function,
  189. void *context, gearman_verbose_t verbose);
  190. /**
  191. * Set custom I/O event callback function for a gearman structure.
  192. *
  193. * @param[in] gearman Structure previously initialized with gearman_universal_create() or
  194. * gearman_clone().
  195. * @param[in] function Function to call when there is an I/O event.
  196. * @param[in] context Argument to pass into the callback function.
  197. */
  198. GEARMAN_INTERNAL_API
  199. void gearman_set_event_watch_fn(gearman_universal_st *gearman,
  200. gearman_event_watch_fn *function,
  201. void *context);
  202. /**
  203. * Set custom memory allocation function for workloads. Normally gearman uses
  204. * the standard system malloc to allocate memory used with workloads. The
  205. * provided function will be used instead.
  206. *
  207. * @param[in] gearman Structure previously initialized with gearman_universal_create() or
  208. * gearman_clone().
  209. * @param[in] function Memory allocation function to use instead of malloc().
  210. * @param[in] context Argument to pass into the callback function.
  211. */
  212. GEARMAN_INTERNAL_API
  213. void gearman_set_workload_malloc_fn(gearman_universal_st *gearman,
  214. gearman_malloc_fn *function,
  215. void *context);
  216. /**
  217. * Set custom memory free function for workloads. Normally gearman uses the
  218. * standard system free to free memory used with workloads. The provided
  219. * function will be used instead.
  220. *
  221. * @param[in] gearman Structure previously initialized with gearman_universal_create() or
  222. * gearman_clone().
  223. * @param[in] function Memory free function to use instead of free().
  224. * @param[in] context Argument to pass into the callback function.
  225. */
  226. GEARMAN_INTERNAL_API
  227. void gearman_set_workload_free_fn(gearman_universal_st *gearman,
  228. gearman_free_fn *function,
  229. void *context);
  230. /**
  231. * Free all connections for a gearman structure.
  232. *
  233. * @param[in] gearman Structure previously initialized with gearman_universal_create() or
  234. * gearman_clone().
  235. */
  236. GEARMAN_INTERNAL_API
  237. void gearman_free_all_cons(gearman_universal_st *gearman);
  238. /**
  239. * Flush the send buffer for all connections.
  240. *
  241. * @param[in] gearman Structure previously initialized with gearman_universal_create() or
  242. * gearman_clone().
  243. * @return Standard gearman return value.
  244. */
  245. GEARMAN_INTERNAL_API
  246. gearman_return_t gearman_flush_all(gearman_universal_st *gearman);
  247. /**
  248. * Wait for I/O on connections.
  249. *
  250. * @param[in] gearman Structure previously initialized with gearman_universal_create() or
  251. * gearman_clone().
  252. * @return Standard gearman return value.
  253. */
  254. GEARMAN_INTERNAL_API
  255. gearman_return_t gearman_wait(gearman_universal_st *gearman);
  256. /**
  257. * Get next connection that is ready for I/O.
  258. *
  259. * @param[in] gearman Structure previously initialized with gearman_universal_create() or
  260. * gearman_clone().
  261. * @return Connection that is ready for I/O, or NULL if there are none.
  262. */
  263. GEARMAN_INTERNAL_API
  264. gearman_connection_st *gearman_ready(gearman_universal_st *gearman);
  265. /**
  266. * Test echo with all connections.
  267. *
  268. * @param[in] gearman Structure previously initialized with gearman_universal_create() or
  269. * gearman_clone().
  270. * @param[in] workload Data to send in echo packet.
  271. * @param[in] workload_size Size of workload.
  272. * @return Standard gearman return value.
  273. */
  274. GEARMAN_INTERNAL_API
  275. gearman_return_t gearman_echo(gearman_universal_st *gearman, const void *workload,
  276. size_t workload_size);
  277. /**
  278. * Free all packets for a gearman structure.
  279. *
  280. * @param[in] gearman Structure previously initialized with gearman_universal_create() or
  281. * gearman_clone().
  282. */
  283. GEARMAN_INTERNAL_API
  284. void gearman_free_all_packets(gearman_universal_st *gearman);
  285. #endif /* GEARMAN_CORE */
  286. /** @} */
  287. #ifdef __cplusplus
  288. }
  289. #endif