-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththpool.h
293 lines (193 loc) · 6.63 KB
/
thpool.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
2020 © Copyright (c) BiDaE Technology Inc.
Provided under BiDaE SHAREWARE LICENSE-1.0 in the LICENSE.
Project Name:
BeDIS
File Name:
thpool.h
File Description:
This file the definitions and declarations of constants, structures, and
functions used in the thpool.c file.
Note: This code is forked from https://github.com/Pithikos/C-Thread-Pool
Author: Johan Hanssen Seferidis
License: MIT
Version:
2.0, 20190617
Abstract:
BeDIS uses LBeacons to deliver 3D coordinates and textual descriptions of
their locations to users' devices. Basically, a LBeacon is an inexpensive,
Bluetooth Smart Ready device. The 3D coordinates and location description
of every LBeacon are retrieved from BeDIS (Building/environment Data and
Information System) and stored locally during deployment and maintenance
times. Once initialized, each LBeacon broadcasts its coordinates and
location description to Bluetooth enabled user devices within its coverage
area.
Authors:
Holly Wang , [email protected]
Gary Xiao , [email protected]
*/
//TODO thread priority
#ifndef THPOOL_H
#define THPOOL_H
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
#include <time.h>
#include "Mempool.h"
/* The number of slots for the memory pool */
#define SLOTS_FOR_MEM_POOL_PER_THREAD 20
/* The size of the slot for the memory pool */
#define SIZE_OF_SLOT 512
#define WAITING_TIME 50
#define err(str) fprintf(stderr, str)
/* ========================== STRUCTURES ============================ */
/* Binary semaphore */
typedef struct bsem {
pthread_mutex_t mutex;
pthread_cond_t cond;
int v;
} bsem;
/* Job */
typedef struct job{
/* A pointer pointer to the previous job */
struct job *prev;
/* A pointer point to the function to be called */
void (*function)(void *arg);
/* A argument for the function pointer */
void *arg;
/* The priority nice level of this job */
int priority;
} job;
/* Job queue */
typedef struct jobqueue{
/* A mutex use for controling the job queue read/write access */
pthread_mutex_t rwmutex;
/* A pointer point to the head of the job queue */
job *front;
/* A pointer point to the tail of the job queue */
job *rear;
/* A binary semaphore flag to identify if there has jobs */
bsem *has_jobs;
/* The number of jobs in the job queue */
int len;
} jobqueue;
/* Thread */
typedef struct thread{
/* The number defined by the thread pool */
int id;
pthread_t pthread;
/* A pointer points to the curret thread pool */
struct thpool_ *thpool_p;
} thread;
/* Threadpool */
typedef struct thpool_{
/* A pointer point to threads */
thread **threads;
/* The number of threads currently alive */
volatile int num_threads_alive;
/* The nnumber of threads currently working */
volatile int num_threads_working;
/* A mutex use for counting threads */
pthread_mutex_t thcount_lock;
jobqueue jobqueue;
volatile int threads_keepalive;
/* Memory pools for the allocation of all variable in the thpool
including thread, bsem and job */
Memory_Pool mempool;
int mempool_size;
} thpool_;
typedef thpool_ *Threadpool;
/* ========================== PROTOTYPES ============================ */
static int thread_init(thpool_ *thpool_p, thread **thread_p, int id);
static void *thread_do(thread *thread_p);
static void thread_destroy(thread *thread_p);
static int jobqueue_init(thpool_ *thpool_p, jobqueue *jobqueue_p);
static void jobqueue_clear(thpool_ *thpool_p, jobqueue *jobqueue_p);
static void jobqueue_push(jobqueue *jobqueue_p, job *newjob_p);
static job *jobqueue_pull(jobqueue *jobqueue_p);
static void jobqueue_destroy(thpool_ *thpool_p, jobqueue *jobqueue_p);
static void bsem_init(bsem *bsem_p, int value);
static void bsem_reset(bsem *bsem_p);
static void bsem_post(bsem *bsem_p);
static void bsem_post_all(bsem *bsem_p);
static void bsem_wait(bsem *bsem_p);
/* ================================= API ==================================== */
/*
thpool_init
Initializes a threadpool. This function will not return untill all
threads have initialized successfully.
Parameters:
num_threads - The number of threads to be created in the threadpool.
Return Value:
Created threadpool on success, NULL on error.
*/
Threadpool thpool_init(int num_threads);
/*
thpool_add_work
Takes an action and its argument and adds it to the threadpool's job queue.
If you want to add to work a function with more than one arguments then
a way to implement this is by passing a pointer to a structure.
NOTICE: You have to cast both the function and argument
to not get warnings.
Parameters:
threadpool - threadpool to which the work will be added
function_p - The pointer point to the function to be added as work.
arg_p - The pointer point to the argument use for function_p.
priority - This priority nice of this work.
@example
void print_num(int num){
printf("%d\n", num);
}
int main() {
..
int a = 10;
thpool_add_work(thpool, (void*)print_num, (void*)a);
..
}
Return_Value:
0 on successs, -1 otherwise.
*/
int thpool_add_work(Threadpool threadpool, void (*function_p)(void *),
void *arg_p, int priority);
/*
thpool_destroy
This will wait for the currently active threads to finish and then 'kill'
the whole threadpool to free up memory.
Parameters:
thpool - The threadpool to be destroyed.
@example
int main() {
threadpool thpool1 = thpool_init(2);
threadpool thpool2 = thpool_init(2);
..
thpool_destroy(thpool1);
..
return 0;
}
Return_value:
None
*/
void thpool_destroy(thpool_ *thpool_p);
/*
thpool_num_threads_working
Working threads are the threads that are performing work (not idle).
Parameters:
thpool - The threadpool that we want to know the number of working threads.
@example
int main() {
threadpool thpool1 = thpool_init(2);
threadpool thpool2 = thpool_init(2);
..
printf("Working threads: %d\n", thpool_num_threads_working(thpool1));
..
return 0;
}
Return_Value:
The number of threads is working currently.
*/
int thpool_num_threads_working(thpool_ *thpool_p);
#endif