1 /*
2  * Copyright (c) 2014-2019, Intel Corporation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *  * Redistributions of source code must retain the above copyright notice,
8  *    this list of conditions and the following disclaimer.
9  *  * Redistributions in binary form must reproduce the above copyright notice,
10  *    this list of conditions and the following disclaimer in the documentation
11  *    and/or other materials provided with the distribution.
12  *  * Neither the name of Intel Corporation nor the names of its contributors
13  *    may be used to endorse or promote products derived from this software
14  *    without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  *
28  *
29  * It looks like there is still no support for C11's threads.h.
30  *
31  * We implement the few features we actually need hoping that this file will
32  * soon go away.
33  */
34 
35 #ifndef THREADS_H
36 #define THREADS_H
37 
38 #include <pthread.h>
39 
40 #ifndef PTHREAD_MUTEX_NORMAL
41 #  define PTHREAD_MUTEX_NORMAL PTHREAD_MUTEX_TIMED_NP
42 #endif
43 
44 #include <stdint.h>
45 #include <stdlib.h>
46 
47 enum {
48 	thrd_success	= 1,
49 	thrd_error
50 };
51 
52 struct pt_thread {
53 	pthread_t thread;
54 };
55 typedef struct pt_thread thrd_t;
56 
57 typedef int (*thrd_start_t)(void *);
58 
59 
60 struct thrd_args {
61 	thrd_start_t fun;
62 	void *arg;
63 };
64 
65 static void *thrd_routine(void *arg)
66 {
67 	struct thrd_args *args;
68 	int result;
69 
70 	args = arg;
71 	if (!args)
72 		return (void *) (intptr_t) -1;
73 
74 	result = -1;
75 	if (args->fun)
76 		result = args->fun(args->arg);
77 
78 	free(args);
79 
80 	return (void *) (intptr_t) result;
81 }
82 
83 static inline int thrd_create(thrd_t *thrd, thrd_start_t fun, void *arg)
84 {
85 	struct thrd_args *args;
86 	int errcode;
87 
88 	if (!thrd || !fun)
89 		return thrd_error;
90 
91 	args = malloc(sizeof(*args));
92 	if (!args)
93 		return thrd_error;
94 
95 	args->fun = fun;
96 	args->arg = arg;
97 
98 	errcode = pthread_create(&thrd->thread, NULL, thrd_routine, args);
99 	if (errcode) {
100 		free(args);
101 		return thrd_error;
102 	}
103 
104 	return thrd_success;
105 }
106 
107 static inline int thrd_join(thrd_t *thrd, int *res)
108 {
109 	void *result;
110 	int errcode;
111 
112 	if (!thrd)
113 		return thrd_error;
114 
115 	errcode = pthread_join(thrd->thread, &result);
116 	if (errcode)
117 		return thrd_error;
118 
119 	if (res)
120 		*res = (int) (intptr_t) result;
121 
122 	return thrd_success;
123 }
124 
125 
126 struct pt_mutex {
127 	pthread_mutex_t mutex;
128 };
129 typedef struct pt_mutex mtx_t;
130 
131 enum {
132 	mtx_plain = PTHREAD_MUTEX_NORMAL
133 };
134 
135 static inline int mtx_init(mtx_t *mtx, int type)
136 {
137 	int errcode;
138 
139 	if (!mtx || type != mtx_plain)
140 		return thrd_error;
141 
142 	errcode = pthread_mutex_init(&mtx->mutex, NULL);
143 	if (errcode)
144 		return thrd_error;
145 
146 	return thrd_success;
147 }
148 
149 static inline void mtx_destroy(mtx_t *mtx)
150 {
151 	if (mtx)
152 		(void) pthread_mutex_destroy(&mtx->mutex);
153 }
154 
155 static inline int mtx_lock(mtx_t *mtx)
156 {
157 	int errcode;
158 
159 	if (!mtx)
160 		return thrd_error;
161 
162 	errcode = pthread_mutex_lock(&mtx->mutex);
163 	if (errcode)
164 		return thrd_error;
165 
166 	return thrd_success;
167 }
168 
169 static inline int mtx_unlock(mtx_t *mtx)
170 {
171 	int errcode;
172 
173 	if (!mtx)
174 		return thrd_error;
175 
176 	errcode = pthread_mutex_unlock(&mtx->mutex);
177 	if (errcode)
178 		return thrd_error;
179 
180 	return thrd_success;
181 }
182 
183 
184 struct pt_cond {
185 	pthread_cond_t cond;
186 };
187 typedef struct pt_cond cnd_t;
188 
189 static inline int cnd_init(cnd_t *cnd)
190 {
191 	int errcode;
192 
193 	if (!cnd)
194 		return thrd_error;
195 
196 	errcode = pthread_cond_init(&cnd->cond, NULL);
197 	if (errcode)
198 		return thrd_error;
199 
200 	return thrd_success;
201 }
202 
203 static inline int cnd_destroy(cnd_t *cnd)
204 {
205 	int errcode;
206 
207 	if (!cnd)
208 		return thrd_error;
209 
210 	errcode = pthread_cond_destroy(&cnd->cond);
211 	if (errcode)
212 		return thrd_error;
213 
214 	return thrd_success;
215 }
216 
217 static inline int cnd_signal(cnd_t *cnd)
218 {
219 	int errcode;
220 
221 	if (!cnd)
222 		return thrd_error;
223 
224 	errcode = pthread_cond_signal(&cnd->cond);
225 	if (errcode)
226 		return thrd_error;
227 
228 	return thrd_success;
229 }
230 
231 static inline int cnd_broadcast(cnd_t *cnd)
232 {
233 	int errcode;
234 
235 	if (!cnd)
236 		return thrd_error;
237 
238 	errcode = pthread_cond_broadcast(&cnd->cond);
239 	if (errcode)
240 		return thrd_error;
241 
242 	return thrd_success;
243 }
244 
245 static inline int cnd_wait(cnd_t *cnd, mtx_t *mtx)
246 {
247 	int errcode;
248 
249 	if (!cnd || !mtx)
250 		return thrd_error;
251 
252 	errcode = pthread_cond_wait(&cnd->cond, &mtx->mutex);
253 	if (errcode)
254 		return thrd_error;
255 
256 	return thrd_success;
257 }
258 
259 #endif /* THREADS_H */
260