test-mutexes.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_cond_t condvar;
  26. static uv_mutex_t mutex;
  27. static uv_rwlock_t rwlock;
  28. static int step;
  29. /* The mutex and rwlock tests are really poor.
  30. * They're very basic sanity checks and nothing more.
  31. * Apologies if that rhymes.
  32. */
  33. TEST_IMPL(thread_mutex) {
  34. uv_mutex_t mutex;
  35. int r;
  36. r = uv_mutex_init(&mutex);
  37. ASSERT(r == 0);
  38. uv_mutex_lock(&mutex);
  39. uv_mutex_unlock(&mutex);
  40. uv_mutex_destroy(&mutex);
  41. return 0;
  42. }
  43. TEST_IMPL(thread_mutex_recursive) {
  44. uv_mutex_t mutex;
  45. int r;
  46. r = uv_mutex_init_recursive(&mutex);
  47. ASSERT(r == 0);
  48. uv_mutex_lock(&mutex);
  49. uv_mutex_lock(&mutex);
  50. ASSERT(0 == uv_mutex_trylock(&mutex));
  51. uv_mutex_unlock(&mutex);
  52. uv_mutex_unlock(&mutex);
  53. uv_mutex_unlock(&mutex);
  54. uv_mutex_destroy(&mutex);
  55. return 0;
  56. }
  57. TEST_IMPL(thread_rwlock) {
  58. uv_rwlock_t rwlock;
  59. int r;
  60. r = uv_rwlock_init(&rwlock);
  61. ASSERT(r == 0);
  62. uv_rwlock_rdlock(&rwlock);
  63. uv_rwlock_rdunlock(&rwlock);
  64. uv_rwlock_wrlock(&rwlock);
  65. uv_rwlock_wrunlock(&rwlock);
  66. uv_rwlock_destroy(&rwlock);
  67. return 0;
  68. }
  69. /* Call when holding |mutex|. */
  70. static void synchronize_nowait(void) {
  71. step += 1;
  72. uv_cond_signal(&condvar);
  73. }
  74. /* Call when holding |mutex|. */
  75. static void synchronize(void) {
  76. int current;
  77. synchronize_nowait();
  78. /* Wait for the other thread. Guard against spurious wakeups. */
  79. for (current = step; current == step; uv_cond_wait(&condvar, &mutex));
  80. ASSERT(step == current + 1);
  81. }
  82. static void thread_rwlock_trylock_peer(void* unused) {
  83. (void) &unused;
  84. uv_mutex_lock(&mutex);
  85. /* Write lock held by other thread. */
  86. ASSERT(UV_EBUSY == uv_rwlock_tryrdlock(&rwlock));
  87. ASSERT(UV_EBUSY == uv_rwlock_trywrlock(&rwlock));
  88. synchronize();
  89. /* Read lock held by other thread. */
  90. ASSERT(0 == uv_rwlock_tryrdlock(&rwlock));
  91. uv_rwlock_rdunlock(&rwlock);
  92. ASSERT(UV_EBUSY == uv_rwlock_trywrlock(&rwlock));
  93. synchronize();
  94. /* Acquire write lock. */
  95. ASSERT(0 == uv_rwlock_trywrlock(&rwlock));
  96. synchronize();
  97. /* Release write lock and acquire read lock. */
  98. uv_rwlock_wrunlock(&rwlock);
  99. ASSERT(0 == uv_rwlock_tryrdlock(&rwlock));
  100. synchronize();
  101. uv_rwlock_rdunlock(&rwlock);
  102. synchronize_nowait(); /* Signal main thread we're going away. */
  103. uv_mutex_unlock(&mutex);
  104. }
  105. TEST_IMPL(thread_rwlock_trylock) {
  106. uv_thread_t thread;
  107. ASSERT(0 == uv_cond_init(&condvar));
  108. ASSERT(0 == uv_mutex_init(&mutex));
  109. ASSERT(0 == uv_rwlock_init(&rwlock));
  110. uv_mutex_lock(&mutex);
  111. ASSERT(0 == uv_thread_create(&thread, thread_rwlock_trylock_peer, NULL));
  112. /* Hold write lock. */
  113. ASSERT(0 == uv_rwlock_trywrlock(&rwlock));
  114. synchronize(); /* Releases the mutex to the other thread. */
  115. /* Release write lock and acquire read lock. Pthreads doesn't support
  116. * the notion of upgrading or downgrading rwlocks, so neither do we.
  117. */
  118. uv_rwlock_wrunlock(&rwlock);
  119. ASSERT(0 == uv_rwlock_tryrdlock(&rwlock));
  120. synchronize();
  121. /* Release read lock. */
  122. uv_rwlock_rdunlock(&rwlock);
  123. synchronize();
  124. /* Write lock held by other thread. */
  125. ASSERT(UV_EBUSY == uv_rwlock_tryrdlock(&rwlock));
  126. ASSERT(UV_EBUSY == uv_rwlock_trywrlock(&rwlock));
  127. synchronize();
  128. /* Read lock held by other thread. */
  129. ASSERT(0 == uv_rwlock_tryrdlock(&rwlock));
  130. uv_rwlock_rdunlock(&rwlock);
  131. ASSERT(UV_EBUSY == uv_rwlock_trywrlock(&rwlock));
  132. synchronize();
  133. ASSERT(0 == uv_thread_join(&thread));
  134. uv_rwlock_destroy(&rwlock);
  135. uv_mutex_unlock(&mutex);
  136. uv_mutex_destroy(&mutex);
  137. uv_cond_destroy(&condvar);
  138. return 0;
  139. }