test-async.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include "uv.h"
  22. #include "task.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. static uv_thread_t thread;
  26. static uv_mutex_t mutex;
  27. static uv_prepare_t prepare;
  28. static uv_async_t async;
  29. static volatile int async_cb_called;
  30. static int prepare_cb_called;
  31. static int close_cb_called;
  32. static void thread_cb(void *arg) {
  33. int n;
  34. int r;
  35. for (;;) {
  36. uv_mutex_lock(&mutex);
  37. n = async_cb_called;
  38. uv_mutex_unlock(&mutex);
  39. if (n == 3) {
  40. break;
  41. }
  42. r = uv_async_send(&async);
  43. ASSERT(r == 0);
  44. /* Work around a bug in Valgrind.
  45. *
  46. * Valgrind runs threads not in parallel but sequentially, i.e. one after
  47. * the other. It also doesn't preempt them, instead it depends on threads
  48. * yielding voluntarily by making a syscall.
  49. *
  50. * That never happens here: the pipe that is associated with the async
  51. * handle is written to once but that's too early for Valgrind's scheduler
  52. * to kick in. Afterwards, the thread busy-loops, starving the main thread.
  53. * Therefore, we yield.
  54. *
  55. * This behavior has been observed with Valgrind 3.7.0 and 3.9.0.
  56. */
  57. uv_sleep(0);
  58. }
  59. }
  60. static void close_cb(uv_handle_t* handle) {
  61. ASSERT(handle != NULL);
  62. close_cb_called++;
  63. }
  64. static void async_cb(uv_async_t* handle, int status) {
  65. int n;
  66. ASSERT(handle == &async);
  67. ASSERT(status == 0);
  68. uv_mutex_lock(&mutex);
  69. n = ++async_cb_called;
  70. uv_mutex_unlock(&mutex);
  71. if (n == 3) {
  72. uv_close((uv_handle_t*)&async, close_cb);
  73. uv_close((uv_handle_t*)&prepare, close_cb);
  74. }
  75. }
  76. static void prepare_cb(uv_prepare_t* handle, int status) {
  77. int r;
  78. ASSERT(handle == &prepare);
  79. ASSERT(status == 0);
  80. if (prepare_cb_called++)
  81. return;
  82. r = uv_thread_create(&thread, thread_cb, NULL);
  83. ASSERT(r == 0);
  84. uv_mutex_unlock(&mutex);
  85. }
  86. TEST_IMPL(async) {
  87. int r;
  88. r = uv_mutex_init(&mutex);
  89. ASSERT(r == 0);
  90. uv_mutex_lock(&mutex);
  91. r = uv_prepare_init(uv_default_loop(), &prepare);
  92. ASSERT(r == 0);
  93. r = uv_prepare_start(&prepare, prepare_cb);
  94. ASSERT(r == 0);
  95. r = uv_async_init(uv_default_loop(), &async, async_cb);
  96. ASSERT(r == 0);
  97. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  98. ASSERT(r == 0);
  99. ASSERT(prepare_cb_called > 0);
  100. ASSERT(async_cb_called == 3);
  101. ASSERT(close_cb_called == 2);
  102. ASSERT(0 == uv_thread_join(&thread));
  103. MAKE_VALGRIND_HAPPY();
  104. return 0;
  105. }