-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadpool.h
65 lines (50 loc) · 1.68 KB
/
threadpool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#ifndef THREADPOOL_H
#define THREADPOOL_H
#include <stdio.h>
#include <stddef.h>
#include <sys/types.h>
/**
* Implementation of ThreadPool and Runnable with blocking queue.
*/
#define err(str) fprintf(stderr, str)
/* ========================== STRUCTURES ============================ */
typedef struct runnable {
void (*function)(void *, size_t);
void *arg;
size_t argsz;
} runnable_t;
typedef struct bin_sem {
pthread_mutex_t mutex;
pthread_cond_t cond;
size_t v;
} bsem;
typedef struct job {
struct job *prev; /* Pointer to the previous job */
runnable_t job;
} job;
typedef struct jobqueue {
pthread_mutex_t r_w_mutex; /* Mutex for read/write on queue */
job *front; /* Pointer to the front job in the queue */
job *rear; /* Pointer to the rear job in the queue */
bsem *has_jobs;
size_t len; /* Number of the jobs in the queue */
} jobqueue;
typedef struct thread {
pthread_t pthread; /* Pointer to the actual thread */
struct thread_pool *thread_pool_p; /* Ensures access to the thread pool */
} thread;
typedef struct thread_pool {
int id;
size_t keepAlive;
thread **threads; /* Pointer to the threads in thread pool */
volatile size_t num_threads_alive;
volatile size_t num_threads_working;
jobqueue *jobqueue;
pthread_mutex_t thcount_lock;
pthread_cond_t threads_idle;
} thread_pool_t;
/* ================================================================== */
int thread_pool_init(thread_pool_t *pool, size_t pool_size);
void thread_pool_destroy(thread_pool_t *pool);
int defer(thread_pool_t *pool, runnable_t runnable);
#endif