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