-
Notifications
You must be signed in to change notification settings - Fork 0
/
cqueue.c
555 lines (446 loc) · 12.3 KB
/
cqueue.c
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
/*
* cqueue.c
*
* A circular fifo queue.
*
* Created on: Nov 6, 2016
* Author: Kelly Wiles
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <stdatomic.h>
#include <errno.h>
#include "tiger_gl.h"
#include "myatomic.h"
#define MILLION 1000000L
#define BILLION 1000000000L
int _maxCQueues;
int _currCQCount;
CQueue **_cqueues = NULL;
pthread_mutex_t _cqueueLock = PTHREAD_MUTEX_INITIALIZER;
int cqInit(int cqueueCnt) {
if (cqueueCnt == 1)
_maxCQueues = 2;
else if (cqueueCnt <= 0)
_maxCQueues = MAX_CQUEUES;
else
_maxCQueues = cqueueCnt;
_currCQCount = 0;
// printf("cqueueCnt: %d\n", cqueueCnt);
// Allocate pointer array to hold each CQueue created with cqCreate()
_cqueues = (CQueue **)calloc(_maxCQueues, sizeof(CQueue *));
if (pthread_mutex_init(&_cqueueLock, NULL) != 0) {
printf("Error: Mutex init lock failed.\n");
return -1;
}
return 0;
}
/*
* This function cqCreate creates a named Linked List Queue
*
* cqName = Queue name to create.
* arrSize = Size of number of messages
*
* NOTE: The max size of the queue will be able to handle is (arrSize - 1)
* This function does not allow for growth if queue is full.
*
* returns -1 on error
* else 0 or greater on success
*/
int cqCreate(const char *cqName, int arrSize) {
return cqCreateDynamic(cqName, arrSize, 0);
}
/*
* This function cqCreateDynamic creates a named Linked List Queue
* Also this function will grow queue if it is full.
* It will never shrink the queue.
*
* cqName = Queue name to create.
* arrSize = Size of number of messages
* growth = how much to grow queue if full, 0 means do not grow.
*
* NOTE: The max size of the queue will be able to handle is (arrSize - 1)
*
* returns -1 on error
* else 0 or greater on success
*/
int cqCreateDynamic(const char *cqName, int arrSize, int growth) {
if (_cqueues == NULL) {
printf("Error: Must call cqInit() first.\n");
return -1;
}
if (_currCQCount >= _maxCQueues) {
printf("Error: No more CQ structures available\n");
return -1;
}
_currCQCount++;
pthread_mutex_lock(&_cqueueLock);
// Look for empty slot or an exiting entry.
CQueue *qp;
int empty = -1;
for (int i = 0; i < _maxCQueues; i++) {
qp = _cqueues[i];
if (qp == NULL) {
empty = i;
continue;
}
// already been created ?
if (strcmp(qp->cqName, cqName) == 0) {
pthread_mutex_unlock(&_cqueueLock);
return i;
}
}
if (empty == -1) {
pthread_mutex_unlock(&_cqueueLock);
printf("Error: No free slots.\n");
return -1;
}
int size = arrSize * sizeof(ItemType);
// printf("Info: size: %d, %d\n", size, (sizeof(CQueue) + size));
CQueue *cp = (CQueue *)calloc(1, (sizeof(CQueue) + size));
if (cp == NULL) {
printf("Error: Unable to allocate memory for CQueue structure\n");
pthread_mutex_unlock(&_cqueueLock);
return -1;
}
_cqueues[empty] = cp;
// printf("Info: %p, %p\n", _cqueues[empty], cp);
pthread_mutex_init(&cp->cqLock, NULL);
pthread_cond_init(&cp->cqCond, NULL);
// cp->cqCond = PTHREAD_COND_INITIALIZER;
// cp->cqLock = PTHREAD_MUTEX_INITIALIZER;
if (pthread_mutex_init(&cp->cqLock, NULL) != 0) {
printf("Error: Mutex init lock failed.\n");
pthread_mutex_unlock(&_cqueueLock);
return -1;
}
if (pthread_cond_init(&cp->cqCond, NULL) != 0) {
printf("Error: Mutex condition init failed.\n");
pthread_mutex_unlock(&_cqueueLock);
return -1;
}
cp->cqItemCount = 0;
cp->queIn = 0;
cp->queOut = arrSize -1;
cp->arrSize = arrSize;
cp->growth = growth;
strcpy(cp->cqName, cqName);
pthread_mutex_unlock(&_cqueueLock);
return empty;
}
/*
* If the queue is used to access an array of memory
* then this will allow the array to be reallocated
* if the queue ever grows in size.
*
* cqNum = Number returned by the cqCreate() or cqQueNum() functions
* buffer = pointer to memory array.
* blkSize = size of an array element.
*
*/
int cqSetBuffer(int cqNum, void *buffer, int blkSize) {
if (_cqueues == NULL) {
printf("Error: Must call cqInit() first.\n");
return -1;
}
CQueue *cp = _cqueues[cqNum];
cp->buffer = buffer;
cp->blkSize = blkSize;
return 0;
}
/*
* Used to change the number of elements to grow by.
*
* cqNum = Number returned by the cqCreate() or cqQueNum() functions
* growth = number of elements to grow queue by.
*
* Can set the growth to zero to stop any future growth.
*/
int cqSetGrowth(int cqNum, int growth) {
if (_cqueues == NULL) {
printf("Error: Must call cqInit() first.\n");
return -1;
}
CQueue *cp = _cqueues[cqNum];
if (growth < 0)
cp->growth = 0;
else
cp->growth = growth;
return 0;
}
/*
* Grows the queue and the buffer associated with it if any..
*
* cqNum = Number returned by the cqCreate() or cqQueNum() functions
* growth = number of elements to grow queue by.
*/
int cqGrow(int cqNum, int growth) {
if (_cqueues == NULL) {
printf("Error: Must call cqInit() first.\n");
return -1;
}
if (growth <= 0)
return -1;
CQueue *cp = _cqueues[cqNum];
pthread_mutex_lock(&cp->cqLock);
int newSize = (cp->arrSize + growth) * sizeof(ItemType);
CQueue *p = (CQueue *)realloc(cp, (sizeof(CQueue) + newSize));
if (p != NULL) {
printf("Info: Grow queue by %d elements.\n", growth);
// Success in reallocating memory.
_cqueues[cqNum] = p;
for (int k = cp->arrSize; k < (cp->arrSize + growth); k++) {
cp->array[k].l = 0; // initialize what we added.
}
cp->arrSize += growth;
cp->growth = growth;
cp->queOut = cp->arrSize - 1;
if (cp->buffer != NULL && cp->blkSize > 0) {
void *p2 = (void *)realloc(*cp->buffer, (cp->arrSize * cp->blkSize));
if (p2 != NULL) {
cp->buffer = &p2;
}
}
}
pthread_mutex_unlock(&cp->cqLock);
return 0;
}
/*
* This function cqAdd adds a value to the circular list.
*
* cqNum = Number returned by the cqCreate() or cqQueNum() functions
* value = The value to place into the circular list.
*
* return 0 on success
* -3 failed to reallocate memory, queue still full
* -2 on queue full
* -1 on error;
*/
int cqAdd(int cqNum, ItemType *value) {
if (_cqueues == NULL) {
printf("Error: Must call cqInit() first.\n");
return -1;
}
CQueue *cp = _cqueues[cqNum];
pthread_mutex_lock(&cp->cqLock);
if (cp->queIn == cp->queOut) {
// queue is full.
if (cp->growth > 0) {
// Grow Queue.
int newSize = (cp->arrSize + cp->growth) * sizeof(ItemType);
CQueue *p = (CQueue *)realloc(cp, (sizeof(CQueue) + newSize));
if (p != NULL) {
printf("Info: Grow queue by %d elements.\n", cp->growth);
// Success in reallocating memory.
_cqueues[cqNum] = p;
for (int k = cp->arrSize; k < (cp->arrSize + cp->growth); k++) {
cp->array[k].l = 0; // initialize what we added.
}
cp->arrSize += cp->growth;
cp->queOut = cp->arrSize - 1;
if (cp->buffer != NULL && cp->blkSize > 0) {
void *p2 = (void *)realloc(*cp->buffer, (cp->arrSize * cp->blkSize));
if (p2 != NULL) {
cp->buffer = &p2;
}
}
} else {
// failed to reallocate memory.
printf("Error: Failed to reallocate memory.\n");
pthread_cond_broadcast(&cp->cqCond);
pthread_mutex_unlock(&cp->cqLock);
return -3;
}
} else {
pthread_cond_broadcast(&cp->cqCond);
pthread_mutex_unlock(&cp->cqLock);
return -2;
}
}
*(ItemType *)&cp->array[cp->queIn] = *value;
// memcpy(&cp->array[cp->queIn], value, sizeof(ItemType));
// move queIn to next slot.
cp->queIn = (cp->queIn + 1) % cp->arrSize;
cp->cqItemCount++;
// printf("printf: cqAdd %s %d\n", cqGetName(cqNum), cp->cqItemCount);
pthread_cond_broadcast(&cp->cqCond);
pthread_mutex_unlock(&cp->cqLock);
return 0;
}
/*
* This function cqRemove returns the first element in the queue.
*
* cqNum = Queue index
* value = Place value from queue here.
* block = if true then block waiting on an item else return null if queue empty
* ms = Timeout in milliseconds, if CQ_TIMERBLOCK is used.
*
* returns -1 on error or empty queue
* else 0
*/
int cqRemove(int cqNum, ItemType *value, int block) {
int ret = -1;
if (_cqueues == NULL) {
printf("Error: Must call cqInit() first.\n");
return ret;
}
CQueue *cp = _cqueues[cqNum];
pthread_mutex_lock(&cp->cqLock);
if (((cp->queOut + 1) % cp->arrSize) == cp->queIn) {
// queue is empty.
if (cp->cqItemCount <= 0 && block == CQ_NONBLOCK) {
pthread_mutex_unlock(&cp->cqLock);
// printf("Error: Queue empty and non blocking is turned on.\n");
return -1;
}
// block waiting on data to arrive.
// printf("Info: Waiting on queue %s %d\n", cqGetName(cqNum), _cqueues[cqNum]->cqItemCount);
while (cp->cqItemCount == 0) {
pthread_cond_wait(&cp->cqCond, &cp->cqLock);
}
// printf("Awake from queue. %s %d\n", cqGetName(cqNum), cp->cqItemCount);
}
cp->queOut = (cp->queOut + 1) % cp->arrSize;
*value = *(ItemType *)&cp->array[cp->queOut];
// memcpy(value, &cp->array[cp->queOut], sizeof(ItemType));
int exp = -1;
int d = 0;
cp->cqItemCount--;
if (AtomicExchange(&cp->cqItemCount, &exp, &d) == 1) {
printf("Info: Warning: cp->cqItemCount was -1.\n");
}
pthread_mutex_unlock(&cp->cqLock);
return 0;
}
/*
* This function cqRemoveTimed returns the first element in the queue.
*
* cqNum = Queue index
* value = Place value from queue here.
* timeout = Block N milliseconds waiting on an item.
*
* returns -1 on error
* -2 timed out
* else 0
*/
int cqRemoveTimed(int cqNum, ItemType *value, int timeout) {
int ret = -1;
int rc;
unsigned long t;
struct timeval tv; // microseconds 1 millionth of a second
struct timespec ts; // nanoseconds 1 billionth of a second
t = timeout * MILLION; // convert milliseconds to nanoseconds.
if (_cqueues == NULL) {
printf("Error: Must call cqInit() first.\n");
return ret;
}
CQueue *cp = _cqueues[cqNum];
pthread_mutex_lock(&cp->cqLock);
if (((cp->queOut + 1) % cp->arrSize) == cp->queIn) {
// queue is empty.
gettimeofday(&tv, NULL);
// Convert from timeval to timespec
ts.tv_sec = tv.tv_sec;
ts.tv_nsec = tv.tv_usec * 1000;
// add timeout (t) into timespec
ts.tv_sec += (t / BILLION);
ts.tv_nsec += (t % BILLION);
// block waiting on data to arrive or time out.
// printf("Waiting on queue %s %d\n", cqGetName(cqNum), _cqueues[cqNum]->cqItemCount);
while (cp->cqItemCount == 0) {
rc = pthread_cond_timedwait(&cp->cqCond, &cp->cqLock, &ts);
if (rc == ETIMEDOUT) {
pthread_mutex_unlock(&cp->cqLock);
return -2;
}
}
// printf("Awake from queue. %s %d\n", cqGetName(cqNum), cp->cqItemCount);
}
cp->queOut = (cp->queOut + 1) % cp->arrSize;
*value = *(ItemType *)&cp->array[cp->queOut];
int exp = -1;
int d = 0;
cp->cqItemCount--;
if (AtomicExchange(&cp->cqItemCount, &exp, &d) == 1) {
printf("Info: Warning: cp->cqItemCount was -1.\n");
}
pthread_mutex_unlock(&cp->cqLock);
return 0;
}
int cqDestroy(int queNum) {
if (_cqueues == NULL) {
printf("Error: Must call cqInit() first.\n");
return -1;
}
// CQueue *cp = _cqueues[queNum];
pthread_mutex_lock(&_cqueueLock);
free(_cqueues[queNum]);
_cqueues[queNum] = NULL;
pthread_mutex_unlock(&_cqueueLock);
return 0;
}
/*
* This function cqCount return the number of item in the named queue.
*
* cqName = CQueue name
*
* returns number items in queue,
* -1 on error.
*/
int cqCount(int queNum) {
// int count = 0;
if (_cqueues == NULL) {
printf("Error: Must call cqInit() first.\n");
return -1;
}
// CQueue *cp = _cqueues[queNum];
// pthread_mutex_lock(&cp->cqLock);
// count = cp->cqItemCount;
// pthread_mutex_unlock(&cp->cqLock);
return _cqueues[queNum]->cqItemCount;
}
int cqArrSize(int queNum) {
if (_cqueues == NULL) {
printf("Error: Must call cqInit() first.\n");
return -1;
}
CQueue *cp = _cqueues[queNum];
return cp->arrSize;
}
char *cqGetName(int queNum) {
char *name = NULL;
if (_cqueues == NULL) {
printf("Error: Must call cqInit() first.\n");
return 0;
}
CQueue *cp = _cqueues[queNum];
if (cp == NULL)
return NULL;
name = cp->cqName;
return name;
}
/*
* This function cqQueNum returns the index number for the given name.
*
* cqName = Name to find index of.
*
* returns queue number or
* -1 on error
*/
int cqGetNum(char *cqName) {
CQueue *cp;
for (int i = 0; i < _maxCQueues; i++) {
cp = _cqueues[i];
if (cp == NULL)
continue;
if (strcmp(cp->cqName, cqName) == 0)
return i;
}
return -1;
}