gwion-util
utilities for the Gwion project
Loading...
Searching...
No Matches
Typedefs | Enumerations | Functions | Variables
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 13 of file threadpool.h.

◆ gwtlock_t

typedef pthread_mutex_t gwtlock_t

Definition at line 12 of file threadpool.h.

◆ gwtreturn_t

typedef void* gwtreturn_t

Definition at line 14 of file threadpool.h.

◆ gwtthread_t

typedef pthread_t gwtthread_t

Definition at line 11 of file threadpool.h.

◆ threadpool_t

typedef struct threadpool_t threadpool_t

Definition at line 17 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 19 of file threadpool.h.

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

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:101
static ANN int gwt_broadcast(gwtcond_t *cond)
Definition threadpool.h:83
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(gwtlock_t *lock)
Definition threadpool.h:71

◆ gwt_broadcast()

static ANN int gwt_broadcast ( gwtcond_t * cond)
inlinestatic

Definition at line 83 of file threadpool.h.

83 {
84 return pthread_cond_broadcast(cond);
85}

◆ gwt_cond_end()

static ANN void gwt_cond_end ( gwtcond_t * cond)
inlinestatic

Definition at line 101 of file threadpool.h.

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

◆ gwt_cond_ini()

static ANN int gwt_cond_ini ( gwtcond_t * cond)
inlinestatic

Definition at line 98 of file threadpool.h.

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

◆ gwt_create()

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

Definition at line 104 of file threadpool.h.

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

◆ gwt_join()

static ANN void gwt_join ( gwtthread_t thread)
inlinestatic

Definition at line 77 of file threadpool.h.

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

◆ gwt_lock()

static ANN int gwt_lock ( gwtlock_t * lock)
inlinestatic

Definition at line 71 of file threadpool.h.

71 {
72 return pthread_mutex_lock(lock);
73}

◆ gwt_lock_end()

static ANN void gwt_lock_end ( gwtlock_t * lock)
inlinestatic

Definition at line 95 of file threadpool.h.

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

◆ gwt_lock_ini()

static ANN int gwt_lock_ini ( gwtlock_t * lock)
inlinestatic

Definition at line 89 of file threadpool.h.

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

◆ gwt_signal()

static ANN int gwt_signal ( gwtcond_t * cond)
inlinestatic

Definition at line 86 of file threadpool.h.

86 {
87 return pthread_cond_signal(cond);
88}

◆ gwt_unlock()

static ANN int gwt_unlock ( gwtlock_t * lock)
inlinestatic

Definition at line 74 of file threadpool.h.

74 {
75 return pthread_mutex_unlock(lock);
76}

◆ gwt_wait()

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

Definition at line 80 of file threadpool.h.

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

◆ 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 27 of file threadpool.h.

◆ routine

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

Definition at line 27 of file threadpool.h.