test-signal-multiple-loops.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. /* This test does not pretend to be cross-platform. */
  22. #ifndef _WIN32
  23. #include "uv.h"
  24. #include "task.h"
  25. #include <errno.h>
  26. #include <signal.h>
  27. #include <stdarg.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. /* The value of NUM_SIGNAL_HANDLING_THREADS is not arbitrary; it needs to be a
  33. * multiple of three for reasons that will become clear when you scroll down.
  34. * We're basically creating three different thread groups. The total needs
  35. * to be divisible by three in order for the numbers in the final check to
  36. * match up.
  37. */
  38. #define NUM_SIGNAL_HANDLING_THREADS 24
  39. #define NUM_LOOP_CREATING_THREADS 10
  40. enum signal_action {
  41. ONLY_SIGUSR1,
  42. ONLY_SIGUSR2,
  43. SIGUSR1_AND_SIGUSR2
  44. };
  45. static uv_sem_t sem;
  46. static uv_mutex_t counter_lock;
  47. static volatile int stop = 0;
  48. static volatile int signal1_cb_counter = 0;
  49. static volatile int signal2_cb_counter = 0;
  50. static volatile int loop_creation_counter = 0;
  51. static void increment_counter(volatile int* counter) {
  52. uv_mutex_lock(&counter_lock);
  53. ++(*counter);
  54. uv_mutex_unlock(&counter_lock);
  55. }
  56. static void signal1_cb(uv_signal_t* handle, int signum) {
  57. ASSERT(signum == SIGUSR1);
  58. increment_counter(&signal1_cb_counter);
  59. uv_signal_stop(handle);
  60. }
  61. static void signal2_cb(uv_signal_t* handle, int signum) {
  62. ASSERT(signum == SIGUSR2);
  63. increment_counter(&signal2_cb_counter);
  64. uv_signal_stop(handle);
  65. }
  66. static void signal_handling_worker(void* context) {
  67. enum signal_action action;
  68. uv_signal_t signal1a;
  69. uv_signal_t signal1b;
  70. uv_signal_t signal2;
  71. uv_loop_t loop;
  72. int r;
  73. action = (enum signal_action) (uintptr_t) context;
  74. ASSERT(0 == uv_loop_init(&loop));
  75. /* Setup the signal watchers and start them. */
  76. if (action == ONLY_SIGUSR1 || action == SIGUSR1_AND_SIGUSR2) {
  77. r = uv_signal_init(&loop, &signal1a);
  78. ASSERT(r == 0);
  79. r = uv_signal_start(&signal1a, signal1_cb, SIGUSR1);
  80. ASSERT(r == 0);
  81. r = uv_signal_init(&loop, &signal1b);
  82. ASSERT(r == 0);
  83. r = uv_signal_start(&signal1b, signal1_cb, SIGUSR1);
  84. ASSERT(r == 0);
  85. }
  86. if (action == ONLY_SIGUSR2 || action == SIGUSR1_AND_SIGUSR2) {
  87. r = uv_signal_init(&loop, &signal2);
  88. ASSERT(r == 0);
  89. r = uv_signal_start(&signal2, signal2_cb, SIGUSR2);
  90. ASSERT(r == 0);
  91. }
  92. /* Signal watchers are now set up. */
  93. uv_sem_post(&sem);
  94. /* Wait for all signals. The signal callbacks stop the watcher, so uv_run
  95. * will return when all signal watchers caught a signal.
  96. */
  97. r = uv_run(&loop, UV_RUN_DEFAULT);
  98. ASSERT(r == 0);
  99. /* Restart the signal watchers. */
  100. if (action == ONLY_SIGUSR1 || action == SIGUSR1_AND_SIGUSR2) {
  101. r = uv_signal_start(&signal1a, signal1_cb, SIGUSR1);
  102. ASSERT(r == 0);
  103. r = uv_signal_start(&signal1b, signal1_cb, SIGUSR1);
  104. ASSERT(r == 0);
  105. }
  106. if (action == ONLY_SIGUSR2 || action == SIGUSR1_AND_SIGUSR2) {
  107. r = uv_signal_start(&signal2, signal2_cb, SIGUSR2);
  108. ASSERT(r == 0);
  109. }
  110. /* Wait for signals once more. */
  111. uv_sem_post(&sem);
  112. r = uv_run(&loop, UV_RUN_DEFAULT);
  113. ASSERT(r == 0);
  114. /* Close the watchers. */
  115. if (action == ONLY_SIGUSR1 || action == SIGUSR1_AND_SIGUSR2) {
  116. uv_close((uv_handle_t*) &signal1a, NULL);
  117. uv_close((uv_handle_t*) &signal1b, NULL);
  118. }
  119. if (action == ONLY_SIGUSR2 || action == SIGUSR1_AND_SIGUSR2) {
  120. uv_close((uv_handle_t*) &signal2, NULL);
  121. }
  122. /* Wait for the signal watchers to close. */
  123. r = uv_run(&loop, UV_RUN_DEFAULT);
  124. ASSERT(r == 0);
  125. uv_loop_close(&loop);
  126. }
  127. static void signal_unexpected_cb(uv_signal_t* handle, int signum) {
  128. ASSERT(0 && "signal_unexpected_cb should never be called");
  129. }
  130. static void loop_creating_worker(void* context) {
  131. (void) context;
  132. do {
  133. uv_loop_t *loop;
  134. uv_signal_t signal;
  135. int r;
  136. loop = malloc(sizeof(*loop));
  137. ASSERT(loop != NULL);
  138. ASSERT(0 == uv_loop_init(loop));
  139. r = uv_signal_init(loop, &signal);
  140. ASSERT(r == 0);
  141. r = uv_signal_start(&signal, signal_unexpected_cb, SIGTERM);
  142. ASSERT(r == 0);
  143. uv_close((uv_handle_t*) &signal, NULL);
  144. r = uv_run(loop, UV_RUN_DEFAULT);
  145. ASSERT(r == 0);
  146. uv_loop_close(loop);
  147. free(loop);
  148. increment_counter(&loop_creation_counter);
  149. } while (!stop);
  150. }
  151. TEST_IMPL(signal_multiple_loops) {
  152. #if defined(__CYGWIN__) || defined(__MSYS__)
  153. /* FIXME: This test needs more investigation. Somehow the `read` in
  154. uv__signal_lock fails spuriously with EACCES or even EAGAIN even
  155. though it is supposed to be blocking. Also the test hangs during
  156. thread setup occasionally. */
  157. RETURN_SKIP("FIXME: This test needs more investigation on Cygwin");
  158. #endif
  159. uv_thread_t loop_creating_threads[NUM_LOOP_CREATING_THREADS];
  160. uv_thread_t signal_handling_threads[NUM_SIGNAL_HANDLING_THREADS];
  161. enum signal_action action;
  162. sigset_t sigset;
  163. int i;
  164. int r;
  165. r = uv_sem_init(&sem, 0);
  166. ASSERT(r == 0);
  167. r = uv_mutex_init(&counter_lock);
  168. ASSERT(r == 0);
  169. /* Create a couple of threads that create a destroy loops continuously. */
  170. for (i = 0; i < NUM_LOOP_CREATING_THREADS; i++) {
  171. r = uv_thread_create(&loop_creating_threads[i],
  172. loop_creating_worker,
  173. NULL);
  174. ASSERT(r == 0);
  175. }
  176. /* Create a couple of threads that actually handle signals. */
  177. for (i = 0; i < NUM_SIGNAL_HANDLING_THREADS; i++) {
  178. switch (i % 3) {
  179. case 0: action = ONLY_SIGUSR1; break;
  180. case 1: action = ONLY_SIGUSR2; break;
  181. case 2: action = SIGUSR1_AND_SIGUSR2; break;
  182. }
  183. r = uv_thread_create(&signal_handling_threads[i],
  184. signal_handling_worker,
  185. (void*) (uintptr_t) action);
  186. ASSERT(r == 0);
  187. }
  188. /* Wait until all threads have started and set up their signal watchers. */
  189. for (i = 0; i < NUM_SIGNAL_HANDLING_THREADS; i++)
  190. uv_sem_wait(&sem);
  191. r = kill(getpid(), SIGUSR1);
  192. ASSERT(r == 0);
  193. r = kill(getpid(), SIGUSR2);
  194. ASSERT(r == 0);
  195. /* Wait for all threads to handle these signals. */
  196. for (i = 0; i < NUM_SIGNAL_HANDLING_THREADS; i++)
  197. uv_sem_wait(&sem);
  198. /* Block all signals to this thread, so we are sure that from here the signal
  199. * handler runs in another thread. This is more likely to catch thread and
  200. * signal safety issues if there are any.
  201. */
  202. sigfillset(&sigset);
  203. pthread_sigmask(SIG_SETMASK, &sigset, NULL);
  204. r = kill(getpid(), SIGUSR1);
  205. ASSERT(r == 0);
  206. r = kill(getpid(), SIGUSR2);
  207. ASSERT(r == 0);
  208. /* Wait for all signal handling threads to exit. */
  209. for (i = 0; i < NUM_SIGNAL_HANDLING_THREADS; i++) {
  210. r = uv_thread_join(&signal_handling_threads[i]);
  211. ASSERT(r == 0);
  212. }
  213. /* Tell all loop creating threads to stop. */
  214. stop = 1;
  215. /* Wait for all loop creating threads to exit. */
  216. for (i = 0; i < NUM_LOOP_CREATING_THREADS; i++) {
  217. r = uv_thread_join(&loop_creating_threads[i]);
  218. ASSERT(r == 0);
  219. }
  220. uv_sem_destroy(&sem);
  221. printf("signal1_cb calls: %d\n", signal1_cb_counter);
  222. printf("signal2_cb calls: %d\n", signal2_cb_counter);
  223. printf("loops created and destroyed: %d\n", loop_creation_counter);
  224. /* The division by three reflects the fact that we spawn three different
  225. * thread groups of (NUM_SIGNAL_HANDLING_THREADS / 3) threads each.
  226. */
  227. ASSERT(signal1_cb_counter == 8 * (NUM_SIGNAL_HANDLING_THREADS / 3));
  228. ASSERT(signal2_cb_counter == 4 * (NUM_SIGNAL_HANDLING_THREADS / 3));
  229. /* We don't know exactly how much loops will be created and destroyed, but at
  230. * least there should be 1 for every loop creating thread.
  231. */
  232. ASSERT(loop_creation_counter >= NUM_LOOP_CREATING_THREADS);
  233. MAKE_VALGRIND_HAPPY();
  234. return 0;
  235. }
  236. #else
  237. typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */
  238. #endif /* !_WIN32 */