test-semaphore.c
Go to the documentation of this file.
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 {
31  int delay;
32  volatile int posted;
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) {
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 
68  uv_sem_destroy(&wc.sem);
69 
70  return 0;
71 }
72 
73 
74 TEST_IMPL(semaphore_2) {
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 
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 
109 
110  return 0;
111 }
sem
static uv_sem_t sem
Definition: test-signal-multiple-loops.c:52
uv_sem_trywait
UV_EXTERN int uv_sem_trywait(uv_sem_t *sem)
Definition: libuv/src/unix/thread.c:685
task.h
memset
return memset(p, 0, total)
mutex
static uv_mutex_t mutex
Definition: threadpool.c:34
uv_mutex_init
UV_EXTERN int uv_mutex_init(uv_mutex_t *handle)
Definition: libuv/src/unix/thread.c:281
string.h
uv_mutex_destroy
UV_EXTERN void uv_mutex_destroy(uv_mutex_t *handle)
Definition: libuv/src/unix/thread.c:323
ASSERT
#define ASSERT(expr)
Definition: task.h:102
uv_thread_join
UV_EXTERN int uv_thread_join(uv_thread_t *tid)
Definition: libuv/src/unix/thread.c:271
worker_config
struct worker_config worker_config
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
worker
Definition: worker.py:1
arg
Definition: cmdline.cc:40
worker_config::posted
volatile int posted
Definition: test-barrier.c:31
worker_config::sem
uv_sem_t sem
Definition: test-semaphore.c:30
uv_mutex_t
pthread_mutex_t uv_mutex_t
Definition: unix.h:135
uv_thread_create
UV_EXTERN int uv_thread_create(uv_thread_t *tid, uv_thread_cb entry, void *arg)
Definition: libuv/src/unix/thread.c:209
uv_sleep
UV_EXTERN void uv_sleep(unsigned int msec)
Definition: unix/core.c:1521
uv_mutex_unlock
UV_EXTERN void uv_mutex_unlock(uv_mutex_t *handle)
Definition: libuv/src/unix/thread.c:349
uv_sem_t
UV_PLATFORM_SEM_T uv_sem_t
Definition: unix.h:137
uv_sem_post
UV_EXTERN void uv_sem_post(uv_sem_t *sem)
Definition: libuv/src/unix/thread.c:669
TEST_IMPL
TEST_IMPL(semaphore_1)
Definition: test-semaphore.c:50
uv_sem_init
UV_EXTERN int uv_sem_init(uv_sem_t *sem, unsigned int value)
Definition: libuv/src/unix/thread.c:649
uv.h
uv_sem_destroy
UV_EXTERN void uv_sem_destroy(uv_sem_t *sem)
Definition: libuv/src/unix/thread.c:661
uv_mutex_lock
UV_EXTERN void uv_mutex_lock(uv_mutex_t *handle)
Definition: libuv/src/unix/thread.c:329
arg
struct arg arg
worker_config::delay
int delay
Definition: test-barrier.c:30
uv_thread_t
pthread_t uv_thread_t
Definition: unix.h:134
uv_sem_wait
UV_EXTERN void uv_sem_wait(uv_sem_t *sem)
Definition: libuv/src/unix/thread.c:677
worker
static void worker(void *arg)
Definition: test-semaphore.c:36
worker_config
Definition: test-barrier.c:28
thread
static uv_thread_t thread
Definition: test-async-null-cb.c:29
worker_config::mutex
uv_mutex_t mutex
Definition: test-condvar.c:36


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:30