PageRenderTime 1096ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/deps/uv/test/test-signal-multiple-loops.c

https://github.com/dshaw/node
C | 289 lines | 174 code | 62 blank | 53 comment | 64 complexity | 69c25ebeccba6cf57e1165aae13f51e7 MD5 | raw file
  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. loop = uv_loop_new();
  75. ASSERT(loop != NULL);
  76. /* Setup the signal watchers and start them. */
  77. if (action == ONLY_SIGUSR1 || action == SIGUSR1_AND_SIGUSR2) {
  78. r = uv_signal_init(loop, &signal1a);
  79. ASSERT(r == 0);
  80. r = uv_signal_start(&signal1a, signal1_cb, SIGUSR1);
  81. ASSERT(r == 0);
  82. r = uv_signal_init(loop, &signal1b);
  83. ASSERT(r == 0);
  84. r = uv_signal_start(&signal1b, signal1_cb, SIGUSR1);
  85. ASSERT(r == 0);
  86. }
  87. if (action == ONLY_SIGUSR2 || action == SIGUSR1_AND_SIGUSR2) {
  88. r = uv_signal_init(loop, &signal2);
  89. ASSERT(r == 0);
  90. r = uv_signal_start(&signal2, signal2_cb, SIGUSR2);
  91. ASSERT(r == 0);
  92. }
  93. /* Signal watchers are now set up. */
  94. uv_sem_post(&sem);
  95. /* Wait for all signals. The signal callbacks stop the watcher, so uv_run
  96. * will return when all signal watchers caught a signal.
  97. */
  98. r = uv_run(loop, UV_RUN_DEFAULT);
  99. ASSERT(r == 0);
  100. /* Restart the signal watchers. */
  101. if (action == ONLY_SIGUSR1 || action == SIGUSR1_AND_SIGUSR2) {
  102. r = uv_signal_start(&signal1a, signal1_cb, SIGUSR1);
  103. ASSERT(r == 0);
  104. r = uv_signal_start(&signal1b, signal1_cb, SIGUSR1);
  105. ASSERT(r == 0);
  106. }
  107. if (action == ONLY_SIGUSR2 || action == SIGUSR1_AND_SIGUSR2) {
  108. r = uv_signal_start(&signal2, signal2_cb, SIGUSR2);
  109. ASSERT(r == 0);
  110. }
  111. /* Wait for signals once more. */
  112. uv_sem_post(&sem);
  113. r = uv_run(loop, UV_RUN_DEFAULT);
  114. ASSERT(r == 0);
  115. /* Close the watchers. */
  116. if (action == ONLY_SIGUSR1 || action == SIGUSR1_AND_SIGUSR2) {
  117. uv_close((uv_handle_t*) &signal1a, NULL);
  118. uv_close((uv_handle_t*) &signal1b, NULL);
  119. }
  120. if (action == ONLY_SIGUSR2 || action == SIGUSR1_AND_SIGUSR2) {
  121. uv_close((uv_handle_t*) &signal2, NULL);
  122. }
  123. /* Wait for the signal watchers to close. */
  124. r = uv_run(loop, UV_RUN_DEFAULT);
  125. ASSERT(r == 0);
  126. uv_loop_delete(loop);
  127. }
  128. static void signal_unexpected_cb(uv_signal_t* handle, int signum) {
  129. ASSERT(0 && "signal_unexpected_cb should never be called");
  130. }
  131. static void loop_creating_worker(void* context) {
  132. (void) context;
  133. do {
  134. uv_loop_t* loop;
  135. uv_signal_t signal;
  136. int r;
  137. loop = uv_loop_new();
  138. ASSERT(loop != NULL);
  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_delete(loop);
  147. increment_counter(&loop_creation_counter);
  148. } while (!stop);
  149. }
  150. TEST_IMPL(signal_multiple_loops) {
  151. uv_thread_t loop_creating_threads[NUM_LOOP_CREATING_THREADS];
  152. uv_thread_t signal_handling_threads[NUM_SIGNAL_HANDLING_THREADS];
  153. enum signal_action action;
  154. sigset_t sigset;
  155. int i;
  156. int r;
  157. r = uv_sem_init(&sem, 0);
  158. ASSERT(r == 0);
  159. r = uv_mutex_init(&counter_lock);
  160. ASSERT(r == 0);
  161. /* Create a couple of threads that create a destroy loops continuously. */
  162. for (i = 0; i < NUM_LOOP_CREATING_THREADS; i++) {
  163. r = uv_thread_create(&loop_creating_threads[i],
  164. loop_creating_worker,
  165. NULL);
  166. ASSERT(r == 0);
  167. }
  168. /* Create a couple of threads that actually handle signals. */
  169. for (i = 0; i < NUM_SIGNAL_HANDLING_THREADS; i++) {
  170. switch (i % 3) {
  171. case 0: action = ONLY_SIGUSR1; break;
  172. case 1: action = ONLY_SIGUSR2; break;
  173. case 2: action = SIGUSR1_AND_SIGUSR2; break;
  174. }
  175. r = uv_thread_create(&signal_handling_threads[i],
  176. signal_handling_worker,
  177. (void*) (uintptr_t) action);
  178. ASSERT(r == 0);
  179. }
  180. /* Wait until all threads have started and set up their signal watchers. */
  181. for (i = 0; i < NUM_SIGNAL_HANDLING_THREADS; i++)
  182. uv_sem_wait(&sem);
  183. r = kill(getpid(), SIGUSR1);
  184. ASSERT(r == 0);
  185. r = kill(getpid(), SIGUSR2);
  186. ASSERT(r == 0);
  187. /* Wait for all threads to handle these signals. */
  188. for (i = 0; i < NUM_SIGNAL_HANDLING_THREADS; i++)
  189. uv_sem_wait(&sem);
  190. /* Block all signals to this thread, so we are sure that from here the signal
  191. * handler runs in another thread. This is is more likely to catch thread and
  192. * signal safety issues if there are any.
  193. */
  194. sigfillset(&sigset);
  195. pthread_sigmask(SIG_SETMASK, &sigset, NULL);
  196. r = kill(getpid(), SIGUSR1);
  197. ASSERT(r == 0);
  198. r = kill(getpid(), SIGUSR2);
  199. ASSERT(r == 0);
  200. /* Wait for all signal handling threads to exit. */
  201. for (i = 0; i < NUM_SIGNAL_HANDLING_THREADS; i++) {
  202. r = uv_thread_join(&signal_handling_threads[i]);
  203. ASSERT(r == 0);
  204. }
  205. /* Tell all loop creating threads to stop. */
  206. stop = 1;
  207. /* Wait for all loop creating threads to exit. */
  208. for (i = 0; i < NUM_LOOP_CREATING_THREADS; i++) {
  209. r = uv_thread_join(&loop_creating_threads[i]);
  210. ASSERT(r == 0);
  211. }
  212. printf("signal1_cb calls: %d\n", signal1_cb_counter);
  213. printf("signal2_cb calls: %d\n", signal2_cb_counter);
  214. printf("loops created and destroyed: %d\n", loop_creation_counter);
  215. /* The division by three reflects the fact that we spawn three different
  216. * thread groups of (NUM_SIGNAL_HANDLING_THREADS / 3) threads each.
  217. */
  218. ASSERT(signal1_cb_counter == 8 * (NUM_SIGNAL_HANDLING_THREADS / 3));
  219. ASSERT(signal2_cb_counter == 4 * (NUM_SIGNAL_HANDLING_THREADS / 3));
  220. /* We don't know exactly how much loops will be created and destroyed, but at
  221. * least there should be 1 for every loop creating thread.
  222. */
  223. ASSERT(loop_creation_counter >= NUM_LOOP_CREATING_THREADS);
  224. MAKE_VALGRIND_HAPPY();
  225. return 0;
  226. }
  227. #endif /* !_WIN32 */