1
0

test-condvar.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 <string.h>
  24. #include <errno.h>
  25. typedef struct {
  26. uv_mutex_t mutex;
  27. uv_cond_t cond;
  28. int delay;
  29. int use_broadcast;
  30. volatile int posted;
  31. } worker_config;
  32. static void worker(void* arg) {
  33. worker_config* c = arg;
  34. if (c->delay)
  35. uv_sleep(c->delay);
  36. uv_mutex_lock(&c->mutex);
  37. ASSERT(c->posted == 0);
  38. c->posted = 1;
  39. if (c->use_broadcast)
  40. uv_cond_broadcast(&c->cond);
  41. else
  42. uv_cond_signal(&c->cond);
  43. uv_mutex_unlock(&c->mutex);
  44. }
  45. TEST_IMPL(condvar_1) {
  46. uv_thread_t thread;
  47. worker_config wc;
  48. memset(&wc, 0, sizeof(wc));
  49. ASSERT(0 == uv_cond_init(&wc.cond));
  50. ASSERT(0 == uv_mutex_init(&wc.mutex));
  51. ASSERT(0 == uv_thread_create(&thread, worker, &wc));
  52. uv_mutex_lock(&wc.mutex);
  53. uv_sleep(100);
  54. uv_cond_wait(&wc.cond, &wc.mutex);
  55. ASSERT(wc.posted == 1);
  56. uv_mutex_unlock(&wc.mutex);
  57. ASSERT(0 == uv_thread_join(&thread));
  58. uv_mutex_destroy(&wc.mutex);
  59. uv_cond_destroy(&wc.cond);
  60. return 0;
  61. }
  62. TEST_IMPL(condvar_2) {
  63. uv_thread_t thread;
  64. worker_config wc;
  65. memset(&wc, 0, sizeof(wc));
  66. wc.delay = 100;
  67. ASSERT(0 == uv_cond_init(&wc.cond));
  68. ASSERT(0 == uv_mutex_init(&wc.mutex));
  69. ASSERT(0 == uv_thread_create(&thread, worker, &wc));
  70. uv_mutex_lock(&wc.mutex);
  71. uv_cond_wait(&wc.cond, &wc.mutex);
  72. uv_mutex_unlock(&wc.mutex);
  73. ASSERT(0 == uv_thread_join(&thread));
  74. uv_mutex_destroy(&wc.mutex);
  75. uv_cond_destroy(&wc.cond);
  76. return 0;
  77. }
  78. TEST_IMPL(condvar_3) {
  79. uv_thread_t thread;
  80. worker_config wc;
  81. int r;
  82. memset(&wc, 0, sizeof(wc));
  83. wc.delay = 100;
  84. ASSERT(0 == uv_cond_init(&wc.cond));
  85. ASSERT(0 == uv_mutex_init(&wc.mutex));
  86. ASSERT(0 == uv_thread_create(&thread, worker, &wc));
  87. uv_mutex_lock(&wc.mutex);
  88. r = uv_cond_timedwait(&wc.cond, &wc.mutex, (uint64_t)(50 * 1e6));
  89. ASSERT(r == -1);
  90. uv_mutex_unlock(&wc.mutex);
  91. ASSERT(0 == uv_thread_join(&thread));
  92. uv_mutex_destroy(&wc.mutex);
  93. uv_cond_destroy(&wc.cond);
  94. return 0;
  95. }
  96. TEST_IMPL(condvar_4) {
  97. uv_thread_t thread;
  98. worker_config wc;
  99. int r;
  100. memset(&wc, 0, sizeof(wc));
  101. wc.delay = 100;
  102. ASSERT(0 == uv_cond_init(&wc.cond));
  103. ASSERT(0 == uv_mutex_init(&wc.mutex));
  104. ASSERT(0 == uv_thread_create(&thread, worker, &wc));
  105. uv_mutex_lock(&wc.mutex);
  106. r = uv_cond_timedwait(&wc.cond, &wc.mutex, (uint64_t)(150 * 1e6));
  107. ASSERT(r == 0);
  108. uv_mutex_unlock(&wc.mutex);
  109. ASSERT(0 == uv_thread_join(&thread));
  110. uv_mutex_destroy(&wc.mutex);
  111. uv_cond_destroy(&wc.cond);
  112. return 0;
  113. }
  114. TEST_IMPL(condvar_5) {
  115. uv_thread_t thread;
  116. worker_config wc;
  117. memset(&wc, 0, sizeof(wc));
  118. wc.use_broadcast = 1;
  119. ASSERT(0 == uv_cond_init(&wc.cond));
  120. ASSERT(0 == uv_mutex_init(&wc.mutex));
  121. ASSERT(0 == uv_thread_create(&thread, worker, &wc));
  122. uv_mutex_lock(&wc.mutex);
  123. uv_sleep(100);
  124. uv_cond_wait(&wc.cond, &wc.mutex);
  125. ASSERT(wc.posted == 1);
  126. uv_mutex_unlock(&wc.mutex);
  127. ASSERT(0 == uv_thread_join(&thread));
  128. uv_mutex_destroy(&wc.mutex);
  129. uv_cond_destroy(&wc.cond);
  130. return 0;
  131. }