-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathblocking_queue.h
556 lines (473 loc) · 18.4 KB
/
blocking_queue.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#ifndef C_FEK_BLOCKING_QUEUE
#define C_FEK_BLOCKING_QUEUE
/*
Author: Felipe Einsfeld Kersting
MIT License
Copyright (c) 2020 Felipe Kersting
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
To use this blocking queue, define C_FEK_BLOCKING_QUEUE_IMPLEMENTATION before including blocking_queue.h in one of your source files.
To use this blocking queue, you must link your binary with pthread.
Note that fair_lock.h is a pre-requisite for this implementation, so you also need to include fair_lock.h in one of your source files
and define C_FEK_FAIR_LOCK_IMPLEMENTATION before including it.
This blocking queue is thread-safe.
This blocking queue has the following properties/features:
- It has a fixed capacity, provided by the caller.
- It allows the caller to add elements to the queue via both a blocking call and a non-blocking call.
- It allows the caller to get elements from the queue via both a blocking call and a non-blocking call.
- If multiple callers are blocked adding/getting an element to/from the queue, they are served in FIFO order.
The last point avoids the problem of starvation.
Note that since the queue is designed to serve callers in FIFO order, this might have a significant impact in performance.
If FIFO is not needed in your implementation, consider using a queue with no such guarantee.
Define C_FEK_BLOCKING_QUEUE_NO_CRT if you don't want the C Runtime Library included. If this is defined, you must provide
implementations for the following functions:
void* malloc(unsigned int size)
void free(void* block)
void* memcpy (void* dest, const void* src, unsigned int n)
For more information about the API, check the comments in the function signatures.
An usage example:
#define C_FEK_BLOCKING_QUEUE_IMPLEMENTATION
#define C_FEK_FAIR_LOCK_IMPLEMENTATION
#include "blocking_queue.h"
#include <assert.h>
int main() {
Blocking_Queue bq;
int aux;
blocking_queue_init(&bq, 4);
// Example of non-blocking calls
blocking_queue_add(&bq, (void*)1);
blocking_queue_poll(&bq, &aux);
assert(aux == 1);
// Example of potentially blocking calls
blocking_queue_put(&bq, (void*)2);
blocking_queue_take(&bq, &aux);
assert(aux == 2);
blocking_queue_destroy(&bq);
return 0;
}
Another example - more similar to a real use-case - since this time the integer is allocated:
#define C_FEK_BLOCKING_QUEUE_IMPLEMENTATION
#define C_FEK_FAIR_LOCK_IMPLEMENTATION
#include "blocking_queue.h"
#include <assert.h>
int main() {
Blocking_Queue bq;
int* aux = malloc(sizeof(int));
int* aux2;
blocking_queue_init(&bq, 4);
// Example of non-blocking calls
*aux = 1;
blocking_queue_add(&bq, aux);
blocking_queue_poll(&bq, &aux2);
assert(*aux2 == 1);
// Example of potentially blocking calls
*aux = 2;
blocking_queue_put(&bq, aux);
blocking_queue_take(&bq, &aux2);
assert(*aux2 == 2);
blocking_queue_destroy(&bq);
free(aux);
return 0;
}
For complete examples, check the tests in the repository https://github.com/felipeek/c-fifo-blocking-queue
*/
#include "fair_lock.h"
#define BQ_ERROR 1
#define BQ_FULL 2
#define BQ_EMPTY 3
#define BQ_CLOSED 4
// This structure is reserved for internal-use only
typedef struct {
// Fair lock used for get operations
Fair_Lock get_lock;
// Fair lock used for add operations
Fair_Lock add_lock;
// Stores whether weak locks are blocked for the 'get_lock'. Used as an optimization
int get_lock_are_weak_locks_blocked;
// Stores whether weak locks are blocked for the 'add_lock'. Used as an optimization
int add_lock_are_weak_locks_blocked;
// Main mutex, synchronizes get/add operations.
pthread_mutex_t mutex;
// Cond used to wake up blocked callers
pthread_cond_t cond;
// The queue of elements. It has a fixed size and is allocated in the init call.
// This is a circular queue. The front and the rear of the queue are given by queue_front and queue_rear
void** queue;
// The capacity of the queue
unsigned int queue_capacity;
// Number of elements currently in the queue.
unsigned int queue_size;
// The front of the queue
unsigned int queue_front;
// The rear of the queue
unsigned int queue_rear;
// If true, the queue does not have a maximum capacity
int is_boundless;
// Number of active callers. Used mainly to synchronize the destroy process.
int active_callers_count;
// Indicates whether the queue was closed.
int closed;
// Mutex to change 'active_callers_count'
pthread_mutex_t active_callers_mutex;
// Cond to help synchronizing the destroy process
pthread_cond_t destroy_cond;
// Auxiliar mutex to make the 'close' call thread-safe
pthread_mutex_t close_mutex;
} Blocking_Queue;
// Init the blocking queue.
// The blocking queue capacity is given by 'capacity'.
// When capacity > 0, a normal FIFO blocking queue with fixed size is used.
// When capacity <= 0, the queue will not have a maximum cap. It will, instead, grow without bounds (this is a special case)
// Note that, in the special case, the queue will serve as a normal boundless thread-safe queue (no blocking will ever occur when adding elements)
// Returns 0 if success, -1 if error.
int blocking_queue_init(Blocking_Queue* bq, unsigned int capacity);
// Adds an element to the blocking queue
// The element is given by 'element'
// This function does NOT block the caller.
// If the queue is full, this function will not add the new element. Instead, it will return BQ_FULL.
// FIFO order is guaranteed - blocked callers will be served in FIFO order. There is no starvation.
// Returns:
// * 0 if success
// * BQ_ERROR if an error happened
// * BQ_FULL if the there is no space in the blocking queue
// * BQ_CLOSED if the blocking queue was closed while the call was blocked
int blocking_queue_add(Blocking_Queue* bq, void* element);
// Puts an element to the blocking queue
// The element is given by 'element'
// This function may block the caller.
// If the queue is full, the caller is blocked until there is space in the queue for the new element.
// FIFO order is guaranteed - blocked callers will be served in FIFO order. There is no starvation.
// Returns:
// * 0 if success
// * BQ_ERROR if an error happened
// * BQ_CLOSED if the blocking queue was closed while the call was blocked
int blocking_queue_put(Blocking_Queue* bq, void* element);
// Poll an element from the blocking queue
// The element is stored in '*element'
// This function does NOT block the caller.
// If the queue is empty, this function will not poll any element. Instead, it will return BQ_EMPTY.
// FIFO order is guaranteed - blocked callers will be served in FIFO order. There is no starvation.
// Returns:
// * 0 if success
// * BQ_ERROR if an error happened
// * BQ_EMPTY if the blocking queue is empty
// * BQ_CLOSED if the blocking queue was closed while the call was blocked
int blocking_queue_poll(Blocking_Queue* bq, void* element);
// Take an element from the blocking queue
// The element is stored in '*element'
// This function may block the caller.
// If the queue is empty, the caller is blocked until there is an element available to take.
// FIFO order is guaranteed - blocked callers will be served in FIFO order. There is no starvation.
// Returns:
// * 0 if success
// * BQ_ERROR if an error happened
// * BQ_CLOSED if the blocking queue was closed while the call was blocked
int blocking_queue_take(Blocking_Queue* bq, void* element);
// Closes the blocking queue.
// When a blocking queue is closed, all _add/_put/_poll/_take calls will immediately return BQ_CLOSED if called.
// If there are active callers blocked in one of these calls, they will also be immediately unblocked and receive BQ_CLOSED.
// After the blocking queue is closed, it cannot be reopened again. The only thing remaining to do is calling 'blocking_queue_destroy'
// to free up the resources.
// This call is an auxiliar call that was introduced just to help solving synchronization issues. It helps the caller to close the queue, while
// keeping its reference valid. If 'blocking_queue_destroy' is called directly, it would first close the queue just like this function does,
// but after all callers return it would immediately free up the resources. This function allows the caller to split the closing of the queue
// and the freeing up of the resources into two calls, so the blocking queue reference is still valid after the queue is closed.
void blocking_queue_close(Blocking_Queue* bq);
// Destroys the blocking queue.
// If the queue is not closed (see 'blocking_queue_close'), it will first close the queue. Closing the queue will make all
// _add/_put_/_poll/_take calls to return immediately with BQ_CLOSED status. For more information, check 'blocking_queue_close'.
// After closing the queue and waiting for all active callers to return, the resources are freed.
// After this function is called, the blocking queue **cannot** be used anymore.
// Using it will cause undefined behavior and may crash the program.
// NOTE: This function can only be called a single time for a given blocking queue. Calling it multiple times will cause undefined behavior.
void blocking_queue_destroy(Blocking_Queue* bq);
#ifdef C_FEK_BLOCKING_QUEUE_IMPLEMENTATION
#if !defined(C_FEK_BLOCKING_QUEUE_NO_CRT)
#include <stdlib.h>
#include <memory.h>
#endif
int blocking_queue_init(Blocking_Queue* bq, unsigned int capacity)
{
if (pthread_mutex_init(&bq->mutex, NULL)) {
return -1;
}
if (pthread_mutex_init(&bq->active_callers_mutex, NULL)) {
pthread_mutex_destroy(&bq->mutex);
return -1;
}
if (pthread_mutex_init(&bq->close_mutex, NULL)) {
pthread_mutex_destroy(&bq->mutex);
pthread_mutex_destroy(&bq->active_callers_mutex);
return -1;
}
if (pthread_cond_init(&bq->cond, NULL)) {
pthread_mutex_destroy(&bq->mutex);
pthread_mutex_destroy(&bq->active_callers_mutex);
pthread_mutex_destroy(&bq->close_mutex);
return -1;
}
if (pthread_cond_init(&bq->destroy_cond, NULL)) {
pthread_mutex_destroy(&bq->mutex);
pthread_mutex_destroy(&bq->active_callers_mutex);
pthread_mutex_destroy(&bq->close_mutex);
pthread_cond_destroy(&bq->cond);
return -1;
}
if (fair_lock_init(&bq->get_lock)) {
pthread_mutex_destroy(&bq->mutex);
pthread_mutex_destroy(&bq->active_callers_mutex);
pthread_mutex_destroy(&bq->close_mutex);
pthread_cond_destroy(&bq->cond);
pthread_cond_destroy(&bq->destroy_cond);
return -1;
}
if (fair_lock_init(&bq->add_lock)) {
pthread_mutex_destroy(&bq->mutex);
pthread_mutex_destroy(&bq->active_callers_mutex);
pthread_mutex_destroy(&bq->close_mutex);
pthread_cond_destroy(&bq->cond);
pthread_cond_destroy(&bq->destroy_cond);
fair_lock_destroy(&bq->get_lock);
return -1;
}
if (capacity <= 0) {
bq->queue_capacity = 1;
bq->is_boundless = 1;
} else {
bq->queue_capacity = capacity;
bq->is_boundless = 0;
}
bq->queue_size = 0;
bq->queue_front = 0;
bq->queue_rear = bq->queue_capacity - 1;
bq->closed = 0;
bq->active_callers_count = 0;
bq->get_lock_are_weak_locks_blocked = 0;
bq->add_lock_are_weak_locks_blocked = 0;
bq->queue = (void**)malloc(bq->queue_capacity * sizeof(void*));
if (bq->queue == NULL)
{
pthread_mutex_destroy(&bq->mutex);
pthread_mutex_destroy(&bq->active_callers_mutex);
pthread_mutex_destroy(&bq->close_mutex);
pthread_cond_destroy(&bq->cond);
pthread_cond_destroy(&bq->destroy_cond);
fair_lock_destroy(&bq->get_lock);
fair_lock_destroy(&bq->add_lock);
return -1;
}
return 0;
}
void blocking_queue_close(Blocking_Queue* bq) {
pthread_mutex_lock(&bq->close_mutex);
pthread_mutex_lock(&bq->mutex);
if (bq->closed) {
pthread_mutex_unlock(&bq->mutex);
pthread_mutex_unlock(&bq->close_mutex);
return;
}
bq->closed = 1;
pthread_mutex_unlock(&bq->mutex);
pthread_mutex_lock(&bq->active_callers_mutex);
while (bq->active_callers_count) {
pthread_cond_signal(&bq->cond);
pthread_cond_wait(&bq->destroy_cond, &bq->active_callers_mutex);
}
pthread_mutex_unlock(&bq->active_callers_mutex);
pthread_mutex_unlock(&bq->close_mutex);
}
void blocking_queue_destroy(Blocking_Queue* bq) {
blocking_queue_close(bq);
free(bq->queue);
fair_lock_destroy(&bq->get_lock);
fair_lock_destroy(&bq->add_lock);
pthread_mutex_destroy(&bq->mutex);
pthread_mutex_destroy(&bq->close_mutex);
}
static void enqueue(Blocking_Queue *bq, void *element) {
//assert(bq->queue_size < bq->queue_capacity);
bq->queue_rear = (bq->queue_rear + 1) % bq->queue_capacity;
bq->queue[bq->queue_rear] = element;
bq->queue_size = bq->queue_size + 1;
}
static void *dequeue(Blocking_Queue *bq) {
//assert(bq->queue_size > 0);
void* element = bq->queue[bq->queue_front];
bq->queue_front = (bq->queue_front + 1) % bq->queue_capacity;
bq->queue_size = bq->queue_size - 1;
return element;
}
static void increase_active_callers_count(Blocking_Queue* bq) {
pthread_mutex_lock(&bq->active_callers_mutex);
++bq->active_callers_count;
pthread_mutex_unlock(&bq->active_callers_mutex);
}
static void decrease_active_callers_count(Blocking_Queue* bq) {
pthread_mutex_lock(&bq->active_callers_mutex);
--bq->active_callers_count;
pthread_cond_signal(&bq->destroy_cond);
pthread_mutex_unlock(&bq->active_callers_mutex);
}
static int grow_queue(Blocking_Queue* bq) {
unsigned int new_capacity = bq->queue_capacity * 2u;
void** new_queue = (void**)malloc(new_capacity * sizeof(void*));
if (new_queue == NULL) {
return 1;
}
if (bq->queue_rear >= bq->queue_front) {
memcpy(new_queue, bq->queue + bq->queue_front, bq->queue_size * sizeof(void*));
} else if (bq->queue_front > bq->queue_rear) {
memcpy(new_queue, bq->queue + bq->queue_front, (bq->queue_capacity - bq->queue_front) * sizeof(void*));
memcpy(new_queue + (bq->queue_capacity - bq->queue_front), bq->queue, (bq->queue_rear + 1) * sizeof(void*));
}
free(bq->queue);
bq->queue = new_queue;
bq->queue_capacity = new_capacity;
bq->queue_front = 0;
bq->queue_rear = bq->queue_size - 1;
return 0;
}
int blocking_queue_add_internal(Blocking_Queue* bq, void* element, int async) {
increase_active_callers_count(bq);
int lock_ret;
if (async) {
lock_ret = fair_lock_lock_weak(&bq->add_lock);
} else {
lock_ret = fair_lock_lock(&bq->add_lock);
}
//assert(lock_ret == 0 || lock_ret == FL_ERROR || lock_ret == FL_ABANDONED);
if (lock_ret == FL_ERROR) {
decrease_active_callers_count(bq);
return BQ_ERROR;
} else if (lock_ret == FL_ABANDONED) {
decrease_active_callers_count(bq);
return BQ_FULL;
}
pthread_mutex_lock(&bq->mutex);
if (bq->closed) {
fair_lock_unlock(&bq->add_lock);
pthread_mutex_unlock(&bq->mutex);
decrease_active_callers_count(bq);
return BQ_CLOSED;
}
if (bq->queue_size == bq->queue_capacity) {
if (bq->is_boundless) {
if (grow_queue(bq)) {
fair_lock_unlock(&bq->add_lock);
pthread_mutex_unlock(&bq->mutex);
decrease_active_callers_count(bq);
return BQ_ERROR;
}
} else {
if (!bq->add_lock_are_weak_locks_blocked) {
fair_lock_block_weak_locks(&bq->add_lock);
bq->add_lock_are_weak_locks_blocked = 1;
}
if (async) {
fair_lock_unlock(&bq->add_lock);
pthread_mutex_unlock(&bq->mutex);
decrease_active_callers_count(bq);
return BQ_FULL;
}
pthread_cond_wait(&bq->cond, &bq->mutex);
if (bq->closed) {
fair_lock_unlock(&bq->add_lock);
pthread_mutex_unlock(&bq->mutex);
decrease_active_callers_count(bq);
return BQ_CLOSED;
}
//assert(bq->queue_size < bq->queue_capacity);
}
}
if (bq->get_lock_are_weak_locks_blocked) {
fair_lock_allow_weak_locks(&bq->get_lock);
bq->get_lock_are_weak_locks_blocked = 0;
}
pthread_cond_signal(&bq->cond);
enqueue(bq, element);
pthread_mutex_unlock(&bq->mutex);
fair_lock_unlock(&bq->add_lock);
decrease_active_callers_count(bq);
return 0;
}
int blocking_queue_get_internal(Blocking_Queue* bq, int async, void* element) {
increase_active_callers_count(bq);
int lock_ret;
if (async) {
lock_ret = fair_lock_lock_weak(&bq->get_lock);
} else {
lock_ret = fair_lock_lock(&bq->get_lock);
}
if (lock_ret == FL_ERROR) {
decrease_active_callers_count(bq);
return BQ_ERROR;
} else if (lock_ret == FL_ABANDONED) {
decrease_active_callers_count(bq);
return BQ_EMPTY;
}
//assert(lock_ret == 0 || lock_ret == FL_ERROR || lock_ret == FL_ABANDONED);
pthread_mutex_lock(&bq->mutex);
if (bq->closed) {
fair_lock_unlock(&bq->get_lock);
pthread_mutex_unlock(&bq->mutex);
decrease_active_callers_count(bq);
return BQ_CLOSED;
}
if (bq->queue_size == 0) {
if (!bq->get_lock_are_weak_locks_blocked) {
fair_lock_block_weak_locks(&bq->get_lock);
bq->get_lock_are_weak_locks_blocked = 1;
}
if (async) {
fair_lock_unlock(&bq->get_lock);
pthread_mutex_unlock(&bq->mutex);
decrease_active_callers_count(bq);
return BQ_EMPTY;
}
pthread_cond_wait(&bq->cond, &bq->mutex);
if (bq->closed) {
fair_lock_unlock(&bq->get_lock);
pthread_mutex_unlock(&bq->mutex);
decrease_active_callers_count(bq);
return BQ_CLOSED;
}
//assert(bq->queue_size >= 1);
}
if (bq->add_lock_are_weak_locks_blocked) {
fair_lock_allow_weak_locks(&bq->add_lock);
bq->add_lock_are_weak_locks_blocked = 0;
}
pthread_cond_signal(&bq->cond);
*(void**)element = dequeue(bq);
pthread_mutex_unlock(&bq->mutex);
fair_lock_unlock(&bq->get_lock);
decrease_active_callers_count(bq);
return 0;
}
int blocking_queue_add(Blocking_Queue* bq, void* element) {
return blocking_queue_add_internal(bq, element, 1);
}
int blocking_queue_put(Blocking_Queue* bq, void* element) {
return blocking_queue_add_internal(bq, element, 0);
}
int blocking_queue_poll(Blocking_Queue* bq, void* element) {
return blocking_queue_get_internal(bq, 1, element);
}
int blocking_queue_take(Blocking_Queue* bq, void* element) {
return blocking_queue_get_internal(bq, 0, element);
}
#endif
#endif