gwion-util
utilities for the Gwion project
Loading...
Searching...
No Matches
threadpool.h File Reference
#include <pthread.h>

Go to the source code of this file.

Typedefs

typedef pthread_t gwtthread_t
 
typedef pthread_mutex_t gwtlock_t
 
typedef pthread_cond_t gwtcond_t
 
typedef void * gwtreturn_t
 
typedef struct threadpool_t threadpool_t
 

Enumerations

enum  threadpool_error_t { threadpool_lock_failure = -2 , threadpool_queue_full = -3 , threadpool_shutdown = -4 , threadpool_thread_failure = -5 }
 

Functions

threadpool_tnew_threadpool (const uint32_t thread_count, const uint32_t queue_size)
 
 ANN2 (1, 2) bool threadpool_add(threadpool_t *pool
 
ANN void free_threadpool (threadpool_t *pool)
 
static ANN int gwt_lock (gwtlock_t *lock)
 
static ANN int gwt_unlock (gwtlock_t *lock)
 
static ANN void gwt_join (gwtthread_t thread)
 
static ANN void gwt_wait (gwtcond_t *cond, gwtlock_t *lock)
 
static ANN int gwt_broadcast (gwtcond_t *cond)
 
static ANN int gwt_signal (gwtcond_t *cond)
 
static ANN int gwt_lock_ini (gwtlock_t *lock)
 
static ANN void gwt_lock_end (gwtlock_t *lock)
 
static ANN int gwt_cond_ini (gwtcond_t *cond)
 
static ANN void gwt_cond_end (gwtcond_t *cond)
 
static ANN bool gwt_create (gwtthread_t *thread, gwtreturn_t(*fun)(void *), void *arg)
 

Variables

void(* routine )(void *)
 
void void * arg
 

Typedef Documentation

◆ gwtcond_t

typedef pthread_cond_t gwtcond_t

Definition at line 12 of file threadpool.h.

◆ gwtlock_t

typedef pthread_mutex_t gwtlock_t

Definition at line 11 of file threadpool.h.

◆ gwtreturn_t

typedef void* gwtreturn_t

Definition at line 13 of file threadpool.h.

◆ gwtthread_t

typedef pthread_t gwtthread_t

Definition at line 10 of file threadpool.h.

◆ threadpool_t

typedef struct threadpool_t threadpool_t

Definition at line 16 of file threadpool.h.

Enumeration Type Documentation

◆ threadpool_error_t

Enumerator
threadpool_lock_failure 
threadpool_queue_full 
threadpool_shutdown 
threadpool_thread_failure 

Definition at line 18 of file threadpool.h.

18 {
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

Function Documentation

◆ ANN2()

ANN2 ( 1 ,
2  )

◆ free_threadpool()

ANN void free_threadpool ( threadpool_t * pool)

Definition at line 100 of file threadpool.c.

100 {
101 (void)gwt_lock(&p->lock);
102 p->shutdown = true;
103 gwt_broadcast(&p->cond);
104 (void)gwt_unlock(&p->lock);
105 if(p->threads) {
106 for(uint32_t i = 0; i < p->started; i++)
107 gwt_join(p->threads[i]);
108 free(p->threads);
109 }
110 if(p->queue) free(p->queue);
111 if(p->has_lock) gwt_lock_end(&p->lock);
112 if(p->has_cond) gwt_cond_end(&p->cond);
113}
static ANN void gwt_cond_end(gwtcond_t *cond)
Definition threadpool.h:100
static ANN int gwt_broadcast(gwtcond_t *cond)
Definition threadpool.h:82
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(gwtlock_t *lock)
Definition threadpool.h:70

◆ gwt_broadcast()

static ANN int gwt_broadcast ( gwtcond_t * cond)
inlinestatic

Definition at line 82 of file threadpool.h.

82 {
83 return pthread_cond_broadcast(cond);
84}

◆ gwt_cond_end()

static ANN void gwt_cond_end ( gwtcond_t * cond)
inlinestatic

Definition at line 100 of file threadpool.h.

100 {
101 pthread_cond_destroy(cond); // int
102}

◆ gwt_cond_ini()

static ANN int gwt_cond_ini ( gwtcond_t * cond)
inlinestatic

Definition at line 97 of file threadpool.h.

97 {
98 return pthread_cond_init(cond, NULL);
99}

◆ gwt_create()

static ANN bool gwt_create ( gwtthread_t * thread,
gwtreturn_t(* fun )(void *),
void * arg )
inlinestatic

Definition at line 103 of file threadpool.h.

103 {
104 return pthread_create(thread, NULL, fun, arg); // int
105}
void void * arg
Definition threadpool.h:26

◆ gwt_join()

static ANN void gwt_join ( gwtthread_t thread)
inlinestatic

Definition at line 76 of file threadpool.h.

76 {
77 pthread_join(thread, NULL);
78}

◆ gwt_lock()

static ANN int gwt_lock ( gwtlock_t * lock)
inlinestatic

Definition at line 70 of file threadpool.h.

70 {
71 return pthread_mutex_lock(lock);
72}

◆ gwt_lock_end()

static ANN void gwt_lock_end ( gwtlock_t * lock)
inlinestatic

Definition at line 94 of file threadpool.h.

94 {
95 pthread_mutex_destroy(lock); // int
96}

◆ gwt_lock_ini()

static ANN int gwt_lock_ini ( gwtlock_t * lock)
inlinestatic

Definition at line 88 of file threadpool.h.

88 {
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}

◆ gwt_signal()

static ANN int gwt_signal ( gwtcond_t * cond)
inlinestatic

Definition at line 85 of file threadpool.h.

85 {
86 return pthread_cond_signal(cond);
87}

◆ gwt_unlock()

static ANN int gwt_unlock ( gwtlock_t * lock)
inlinestatic

Definition at line 73 of file threadpool.h.

73 {
74 return pthread_mutex_unlock(lock);
75}

◆ gwt_wait()

static ANN void gwt_wait ( gwtcond_t * cond,
gwtlock_t * lock )
inlinestatic

Definition at line 79 of file threadpool.h.

79 {
80 pthread_cond_wait(cond, lock);
81}

◆ new_threadpool()

threadpool_t * new_threadpool ( const uint32_t thread_count,
const uint32_t queue_size )

Definition at line 66 of file threadpool.c.

66 {
67 threadpool_t *p = malloc(sizeof(threadpool_t));
68 if(!p) return NULL;
69 p->head = p->tail = p->active = 0;
70 p->shutdown = p->has_lock = p->has_cond = false;
71 p->started = 0;
72 p->queue_size = queue_size;
73 if(alloc(p, thread_count, queue_size) || !utils(p) ||
74 !start(p, thread_count)) {
76 return NULL;
77 }
78 return p;
79}
uint32_t started
Definition threadpool.c:17
uint32_t queue_size
Definition threadpool.c:16
uint32_t tail
Definition threadpool.c:14
uint32_t head
Definition threadpool.c:13
uint32_t active
Definition threadpool.c:15
static ANN bool start(threadpool_t *p, const uint32_t thread_count)
Definition threadpool.c:57
static ANN bool utils(threadpool_t *p)
Definition threadpool.c:46
static ANN bool alloc(threadpool_t *p, const uint32_t thread_count, const uint32_t queue_size)
Definition threadpool.c:40
ANN void free_threadpool(threadpool_t *p)
Definition threadpool.c:100

Variable Documentation

◆ arg

void void* arg

Definition at line 26 of file threadpool.h.

◆ routine

void(* routine) (void *) ( void * )

Definition at line 26 of file threadpool.h.