libsheepy
tpool.h
Go to the documentation of this file.
1 /**********************************
2  * @author Johan Hanssen Seferidis
3  * License: MIT
4  *
5  **********************************/
6 
7 #pragma once
8 
9 #if (__OpenBSD__)
10 // have to include pthread.h for pthread_mutex_t in OpenBSD with GCC 4.9.4
11 #include "pthread.h"
12 #endif
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 // locks, max 512 threads
19 #define tpoolLockCount 512
20 extern pthread_mutex_t tpoolLocks[tpoolLockCount]; // only one thread can have the lock
21 
22 // use lock in the tpoolLocks array
23 void tpoolLock(int position);
24 // unlock a lock in the tpoolLocks array
25 void tpoolUlock(int position);
26 
27 typedef struct {
28  union {
29  char *s;
30  };
31  union {
32  char *s2;
33  };
34  // return value
35  union {
36  char **list;
37  int status;
38  };
39  void (*cb)(void*);
40  void *cbArgs;
41  void *channel;
42 } tpoolArgs;
43 
44 #define tpoolAdd(task, args) tpool_add_work(tpool, task, args)
45 #define tpoolWait tpool_wait(tpool)
46 #define tpoolKill tpool_destroy(tpool)
47 #define tpoolPause tpool_pause(tpool)
48 #define tpoolResume tpool_resume(tpool)
49 #define tpoolNum tpool_num_threads_working(tpool)
50 
51 /* =================================== API ======================================= */
52 
53 
54 typedef struct tpool_* threadpool;
55 extern threadpool tpool;
56 
74 threadpool tpool_init(int num_threads);
75 
76 
104 int tpool_add_work(threadpool, void (*function_p)(void*), void* arg_p);
105 
106 
134 void tpool_wait(threadpool);
135 
136 
158 void tpool_pause(threadpool);
159 
160 
174 void tpool_resume(threadpool);
175 
176 
196 void tpool_destroy(threadpool);
197 
198 
217 int tpool_num_threads_working(threadpool);
218 
219 
220 #ifdef __cplusplus
221 }
222 #endif
int tpool_num_threads_working(threadpool)
Definition: tpool.c:304
char * s
Definition: tpool.h:29
threadpool tpool
Definition: tpool.c:47
int status
Definition: tpool.h:37
void tpoolLock(int position)
Definition: tpool.c:127
#define tpoolLockCount
Definition: tpool.h:19
void tpool_destroy(threadpool)
Definition: tpool.c:232
void tpool_pause(threadpool)
Definition: tpool.c:282
void tpoolUlock(int position)
Definition: tpool.c:132
char ** list
Definition: tpool.h:36
void tpool_wait(threadpool)
Definition: tpool.c:221
pthread_mutex_t tpoolLocks[tpoolLockCount]
Definition: tpool.c:45
void * cbArgs
Definition: tpool.h:40
threadpool tpool_init(int num_threads)
Definition: tpool.c:137
int tpool_add_work(threadpool, void(*function_p)(void *), void *arg_p)
Definition: tpool.c:192
Definition: tpool.c:87
char * s2
Definition: tpool.h:32
void tpool_resume(threadpool)
Definition: tpool.c:292
void * channel
Definition: tpool.h:41
struct tpool_ * threadpool
Definition: tpool.h:54