signal.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Copyright (c) 2013-2014 Hugh Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "../util/darray.h"
  17. #include "../util/threading.h"
  18. #include "decl.h"
  19. #include "signal.h"
  20. struct signal_callback {
  21. signal_callback_t callback;
  22. void *data;
  23. bool remove;
  24. };
  25. struct signal_info {
  26. struct decl_info func;
  27. DARRAY(struct signal_callback) callbacks;
  28. pthread_mutex_t mutex;
  29. bool signalling;
  30. struct signal_info *next;
  31. };
  32. static inline struct signal_info *signal_info_create(struct decl_info *info)
  33. {
  34. pthread_mutexattr_t attr;
  35. struct signal_info *si;
  36. if (pthread_mutexattr_init(&attr) != 0)
  37. return NULL;
  38. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  39. return NULL;
  40. si = bmalloc(sizeof(struct signal_info));
  41. si->func = *info;
  42. si->next = NULL;
  43. si->signalling = false;
  44. da_init(si->callbacks);
  45. if (pthread_mutex_init(&si->mutex, &attr) != 0) {
  46. blog(LOG_ERROR, "Could not create signal");
  47. decl_info_free(&si->func);
  48. bfree(si);
  49. return NULL;
  50. }
  51. return si;
  52. }
  53. static inline void signal_info_destroy(struct signal_info *si)
  54. {
  55. if (si) {
  56. pthread_mutex_destroy(&si->mutex);
  57. decl_info_free(&si->func);
  58. da_free(si->callbacks);
  59. bfree(si);
  60. }
  61. }
  62. static inline size_t signal_get_callback_idx(struct signal_info *si,
  63. signal_callback_t callback, void *data)
  64. {
  65. for (size_t i = 0; i < si->callbacks.num; i++) {
  66. struct signal_callback *sc = si->callbacks.array+i;
  67. if (sc->callback == callback && sc->data == data)
  68. return i;
  69. }
  70. return DARRAY_INVALID;
  71. }
  72. struct signal_handler {
  73. struct signal_info *first;
  74. pthread_mutex_t mutex;
  75. };
  76. static struct signal_info *getsignal(signal_handler_t *handler,
  77. const char *name, struct signal_info **p_last)
  78. {
  79. struct signal_info *signal, *last= NULL;
  80. signal = handler->first;
  81. while (signal != NULL) {
  82. if (strcmp(signal->func.name, name) == 0)
  83. break;
  84. last = signal;
  85. signal = signal->next;
  86. }
  87. if (p_last)
  88. *p_last = last;
  89. return signal;
  90. }
  91. /* ------------------------------------------------------------------------- */
  92. signal_handler_t *signal_handler_create(void)
  93. {
  94. struct signal_handler *handler = bmalloc(sizeof(struct signal_handler));
  95. handler->first = NULL;
  96. if (pthread_mutex_init(&handler->mutex, NULL) != 0) {
  97. blog(LOG_ERROR, "Couldn't create signal handler!");
  98. bfree(handler);
  99. return NULL;
  100. }
  101. return handler;
  102. }
  103. void signal_handler_destroy(signal_handler_t *handler)
  104. {
  105. if (handler) {
  106. struct signal_info *sig = handler->first;
  107. while (sig != NULL) {
  108. struct signal_info *next = sig->next;
  109. signal_info_destroy(sig);
  110. sig = next;
  111. }
  112. pthread_mutex_destroy(&handler->mutex);
  113. bfree(handler);
  114. }
  115. }
  116. bool signal_handler_add(signal_handler_t *handler, const char *signal_decl)
  117. {
  118. struct decl_info func = {0};
  119. struct signal_info *sig, *last;
  120. bool success = true;
  121. if (!parse_decl_string(&func, signal_decl)) {
  122. blog(LOG_ERROR, "Signal declaration invalid: %s", signal_decl);
  123. return false;
  124. }
  125. pthread_mutex_lock(&handler->mutex);
  126. sig = getsignal(handler, func.name, &last);
  127. if (sig) {
  128. blog(LOG_WARNING, "Signal declaration '%s' exists", func.name);
  129. decl_info_free(&func);
  130. success = false;
  131. } else {
  132. sig = signal_info_create(&func);
  133. if (!last)
  134. handler->first = sig;
  135. else
  136. last->next = sig;
  137. }
  138. pthread_mutex_unlock(&handler->mutex);
  139. return success;
  140. }
  141. void signal_handler_connect(signal_handler_t *handler, const char *signal,
  142. signal_callback_t callback, void *data)
  143. {
  144. struct signal_info *sig, *last;
  145. struct signal_callback cb_data = {callback, data, false};
  146. size_t idx;
  147. if (!handler)
  148. return;
  149. pthread_mutex_lock(&handler->mutex);
  150. sig = getsignal(handler, signal, &last);
  151. pthread_mutex_unlock(&handler->mutex);
  152. if (!sig) {
  153. blog(LOG_WARNING, "signal_handler_connect: "
  154. "signal '%s' not found", signal);
  155. return;
  156. }
  157. /* -------------- */
  158. pthread_mutex_lock(&sig->mutex);
  159. idx = signal_get_callback_idx(sig, callback, data);
  160. if (idx == DARRAY_INVALID)
  161. da_push_back(sig->callbacks, &cb_data);
  162. pthread_mutex_unlock(&sig->mutex);
  163. }
  164. static inline struct signal_info *getsignal_locked(signal_handler_t *handler,
  165. const char *name)
  166. {
  167. struct signal_info *sig;
  168. if (!handler)
  169. return NULL;
  170. pthread_mutex_lock(&handler->mutex);
  171. sig = getsignal(handler, name, NULL);
  172. pthread_mutex_unlock(&handler->mutex);
  173. return sig;
  174. }
  175. void signal_handler_disconnect(signal_handler_t *handler, const char *signal,
  176. signal_callback_t callback, void *data)
  177. {
  178. struct signal_info *sig = getsignal_locked(handler, signal);
  179. size_t idx;
  180. if (!sig)
  181. return;
  182. pthread_mutex_lock(&sig->mutex);
  183. idx = signal_get_callback_idx(sig, callback, data);
  184. if (idx != DARRAY_INVALID) {
  185. if (sig->signalling)
  186. sig->callbacks.array[idx].remove = true;
  187. else
  188. da_erase(sig->callbacks, idx);
  189. }
  190. pthread_mutex_unlock(&sig->mutex);
  191. }
  192. void signal_handler_signal(signal_handler_t *handler, const char *signal,
  193. calldata_t *params)
  194. {
  195. struct signal_info *sig = getsignal_locked(handler, signal);
  196. if (!sig)
  197. return;
  198. pthread_mutex_lock(&sig->mutex);
  199. sig->signalling = true;
  200. for (size_t i = 0; i < sig->callbacks.num; i++) {
  201. struct signal_callback *cb = sig->callbacks.array+i;
  202. cb->callback(cb->data, params);
  203. }
  204. for (size_t i = sig->callbacks.num; i > 0; i--) {
  205. struct signal_callback *cb = sig->callbacks.array+i-1;
  206. if (cb->remove)
  207. da_erase(sig->callbacks, i-1);
  208. }
  209. sig->signalling = false;
  210. pthread_mutex_unlock(&sig->mutex);
  211. }