forked from aerospike/act
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
316 lines (254 loc) · 6.94 KB
/
queue.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
/*
* queue.c
*
* Copyright (c) 2008-2014 Aerospike, Inc. All rights reserved.
*
* 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.
*/
//==========================================================
// Includes
//
#include "queue.h"
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
//==========================================================
// Constants & Macros
//
#define CF_QUEUE_ALLOCSZ (64 * 1024)
#define CF_Q_SZ(__q) (__q->write_offset - __q->read_offset)
#define CF_Q_EMPTY(__q) (__q->write_offset == __q->read_offset)
#define CF_Q_ELEM_PTR(__q, __i) (&q->queue[(__i % __q->allocsz) * q->elementsz])
//==========================================================
// Forward Declarations
//
int cf_queue_resize(cf_queue *q, uint new_sz);
void cf_queue_unwrap(cf_queue *q);
//==========================================================
// Public API
//
//------------------------------------------------
// Create a queue.
//
cf_queue*
cf_queue_create(size_t elementsz, bool threadsafe)
{
cf_queue *q = NULL;
q = malloc( sizeof(cf_queue));
if (! q) {
fprintf(stdout, "ERROR: creating queue (malloc)\n");
return NULL;
}
q->queue = malloc(CF_QUEUE_ALLOCSZ * elementsz);
if (! q->queue) {
fprintf(stdout, "ERROR: creating queue (malloc)\n");
free(q);
return NULL;
}
q->allocsz = CF_QUEUE_ALLOCSZ;
q->write_offset = q->read_offset = 0;
q->elementsz = elementsz;
q->threadsafe = threadsafe;
if (! q->threadsafe) {
return q;
}
if (0 != pthread_mutex_init(&q->LOCK, NULL)) {
fprintf(stdout, "ERROR: creating queue (mutex init)\n");
free(q->queue);
free(q);
return NULL;
}
if (0 != pthread_cond_init(&q->CV, NULL)) {
fprintf(stdout, "ERROR: creating queue (cond init)\n");
pthread_mutex_destroy(&q->LOCK);
free(q->queue);
free(q);
return NULL;
}
return q;
}
//------------------------------------------------
// Destroy a queue.
//
void
cf_queue_destroy(cf_queue *q)
{
if (q->threadsafe) {
pthread_cond_destroy(&q->CV);
pthread_mutex_destroy(&q->LOCK);
}
free(q->queue);
free(q);
}
//------------------------------------------------
// Get the number of elements in the queue.
//
int
cf_queue_sz(cf_queue *q)
{
int rv;
if (q->threadsafe) {
pthread_mutex_lock(&q->LOCK);
}
rv = CF_Q_SZ(q);
if (q->threadsafe) {
pthread_mutex_unlock(&q->LOCK);
}
return rv;
}
//------------------------------------------------
// Push an element to the tail of the queue.
//
int
cf_queue_push(cf_queue *q, void *ptr)
{
if (q->threadsafe) {
pthread_mutex_lock(&q->LOCK);
}
if (CF_Q_SZ(q) == q->allocsz) {
if (CF_QUEUE_OK != cf_queue_resize(q, q->allocsz * 2)) {
if (q->threadsafe) {
pthread_mutex_unlock(&q->LOCK);
}
return CF_QUEUE_ERR;
}
}
memcpy(CF_Q_ELEM_PTR(q, q->write_offset), ptr, q->elementsz);
q->write_offset++;
// We're at risk of overflowing the write offset if it's too big.
if (q->write_offset & 0xC0000000) {
cf_queue_unwrap(q);
}
if (q->threadsafe) {
pthread_cond_signal(&q->CV);
pthread_mutex_unlock(&q->LOCK);
}
return CF_QUEUE_OK;
}
//------------------------------------------------
// Pop an element from the head of the queue.
//
// ms_wait < 0 - wait forever
// ms_wait = 0 - don't wait at all
// ms_wait > 0 - wait that number of milliseconds
//
int
cf_queue_pop(cf_queue *q, void *buf, int ms_wait)
{
if (q->threadsafe) {
pthread_mutex_lock(&q->LOCK);
}
if (q->threadsafe) {
struct timespec tp;
if (ms_wait > 0) {
clock_gettime(CLOCK_REALTIME, &tp);
tp.tv_sec += ms_wait / 1000;
tp.tv_nsec += (ms_wait % 1000) * 1000000;
if (tp.tv_nsec > 1000000000) {
tp.tv_nsec -= 1000000000;
tp.tv_sec++;
}
}
// Note that we apparently have to use a while loop. Careful reading of
// the pthread_cond_signal() documentation says that AT LEAST ONE
// waiting thread will be awakened...
while (CF_Q_EMPTY(q)) {
if (CF_QUEUE_FOREVER == ms_wait) {
pthread_cond_wait(&q->CV, &q->LOCK);
}
else if (CF_QUEUE_NOWAIT == ms_wait) {
pthread_mutex_unlock(&q->LOCK);
return CF_QUEUE_EMPTY;
}
else {
pthread_cond_timedwait(&q->CV, &q->LOCK, &tp);
if (CF_Q_EMPTY(q)) {
pthread_mutex_unlock(&q->LOCK);
return CF_QUEUE_EMPTY;
}
}
}
}
else if (CF_Q_EMPTY(q)) {
return CF_QUEUE_EMPTY;
}
memcpy(buf, CF_Q_ELEM_PTR(q, q->read_offset), q->elementsz);
q->read_offset++;
if (q->read_offset == q->write_offset) {
q->read_offset = q->write_offset = 0;
}
if (q->threadsafe) {
pthread_mutex_unlock(&q->LOCK);
}
return CF_QUEUE_OK;
}
//==========================================================
// Utilities
//
//------------------------------------------------
// Change allocated capacity - called under lock.
//
int
cf_queue_resize(cf_queue *q, uint new_sz)
{
// The rare case where the queue is not fragmented, and none of the offsets
// need to move.
if (0 == q->read_offset % q->allocsz) {
q->queue = realloc(q->queue, new_sz * q->elementsz);
if (! q->queue) {
fprintf(stdout, "ERROR: resizing queue (realloc)\n");
return CF_QUEUE_ERR;
}
q->read_offset = 0;
q->write_offset = q->allocsz;
}
else {
uint8_t *newq = malloc(new_sz * q->elementsz);
if (! newq) {
fprintf(stdout, "ERROR: resizing queue (malloc)\n");
return CF_QUEUE_ERR;
}
// endsz is used bytes in old queue from insert point to end.
uint32_t endsz =
(q->allocsz - (q->read_offset % q->allocsz)) * q->elementsz;
memcpy(&newq[0], CF_Q_ELEM_PTR(q, q->read_offset), endsz);
memcpy(&newq[endsz], &q->queue[0], (q->allocsz * q->elementsz) - endsz);
free(q->queue);
q->queue = newq;
q->write_offset = q->allocsz;
q->read_offset = 0;
}
q->allocsz = new_sz;
return CF_QUEUE_OK;
}
//------------------------------------------------
// Reset read & write offsets - called under lock.
//
void
cf_queue_unwrap(cf_queue *q)
{
int sz = CF_Q_SZ(q);
q->read_offset %= q->allocsz;
q->write_offset = q->read_offset + sz;
}