gwion-util
utilities for the Gwion project
Loading...
Searching...
No Matches
threadpool.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef BUILD_ON_WINDOWS
4typedef HANDLE gwtthread_t;
5typedef PCRITICAL_SECTION gwtlock_t;
6typedef PCONDITION_VARIABLE gwtcond_t;
7typedef long unsigned gwtreturn_t;
8#else
9#include <pthread.h>
10typedef pthread_t gwtthread_t;
11typedef pthread_mutex_t gwtlock_t;
12typedef pthread_cond_t gwtcond_t;
13typedef void* gwtreturn_t;
14#endif
15
17
24
25threadpool_t *new_threadpool(const uint32_t thread_count, const uint32_t queue_size);
26ANN2(1, 2) bool threadpool_add(threadpool_t *pool, void (*routine)(void *), void *arg);
28
29
30#ifdef BUILD_ON_WINDOWS
31ANN static inline int gwt_lock(gwtlock_t *lock) {
32 EnterCriticalSection(*lock);
33 return 0;
34}
35ANN static inline int gwt_unlock(gwtlock_t *lock) {
36 LeaveCriticalSection(*lock);
37 return 0;
38}
39ANN static inline void gwt_wait(gwtcond_t *cond, gwtlock_t *lock) {
40 SleepConditionVariableCS(*cond, *lock, INFINITE); // bool
41}
42ANN static inline int gwt_broadcast(gwtcond_t *cond) {
43 WakeAllConditionVariable(*cond);
44 return 0;
45}
46ANN static inline int gwt_signal(gwtcond_t *cond) {
47 WakeConditionVariable(*cond);
48 return 0;
49}
50ANN static inline int gwt_create(gwtthread_t *thread, gwtreturn_t (*fun)(void*), void *arg) {
51 *thread = CreateThread(NULL, 0, fun, arg, 0, NULL);
52 return !!*thread;
53}
54ANN static inline void gwt_join(gwtthread_t thread) {
55 WaitForSingleObject(thread, INFINITE); // dword // (DWORD)0xFFFFFFFF on error
56}
57ANN static inline void gwt_lock_end(gwtlock_t *lock) {
58 return DeleteCriticalSection(*lock);
59}
60ANN static inline int gwt_lock_ini(gwtlock_t *lock) {
61 InitializeCriticalSection(*lock);
62 return 0;
63}
64ANN static inline int gwt_cond_end(gwtcond_t *cond NUSED) { return 0;}
65ANN static inline int gwt_cond_ini(gwtcond_t *cond) {
66 InitializeConditionVariable(*cond);
67 return 0;
68}
69#else
70ANN static inline int gwt_lock(gwtlock_t *lock) {
71 return pthread_mutex_lock(lock);
72}
73ANN static inline int gwt_unlock(gwtlock_t *lock) {
74 return pthread_mutex_unlock(lock);
75}
76ANN static inline void gwt_join(gwtthread_t thread) {
77 pthread_join(thread, NULL);
78}
79ANN static inline void gwt_wait(gwtcond_t *cond, gwtlock_t *lock) {
80 pthread_cond_wait(cond, lock);
81}
82ANN static inline int gwt_broadcast(gwtcond_t *cond) {
83 return pthread_cond_broadcast(cond);
84}
85ANN static inline int gwt_signal(gwtcond_t *cond) {
86 return pthread_cond_signal(cond);
87}
88ANN static inline int gwt_lock_ini(gwtlock_t *lock) {
89 pthread_mutexattr_t attr;
90 pthread_mutexattr_init(&attr);
91 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
92 return pthread_mutex_init(lock, &attr);
93}
94ANN static inline void gwt_lock_end(gwtlock_t *lock) {
95 pthread_mutex_destroy(lock); // int
96}
97ANN static inline int gwt_cond_ini(gwtcond_t *cond) {
98 return pthread_cond_init(cond, NULL);
99}
100ANN static inline void gwt_cond_end(gwtcond_t *cond) {
101 pthread_cond_destroy(cond); // int
102}
103ANN static inline bool gwt_create(gwtthread_t *thread, gwtreturn_t (*fun)(void*), void *arg) {
104 return pthread_create(thread, NULL, fun, arg); // int
105}
106#endif
#define NUSED
Definition defs.h:22
#define ANN
Definition defs.h:18
#define ANN2(...)
Definition defs.h:19
Definition mpool.c:15
gwtlock_t lock
Definition threadpool.c:9
uint32_t queue_size
Definition threadpool.c:16
gwtcond_t cond
Definition threadpool.c:10
bool threadpool_add(threadpool_t *p, void(*fun)(void *), void *arg)
Definition threadpool.c:93
ANN void free_threadpool(threadpool_t *p)
Definition threadpool.c:100
pthread_cond_t gwtcond_t
Definition threadpool.h:12
static ANN bool gwt_create(gwtthread_t *thread, gwtreturn_t(*fun)(void *), void *arg)
Definition threadpool.h:103
static ANN void gwt_cond_end(gwtcond_t *cond)
Definition threadpool.h:100
static ANN int gwt_cond_ini(gwtcond_t *cond)
Definition threadpool.h:97
void(* routine)(void *)
Definition threadpool.h:26
pthread_mutex_t gwtlock_t
Definition threadpool.h:11
void * gwtreturn_t
Definition threadpool.h:13
static ANN int gwt_broadcast(gwtcond_t *cond)
Definition threadpool.h:82
threadpool_error_t
Definition threadpool.h:18
@ threadpool_thread_failure
Definition threadpool.h:22
@ threadpool_lock_failure
Definition threadpool.h:19
@ threadpool_shutdown
Definition threadpool.h:21
@ threadpool_queue_full
Definition threadpool.h:20
static ANN int gwt_unlock(gwtlock_t *lock)
Definition threadpool.h:73
static ANN void gwt_join(gwtthread_t thread)
Definition threadpool.h:76
static ANN void gwt_lock_end(gwtlock_t *lock)
Definition threadpool.h:94
static ANN int gwt_lock_ini(gwtlock_t *lock)
Definition threadpool.h:88
static ANN int gwt_signal(gwtcond_t *cond)
Definition threadpool.h:85
static ANN void gwt_wait(gwtcond_t *cond, gwtlock_t *lock)
Definition threadpool.h:79
static ANN int gwt_lock(gwtlock_t *lock)
Definition threadpool.h:70
void void * arg
Definition threadpool.h:26
pthread_t gwtthread_t
Definition threadpool.h:10
threadpool_t * new_threadpool(const uint32_t thread_count, const uint32_t queue_size)
Definition threadpool.c:66