1
0

test-semaphore.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <stdlib.h>
  24. #include <string.h>
  25. typedef struct {
  26. uv_mutex_t mutex;
  27. uv_sem_t sem;
  28. int delay;
  29. volatile int posted;
  30. } worker_config;
  31. static void worker(void* arg) {
  32. worker_config* c = arg;
  33. if (c->delay)
  34. uv_sleep(c->delay);
  35. uv_mutex_lock(&c->mutex);
  36. ASSERT(c->posted == 0);
  37. uv_sem_post(&c->sem);
  38. c->posted = 1;
  39. uv_mutex_unlock(&c->mutex);
  40. }
  41. TEST_IMPL(semaphore_1) {
  42. uv_thread_t thread;
  43. worker_config wc;
  44. memset(&wc, 0, sizeof(wc));
  45. ASSERT(0 == uv_sem_init(&wc.sem, 0));
  46. ASSERT(0 == uv_mutex_init(&wc.mutex));
  47. ASSERT(0 == uv_thread_create(&thread, worker, &wc));
  48. uv_sleep(100);
  49. uv_mutex_lock(&wc.mutex);
  50. ASSERT(wc.posted == 1);
  51. uv_sem_wait(&wc.sem); /* should not block */
  52. uv_mutex_unlock(&wc.mutex); /* ergo, it should be ok to unlock after wait */
  53. ASSERT(0 == uv_thread_join(&thread));
  54. uv_mutex_destroy(&wc.mutex);
  55. uv_sem_destroy(&wc.sem);
  56. return 0;
  57. }
  58. TEST_IMPL(semaphore_2) {
  59. uv_thread_t thread;
  60. worker_config wc;
  61. memset(&wc, 0, sizeof(wc));
  62. wc.delay = 100;
  63. ASSERT(0 == uv_sem_init(&wc.sem, 0));
  64. ASSERT(0 == uv_mutex_init(&wc.mutex));
  65. ASSERT(0 == uv_thread_create(&thread, worker, &wc));
  66. uv_sem_wait(&wc.sem);
  67. ASSERT(0 == uv_thread_join(&thread));
  68. uv_mutex_destroy(&wc.mutex);
  69. uv_sem_destroy(&wc.sem);
  70. return 0;
  71. }
  72. TEST_IMPL(semaphore_3) {
  73. uv_sem_t sem;
  74. ASSERT(0 == uv_sem_init(&sem, 3));
  75. uv_sem_wait(&sem); /* should not block */
  76. uv_sem_wait(&sem); /* should not block */
  77. ASSERT(0 == uv_sem_trywait(&sem));
  78. ASSERT(UV_EAGAIN == uv_sem_trywait(&sem));
  79. uv_sem_post(&sem);
  80. ASSERT(0 == uv_sem_trywait(&sem));
  81. ASSERT(UV_EAGAIN == uv_sem_trywait(&sem));
  82. uv_sem_destroy(&sem);
  83. return 0;
  84. }