1 /* $NetBSD: cnd.c,v 1.1 2019/04/24 11:43:19 kamil Exp $ */
2
3 /*-
4 * Copyright (c) 2016 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Kamil Rytarowski.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: cnd.c,v 1.1 2019/04/24 11:43:19 kamil Exp $");
34
35 #include <assert.h>
36 #include <errno.h>
37 #include <pthread.h>
38 #include <threads.h>
39
40 int
cnd_broadcast(cnd_t * cond)41 cnd_broadcast(cnd_t *cond)
42 {
43
44 _DIAGASSERT(cond != NULL);
45
46 if (pthread_cond_broadcast(cond) == 0)
47 return thrd_success;
48
49 return thrd_error;
50 }
51
52 void
cnd_destroy(cnd_t * cond)53 cnd_destroy(cnd_t *cond)
54 {
55
56 _DIAGASSERT(cond != NULL);
57
58 /*
59 * The cnd_destroy(3) function that conforms to C11 returns no value.
60 */
61 (void)pthread_cond_destroy(cond);
62 }
63
64 int
cnd_init(cnd_t * cond)65 cnd_init(cnd_t *cond)
66 {
67
68 _DIAGASSERT(cond != NULL);
69
70 if (pthread_cond_init(cond, NULL) == 0)
71 return thrd_success;
72
73 return thrd_error;
74 }
75
76 int
cnd_signal(cnd_t * cond)77 cnd_signal(cnd_t *cond)
78 {
79
80 _DIAGASSERT(cond != NULL);
81
82 if (pthread_cond_signal(cond) == 0)
83 return thrd_success;
84
85 return thrd_error;
86 }
87
88 int
cnd_timedwait(cnd_t * __restrict cond,mtx_t * __restrict mtx,const struct timespec * __restrict ts)89 cnd_timedwait(cnd_t * __restrict cond, mtx_t * __restrict mtx,
90 const struct timespec * __restrict ts)
91 {
92
93 _DIAGASSERT(cond != NULL);
94 _DIAGASSERT(mtx != NULL);
95 _DIAGASSERT(ts != NULL);
96
97 switch (pthread_cond_timedwait(cond, mtx, ts)) {
98 case 0:
99 return thrd_success;
100 case ETIMEDOUT:
101 return thrd_timedout;
102 default:
103 return thrd_error;
104 }
105 }
106
107 int
cnd_wait(cnd_t * cond,mtx_t * mtx)108 cnd_wait(cnd_t *cond, mtx_t *mtx)
109 {
110
111 _DIAGASSERT(cond != NULL);
112 _DIAGASSERT(mtx != NULL);
113
114 if (pthread_cond_wait(cond, mtx) == 0)
115 return thrd_success;
116
117 return thrd_error;
118 }
119