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