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