2 * Copyright (c) 2014-2018, Intel Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
29 * It looks like there is still no support for C11's threads.h.
31 * We implement the few features we actually need hoping that this file will
40 #ifndef PTHREAD_MUTEX_NORMAL
41 # define PTHREAD_MUTEX_NORMAL PTHREAD_MUTEX_TIMED_NP
55 typedef struct pt_thread thrd_t;
57 typedef int (*thrd_start_t)(void *);
65 static void *thrd_routine(void *arg)
67 struct thrd_args *args;
72 return (void *) (intptr_t) -1;
76 result = args->fun(args->arg);
80 return (void *) (intptr_t) result;
83 static inline int thrd_create(thrd_t *thrd, thrd_start_t fun, void *arg)
85 struct thrd_args *args;
91 args = malloc(sizeof(*args));
98 errcode = pthread_create(&thrd->thread, NULL, thrd_routine, args);
107 static inline int thrd_join(thrd_t *thrd, int *res)
115 errcode = pthread_join(thrd->thread, &result);
120 *res = (int) (intptr_t) result;
127 pthread_mutex_t mutex;
129 typedef struct pt_mutex mtx_t;
132 mtx_plain = PTHREAD_MUTEX_NORMAL
135 static inline int mtx_init(mtx_t *mtx, int type)
139 if (!mtx || type != mtx_plain)
142 errcode = pthread_mutex_init(&mtx->mutex, NULL);
149 static inline void mtx_destroy(mtx_t *mtx)
152 (void) pthread_mutex_destroy(&mtx->mutex);
155 static inline int mtx_lock(mtx_t *mtx)
162 errcode = pthread_mutex_lock(&mtx->mutex);
169 static inline int mtx_unlock(mtx_t *mtx)
176 errcode = pthread_mutex_unlock(&mtx->mutex);
187 typedef struct pt_cond cnd_t;
189 static inline int cnd_init(cnd_t *cnd)
196 errcode = pthread_cond_init(&cnd->cond, NULL);
203 static inline int cnd_destroy(cnd_t *cnd)
210 errcode = pthread_cond_destroy(&cnd->cond);
217 static inline int cnd_signal(cnd_t *cnd)
224 errcode = pthread_cond_signal(&cnd->cond);
231 static inline int cnd_broadcast(cnd_t *cnd)
238 errcode = pthread_cond_broadcast(&cnd->cond);
245 static inline int cnd_wait(cnd_t *cnd, mtx_t *mtx)
252 errcode = pthread_cond_wait(&cnd->cond, &mtx->mutex);
259 #endif /* THREADS_H */