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
50 typedef struct pt_thread thrd_t;
52 typedef int (*thrd_start_t)(void *);
60 static DWORD WINAPI thrd_routine(void *arg)
62 struct thrd_args *args;
65 args = (struct thrd_args *) arg;
71 result = args->fun(args->arg);
75 return (DWORD) result;
78 static inline int thrd_create(thrd_t *thrd, thrd_start_t fun, void *arg)
80 struct thrd_args *args;
86 args = malloc(sizeof(*args));
93 handle = CreateThread(NULL, 0, thrd_routine, args, 0, NULL);
99 thrd->handle = handle;
103 static inline int thrd_join(thrd_t *thrd, int *res)
111 status = WaitForSingleObject(thrd->handle, INFINITE);
118 success = GetExitCodeThread(thrd->handle, &result);
120 (void) CloseHandle(thrd->handle);
127 success = CloseHandle(thrd->handle);
137 typedef struct pt_mutex mtx_t;
143 static inline int mtx_init(mtx_t *mtx, int type)
145 if (!mtx || type != mtx_plain)
148 InitializeCriticalSection(&mtx->cs);
153 static inline void mtx_destroy(mtx_t *mtx)
156 DeleteCriticalSection(&mtx->cs);
159 static inline int mtx_lock(mtx_t *mtx)
164 EnterCriticalSection(&mtx->cs);
169 static inline int mtx_unlock(mtx_t *mtx)
174 LeaveCriticalSection(&mtx->cs);
181 CONDITION_VARIABLE cond;
183 typedef struct pt_cond cnd_t;
185 static inline int cnd_init(cnd_t *cnd)
190 InitializeConditionVariable(&cnd->cond);
195 static inline int cnd_destroy(cnd_t *cnd)
205 static inline int cnd_signal(cnd_t *cnd)
210 WakeConditionVariable(&cnd->cond);
215 static inline int cnd_broadcast(cnd_t *cnd)
220 WakeAllConditionVariable(&cnd->cond);
225 static inline int cnd_wait(cnd_t *cnd, mtx_t *mtx)
232 success = SleepConditionVariableCS(&cnd->cond, &mtx->cs, INFINITE);
239 #endif /* THREADS_H */