1 /*        $NetBSD: mtx.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: mtx.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 void
mtx_destroy(mtx_t * mtx)41 mtx_destroy(mtx_t *mtx)
42 {
43 
44           _DIAGASSERT(mtx != NULL);
45 
46           /*
47            * The cnd_destroy(3) function that conforms to C11 returns no value.
48            */
49           (void)pthread_mutex_destroy(mtx);
50 }
51 
52 static inline int
mtx_init_default(mtx_t * mtx)53 mtx_init_default(mtx_t *mtx)
54 {
55 
56           _DIAGASSERT(mtx != NULL);
57 
58           if (pthread_mutex_init(mtx, NULL) != 0)
59                     return thrd_error;
60 
61         return thrd_success;
62 }
63 
64 static inline int
mtx_init_recursive(mtx_t * mtx)65 mtx_init_recursive(mtx_t *mtx)
66 {
67           pthread_mutexattr_t attr;
68 
69           _DIAGASSERT(mtx != NULL);
70 
71           if (pthread_mutexattr_init(&attr) != 0)
72                     return thrd_error;
73 
74           if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0) {
75                     pthread_mutexattr_destroy(&attr);
76 
77                     return thrd_error;
78           }
79 
80           if (pthread_mutex_init(mtx, &attr) == 0)
81                     return thrd_success;
82 
83           pthread_mutexattr_destroy(&attr);
84 
85           return thrd_error;
86 }
87 
88 int
mtx_init(mtx_t * mtx,int type)89 mtx_init(mtx_t *mtx, int type)
90 {
91 
92           _DIAGASSERT(mtx != NULL);
93 
94           switch (type) {
95           case mtx_plain:
96           case mtx_timed:
97                     return mtx_init_default(mtx);
98           case mtx_plain | mtx_recursive:
99           case mtx_timed | mtx_recursive:
100                     return mtx_init_recursive(mtx);
101           default:
102                     return thrd_error;
103           }
104 }
105 
106 int
mtx_lock(mtx_t * mtx)107 mtx_lock(mtx_t *mtx)
108 {
109 
110           _DIAGASSERT(mtx != NULL);
111 
112           if (pthread_mutex_lock(mtx) == 0)
113                     return thrd_success;
114 
115           return thrd_error;
116 }
117 
118 int
mtx_timedlock(mtx_t * __restrict mtx,const struct timespec * __restrict ts)119 mtx_timedlock(mtx_t *__restrict mtx, const struct timespec *__restrict ts)
120 {
121 
122           _DIAGASSERT(mtx != NULL);
123           _DIAGASSERT(ts != NULL);
124 
125           switch(pthread_mutex_timedlock(mtx, ts)) {
126           case 0:
127                     return thrd_success;
128           case ETIMEDOUT:
129                     return thrd_timedout;
130           default:
131                     return thrd_error;
132           }
133 }
134 
135 int
mtx_trylock(mtx_t * mtx)136 mtx_trylock(mtx_t *mtx)
137 {
138 
139           _DIAGASSERT(mtx != NULL);
140 
141           switch(pthread_mutex_trylock(mtx)) {
142           case 0:
143                     return thrd_success;
144           case EBUSY:
145                     return thrd_busy;
146           default:
147                     return thrd_error;
148           }
149 }
150 
151 int
mtx_unlock(mtx_t * mtx)152 mtx_unlock(mtx_t *mtx)
153 {
154 
155           _DIAGASSERT(mtx != NULL);
156 
157           if (pthread_mutex_unlock(mtx) == 0)
158                     return thrd_success;
159 
160           return thrd_error;
161 }
162