signal.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. bool keep_ref;
  25. };
  26. struct signal_info {
  27. struct decl_info func;
  28. DARRAY(struct signal_callback) callbacks;
  29. pthread_mutex_t mutex;
  30. bool signalling;
  31. struct signal_info *next;
  32. };
  33. static inline struct signal_info *signal_info_create(struct decl_info *info)
  34. {
  35. pthread_mutexattr_t attr;
  36. struct signal_info *si;
  37. if (pthread_mutexattr_init(&attr) != 0)
  38. return NULL;
  39. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  40. return NULL;
  41. si = bmalloc(sizeof(struct signal_info));
  42. si->func = *info;
  43. si->next = NULL;
  44. si->signalling = false;
  45. da_init(si->callbacks);
  46. if (pthread_mutex_init(&si->mutex, &attr) != 0) {
  47. blog(LOG_ERROR, "Could not create signal");
  48. decl_info_free(&si->func);
  49. bfree(si);
  50. return NULL;
  51. }
  52. return si;
  53. }
  54. static inline void signal_info_destroy(struct signal_info *si)
  55. {
  56. if (si) {
  57. pthread_mutex_destroy(&si->mutex);
  58. decl_info_free(&si->func);
  59. da_free(si->callbacks);
  60. bfree(si);
  61. }
  62. }
  63. static inline size_t signal_get_callback_idx(struct signal_info *si,
  64. signal_callback_t callback,
  65. void *data)
  66. {
  67. for (size_t i = 0; i < si->callbacks.num; i++) {
  68. struct signal_callback *sc = si->callbacks.array + i;
  69. if (sc->callback == callback && sc->data == data)
  70. return i;
  71. }
  72. return DARRAY_INVALID;
  73. }
  74. struct global_callback_info {
  75. global_signal_callback_t callback;
  76. void *data;
  77. long signaling;
  78. bool remove;
  79. };
  80. struct signal_handler {
  81. struct signal_info *first;
  82. pthread_mutex_t mutex;
  83. volatile long refs;
  84. DARRAY(struct global_callback_info) global_callbacks;
  85. pthread_mutex_t global_callbacks_mutex;
  86. };
  87. static struct signal_info *getsignal(signal_handler_t *handler,
  88. const char *name,
  89. struct signal_info **p_last)
  90. {
  91. struct signal_info *signal, *last = NULL;
  92. signal = handler->first;
  93. while (signal != NULL) {
  94. if (strcmp(signal->func.name, name) == 0)
  95. break;
  96. last = signal;
  97. signal = signal->next;
  98. }
  99. if (p_last)
  100. *p_last = last;
  101. return signal;
  102. }
  103. /* ------------------------------------------------------------------------- */
  104. signal_handler_t *signal_handler_create(void)
  105. {
  106. struct signal_handler *handler = bzalloc(sizeof(struct signal_handler));
  107. handler->first = NULL;
  108. handler->refs = 1;
  109. pthread_mutexattr_t attr;
  110. if (pthread_mutexattr_init(&attr) != 0)
  111. return NULL;
  112. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  113. return NULL;
  114. if (pthread_mutex_init(&handler->mutex, NULL) != 0) {
  115. blog(LOG_ERROR, "Couldn't create signal handler mutex!");
  116. bfree(handler);
  117. return NULL;
  118. }
  119. if (pthread_mutex_init(&handler->global_callbacks_mutex, &attr) != 0) {
  120. blog(LOG_ERROR, "Couldn't create signal handler global "
  121. "callbacks mutex!");
  122. pthread_mutex_destroy(&handler->mutex);
  123. bfree(handler);
  124. return NULL;
  125. }
  126. return handler;
  127. }
  128. static void signal_handler_actually_destroy(signal_handler_t *handler)
  129. {
  130. struct signal_info *sig = handler->first;
  131. while (sig != NULL) {
  132. struct signal_info *next = sig->next;
  133. signal_info_destroy(sig);
  134. sig = next;
  135. }
  136. da_free(handler->global_callbacks);
  137. pthread_mutex_destroy(&handler->global_callbacks_mutex);
  138. pthread_mutex_destroy(&handler->mutex);
  139. bfree(handler);
  140. }
  141. void signal_handler_destroy(signal_handler_t *handler)
  142. {
  143. if (handler && os_atomic_dec_long(&handler->refs) == 0) {
  144. signal_handler_actually_destroy(handler);
  145. }
  146. }
  147. bool signal_handler_add(signal_handler_t *handler, const char *signal_decl)
  148. {
  149. struct decl_info func = {0};
  150. struct signal_info *sig, *last;
  151. bool success = true;
  152. if (!parse_decl_string(&func, signal_decl)) {
  153. blog(LOG_ERROR, "Signal declaration invalid: %s", signal_decl);
  154. return false;
  155. }
  156. pthread_mutex_lock(&handler->mutex);
  157. sig = getsignal(handler, func.name, &last);
  158. if (sig) {
  159. blog(LOG_WARNING, "Signal declaration '%s' exists", func.name);
  160. decl_info_free(&func);
  161. success = false;
  162. } else {
  163. sig = signal_info_create(&func);
  164. if (!last)
  165. handler->first = sig;
  166. else
  167. last->next = sig;
  168. }
  169. pthread_mutex_unlock(&handler->mutex);
  170. return success;
  171. }
  172. static void signal_handler_connect_internal(signal_handler_t *handler,
  173. const char *signal,
  174. signal_callback_t callback,
  175. void *data, bool keep_ref)
  176. {
  177. struct signal_info *sig, *last;
  178. struct signal_callback cb_data = {callback, data, false, keep_ref};
  179. size_t idx;
  180. if (!handler)
  181. return;
  182. pthread_mutex_lock(&handler->mutex);
  183. sig = getsignal(handler, signal, &last);
  184. pthread_mutex_unlock(&handler->mutex);
  185. if (!sig) {
  186. blog(LOG_WARNING,
  187. "signal_handler_connect: "
  188. "signal '%s' not found",
  189. signal);
  190. return;
  191. }
  192. /* -------------- */
  193. pthread_mutex_lock(&sig->mutex);
  194. if (keep_ref)
  195. os_atomic_inc_long(&handler->refs);
  196. idx = signal_get_callback_idx(sig, callback, data);
  197. if (keep_ref || idx == DARRAY_INVALID)
  198. da_push_back(sig->callbacks, &cb_data);
  199. pthread_mutex_unlock(&sig->mutex);
  200. }
  201. void signal_handler_connect(signal_handler_t *handler, const char *signal,
  202. signal_callback_t callback, void *data)
  203. {
  204. signal_handler_connect_internal(handler, signal, callback, data, false);
  205. }
  206. void signal_handler_connect_ref(signal_handler_t *handler, const char *signal,
  207. signal_callback_t callback, void *data)
  208. {
  209. signal_handler_connect_internal(handler, signal, callback, data, true);
  210. }
  211. static inline struct signal_info *getsignal_locked(signal_handler_t *handler,
  212. const char *name)
  213. {
  214. struct signal_info *sig;
  215. if (!handler)
  216. return NULL;
  217. pthread_mutex_lock(&handler->mutex);
  218. sig = getsignal(handler, name, NULL);
  219. pthread_mutex_unlock(&handler->mutex);
  220. return sig;
  221. }
  222. void signal_handler_disconnect(signal_handler_t *handler, const char *signal,
  223. signal_callback_t callback, void *data)
  224. {
  225. struct signal_info *sig = getsignal_locked(handler, signal);
  226. bool keep_ref = false;
  227. size_t idx;
  228. if (!sig)
  229. return;
  230. pthread_mutex_lock(&sig->mutex);
  231. idx = signal_get_callback_idx(sig, callback, data);
  232. if (idx != DARRAY_INVALID) {
  233. if (sig->signalling) {
  234. sig->callbacks.array[idx].remove = true;
  235. } else {
  236. keep_ref = sig->callbacks.array[idx].keep_ref;
  237. da_erase(sig->callbacks, idx);
  238. }
  239. }
  240. pthread_mutex_unlock(&sig->mutex);
  241. if (keep_ref && os_atomic_dec_long(&handler->refs) == 0) {
  242. signal_handler_actually_destroy(handler);
  243. }
  244. }
  245. static THREAD_LOCAL struct signal_callback *current_signal_cb = NULL;
  246. static THREAD_LOCAL struct global_callback_info *current_global_cb = NULL;
  247. void signal_handler_remove_current(void)
  248. {
  249. if (current_signal_cb)
  250. current_signal_cb->remove = true;
  251. else if (current_global_cb)
  252. current_global_cb->remove = true;
  253. }
  254. void signal_handler_signal(signal_handler_t *handler, const char *signal,
  255. calldata_t *params)
  256. {
  257. struct signal_info *sig = getsignal_locked(handler, signal);
  258. long remove_refs = 0;
  259. if (!sig)
  260. return;
  261. pthread_mutex_lock(&sig->mutex);
  262. sig->signalling = true;
  263. for (size_t i = 0; i < sig->callbacks.num; i++) {
  264. struct signal_callback *cb = sig->callbacks.array + i;
  265. if (!cb->remove) {
  266. current_signal_cb = cb;
  267. cb->callback(cb->data, params);
  268. current_signal_cb = NULL;
  269. }
  270. }
  271. for (size_t i = sig->callbacks.num; i > 0; i--) {
  272. struct signal_callback *cb = sig->callbacks.array + i - 1;
  273. if (cb->remove) {
  274. if (cb->keep_ref)
  275. remove_refs++;
  276. da_erase(sig->callbacks, i - 1);
  277. }
  278. }
  279. sig->signalling = false;
  280. pthread_mutex_unlock(&sig->mutex);
  281. pthread_mutex_lock(&handler->global_callbacks_mutex);
  282. if (handler->global_callbacks.num) {
  283. for (size_t i = 0; i < handler->global_callbacks.num; i++) {
  284. struct global_callback_info *cb =
  285. handler->global_callbacks.array + i;
  286. if (!cb->remove) {
  287. cb->signaling++;
  288. current_global_cb = cb;
  289. cb->callback(cb->data, signal, params);
  290. current_global_cb = NULL;
  291. cb->signaling--;
  292. }
  293. }
  294. for (size_t i = handler->global_callbacks.num; i > 0; i--) {
  295. struct global_callback_info *cb =
  296. handler->global_callbacks.array + (i - 1);
  297. if (cb->remove && !cb->signaling)
  298. da_erase(handler->global_callbacks, i - 1);
  299. }
  300. }
  301. pthread_mutex_unlock(&handler->global_callbacks_mutex);
  302. if (remove_refs) {
  303. os_atomic_set_long(&handler->refs,
  304. os_atomic_load_long(&handler->refs) -
  305. remove_refs);
  306. }
  307. }
  308. void signal_handler_connect_global(signal_handler_t *handler,
  309. global_signal_callback_t callback,
  310. void *data)
  311. {
  312. struct global_callback_info cb_data = {callback, data, 0, false};
  313. size_t idx;
  314. if (!handler || !callback)
  315. return;
  316. pthread_mutex_lock(&handler->global_callbacks_mutex);
  317. idx = da_find(handler->global_callbacks, &cb_data, 0);
  318. if (idx == DARRAY_INVALID)
  319. da_push_back(handler->global_callbacks, &cb_data);
  320. pthread_mutex_unlock(&handler->global_callbacks_mutex);
  321. }
  322. void signal_handler_disconnect_global(signal_handler_t *handler,
  323. global_signal_callback_t callback,
  324. void *data)
  325. {
  326. struct global_callback_info cb_data = {callback, data, 0, false};
  327. size_t idx;
  328. if (!handler || !callback)
  329. return;
  330. pthread_mutex_lock(&handler->global_callbacks_mutex);
  331. idx = da_find(handler->global_callbacks, &cb_data, 0);
  332. if (idx != DARRAY_INVALID) {
  333. struct global_callback_info *cb =
  334. handler->global_callbacks.array + idx;
  335. if (cb->signaling)
  336. cb->remove = true;
  337. else
  338. da_erase(handler->global_callbacks, idx);
  339. }
  340. pthread_mutex_unlock(&handler->global_callbacks_mutex);
  341. }