-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c838675
commit c7d5ad8
Showing
7 changed files
with
232 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#ifndef _THREADPOOL_H_ | ||
#define _THREADPOOL_H_ | ||
|
||
#ifdef BUILD_ON_WINDOWS | ||
typedef HANDLE gwtthread_t; | ||
typedef PCRITICAL_SECTION gwtmutex_t; | ||
typedef PCONDITION_VARIABLE gwtcond_t; | ||
typedef long unsigned gwtreturn_t; | ||
#else | ||
#include <pthread.h> | ||
typedef pthread_t gwtthread_t; | ||
typedef pthread_mutex_t gwtlock_t; | ||
typedef pthread_cond_t gwtcond_t; | ||
typedef void* gwtreturn_t; | ||
#endif | ||
|
||
typedef struct threadpool_t threadpool_t; | ||
|
||
typedef enum { | ||
threadpool_lock_failure = -2, | ||
threadpool_queue_full = -3, | ||
threadpool_shutdown = -4, | ||
threadpool_thread_failure = -5 | ||
} threadpool_error_t; | ||
|
||
threadpool_t *new_threadpool(const uint32_t thread_count, const uint32_t queue_size); | ||
ANN2(1, 2) bool threadpool_add(threadpool_t *pool, void (*routine)(void *), void *arg); | ||
ANN void free_threadpool(threadpool_t *pool); | ||
|
||
|
||
#ifdef BUILD_ON_WINDOWS | ||
ANN static inline void gwt_lock(threadpool_t *pool) { | ||
EnterCriticalSection(&pool->lock); // void | ||
} | ||
ANN static inline void gwt_unlock(threadpool_t *pool) { | ||
LeaveCriticalSection(&pool->lock); // void | ||
} | ||
ANN static inline void gwt_wait(gwtcond_t *cond, gwtlock_t *lock) { | ||
SleepConditionVariableCS(cond, lock, INFINITE); // bool | ||
} | ||
ANN static inline void gwt_broadcast(threadpool_t *pool) { | ||
WakeAllConditionVariable(&pool->cond); // void | ||
} | ||
ANN static inline void gwt_signal(threadpool_t *pool) { | ||
return WakeConditionVariable(&pool->cond); // void | ||
} | ||
ANN static inline void gwt_create(gwtthread_t *thread, void* (*fun)(void*), void *arg) { | ||
*thread = CreateThread(NULL, 0, func, arg, 0, NULL); | ||
} | ||
ANN static inline void gwt_join(gwtthread_t thread) { | ||
WaitForSingleObject(thread, INFINITE); // dword // (DWORD)0xFFFFFFFF on error | ||
} | ||
ANN static inline void gwt_lock_end(gwtlock_t *lock) { | ||
return DeleteCriticalSection(*lock); | ||
} | ||
ANN static inline void gwt_cond_end(gwtcond_t *cond) {} | ||
#else | ||
ANN static inline int gwt_lock(gwtlock_t *lock) { | ||
return pthread_mutex_lock(lock); | ||
} | ||
ANN static inline int gwt_unlock(gwtlock_t *lock) { | ||
return pthread_mutex_unlock(lock); | ||
} | ||
ANN static inline void gwt_join(gwtthread_t thread) { | ||
pthread_join(thread, NULL); | ||
} | ||
ANN static inline void gwt_wait(gwtcond_t *cond, gwtlock_t *lock) { | ||
pthread_cond_wait(cond, lock); | ||
} | ||
ANN static inline void gwt_broadcast(gwtcond_t *cond) { | ||
pthread_cond_broadcast(cond); | ||
} | ||
ANN static inline int gwt_signal(gwtcond_t *cond) { | ||
return pthread_cond_signal(cond); | ||
} | ||
ANN static inline int gwt_lock_ini(gwtlock_t *lock) { | ||
pthread_mutexattr_t attr; | ||
pthread_mutexattr_init(&attr); | ||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); | ||
return pthread_mutex_init(lock, &attr); | ||
} | ||
ANN static inline void gwt_lock_end(gwtlock_t *lock) { | ||
pthread_mutex_destroy(lock); // int | ||
} | ||
ANN static inline int gwt_cond_ini(gwtcond_t *cond) { | ||
return pthread_cond_init(cond, NULL); | ||
} | ||
ANN static inline void gwt_cond_end(gwtcond_t *cond) { | ||
pthread_cond_destroy(cond); // int | ||
} | ||
ANN static inline bool gwt_create(gwtthread_t *thread, gwtreturn_t (*fun)(void*), void *arg) { | ||
return !pthread_create(thread, NULL, fun, arg); // int | ||
} | ||
#endif | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#include "gwion_util.h" | ||
|
||
typedef struct { | ||
void (*fun)(void *); | ||
void *arg; | ||
} task_t; | ||
|
||
struct threadpool_t { | ||
gwtlock_t lock; | ||
gwtcond_t cond; | ||
gwtthread_t *threads; | ||
task_t *queue; | ||
uint32_t head; | ||
uint32_t tail; | ||
uint32_t active; | ||
uint32_t queue_size; | ||
uint32_t started; | ||
bool shutdown; | ||
bool has_lock; | ||
bool has_cond; | ||
}; | ||
|
||
ANN static gwtreturn_t threadpool_thread(void *data) { | ||
threadpool_t *p = (threadpool_t *)data; | ||
while(true) { | ||
(void)gwt_lock(&p->lock); | ||
while(!p->active && !p->shutdown) | ||
(void)gwt_wait(&p->cond, &p->lock); | ||
if(p->shutdown) break; | ||
task_t task = p->queue[p->head]; | ||
p->head = (p->head + 1) % p->queue_size; | ||
p->active--; | ||
(void)gwt_unlock(&p->lock); | ||
task.fun(task.arg); | ||
} | ||
(void)gwt_unlock(&p->lock); | ||
THREAD_RETURN(NULL); | ||
} | ||
|
||
ANN static bool alloc(threadpool_t *p, const uint32_t thread_count, | ||
const uint32_t queue_size) { | ||
return !(p->threads = malloc(thread_count * sizeof(gwtthread_t))) || | ||
!(p->queue = malloc(queue_size * sizeof(task_t))); | ||
} | ||
|
||
ANN static bool utils(threadpool_t *p) { | ||
if(gwt_lock_ini(&p->lock)) return false; | ||
p->has_lock = true; | ||
if(gwt_cond_ini(&p->cond)) return false; | ||
p->has_cond = true; | ||
return true; | ||
} | ||
|
||
ANN static bool start(threadpool_t *p, const uint32_t thread_count) { | ||
for(uint32_t i = 0; i < thread_count; i++) { | ||
if(gwt_create(&p->threads[i], threadpool_thread, p)) | ||
return false; | ||
p->started++; | ||
} | ||
return true; | ||
} | ||
|
||
threadpool_t *new_threadpool(const uint32_t thread_count, const uint32_t queue_size) { | ||
threadpool_t *p = malloc(sizeof(threadpool_t)); | ||
if(!p) return NULL; | ||
p->head = p->tail = p->active = 0; | ||
p->shutdown = p->has_lock = p->has_cond = false; | ||
p->started = 0; | ||
if(alloc(p, thread_count, queue_size) || !utils(p) || | ||
start(p, thread_count)) { | ||
free_threadpool(p); | ||
return NULL; | ||
} | ||
return p; | ||
} | ||
|
||
ANN2(1, 2) static bool add(threadpool_t *p, void (*fun)(void *), void *arg) { | ||
if(unlikely(p->shutdown || p->active == p->queue_size)) | ||
return false; | ||
const uint32_t next = (p->tail + 1) % p->queue_size; | ||
task_t t = { .fun = fun, .arg = arg }; | ||
p->queue[p->tail] = t; | ||
p->tail = next; | ||
p->active++; | ||
(void)gwt_signal(&p->cond); | ||
return true; | ||
} | ||
|
||
bool threadpool_add(threadpool_t *p, void (*fun)(void *), void *arg) { | ||
(void)gwt_lock(&p->lock); | ||
const bool ret = add(p, fun, arg); | ||
(void)gwt_unlock(&p->lock); | ||
return ret; | ||
} | ||
|
||
ANN void free_threadpool(threadpool_t *p) { | ||
(void)gwt_lock(&p->lock); | ||
p->shutdown = true; | ||
gwt_broadcast(&p->cond); | ||
(void)gwt_unlock(&p->lock); | ||
if(p->threads) { | ||
for(uint32_t i = 0; i < p->started; i++) | ||
gwt_join(p->threads[i]); | ||
free(p->threads); | ||
} | ||
if(p->queue) free(p->queue); | ||
if(p->has_lock) gwt_lock_end(&p->lock); | ||
if(p->has_cond) gwt_cond_end(&p->cond); | ||
} |