-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrpa_queue.c
439 lines (383 loc) · 11.1 KB
/
rpa_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
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
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "rpa_queue.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
// uncomment to print debug messages
//#define QUEUE_DEBUG
struct rpa_queue_t {
void **data;
volatile uint32_t nelts; /**< # elements */
uint32_t in; /**< next empty location */
uint32_t out; /**< next filled location */
uint32_t bounds;/**< max size of queue */
uint32_t full_waiters;
uint32_t empty_waiters;
pthread_mutex_t *one_big_mutex;
pthread_cond_t *not_empty;
pthread_cond_t *not_full;
int terminated;
};
#ifdef QUEUE_DEBUG
static void Q_DBG(const char*msg, rpa_queue_t *q) {
fprintf(stderr, "#%d in %d out %d\t%s\n",
q->nelts, q->in, q->out,
msg
);
}
#else
#define Q_DBG(x,y)
#endif
/**
* Detects when the rpa_queue_t is full. This utility function is expected
* to be called from within critical sections, and is not threadsafe.
*/
#define rpa_queue_full(queue) ((queue)->nelts == (queue)->bounds)
/**
* Detects when the rpa_queue_t is empty. This utility function is expected
* to be called from within critical sections, and is not threadsafe.
*/
#define rpa_queue_empty(queue) ((queue)->nelts == 0)
static void set_timeout(struct timespec * abstime, int wait_ms)
{
clock_gettime(CLOCK_REALTIME, abstime);
/* add seconds */
abstime->tv_sec += (wait_ms / 1000);
/* add and carry microseconds */
long ms = abstime->tv_nsec / 1000000L;
ms += wait_ms % 1000;
while (ms > 1000) {
ms -= 1000;
abstime->tv_sec += 1;
}
abstime->tv_nsec = ms * 1000000L;
}
/**
* Callback routine that is called to destroy this
* rpa_queue_t when its pool is destroyed.
*/
void rpa_queue_destroy(rpa_queue_t * queue)
{
/* Ignore errors here, we can't do anything about them anyway. */
pthread_cond_destroy(queue->not_empty);
pthread_cond_destroy(queue->not_full);
pthread_mutex_destroy(queue->one_big_mutex);
}
void rpa_queue_free(rpa_queue_t * queue)
{
if (queue->data) free(queue->data);
if (queue->not_empty) free(queue->not_empty);
if (queue->not_full) free(queue->not_full);
if (queue->one_big_mutex) free(queue->one_big_mutex);
free(queue);
}
/**
* Initialize the rpa_queue_t.
*/
bool rpa_queue_create(rpa_queue_t **q, uint32_t queue_capacity)
{
rpa_queue_t *queue;
queue = malloc(sizeof(rpa_queue_t));
if (!queue) {
return false;
}
*q = queue;
memset(queue, 0, sizeof(rpa_queue_t));
if (!(queue->one_big_mutex = malloc(sizeof(pthread_mutex_t)))) return false;
if (!(queue->not_empty = malloc(sizeof(pthread_cond_t)))) return false;
if (!(queue->not_full = malloc(sizeof(pthread_cond_t)))) return false;
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
int rv = pthread_mutex_init(queue->one_big_mutex, &attr);
if (rv != 0) {
Q_DBG("pthread_mutex_init failed", queue);
goto error;
}
rv = pthread_cond_init(queue->not_empty, NULL);
if (rv != 0) {
Q_DBG("pthread_cond_init not_empty failed", queue);
goto error;
}
rv = pthread_cond_init(queue->not_full, NULL);
if (rv != 0) {
Q_DBG("pthread_cond_init not_full failed", queue);
goto error;
}
/* Set all the data in the queue to NULL */
queue->data = malloc(queue_capacity * sizeof(void*));
queue->bounds = queue_capacity;
queue->nelts = 0;
queue->in = 0;
queue->out = 0;
queue->terminated = 0;
queue->full_waiters = 0;
queue->empty_waiters = 0;
return true;
error:
free(queue);
return false;
}
/**
* Push new data onto the queue. Blocks if the queue is full. Once
* the push operation has completed, it signals other threads waiting
* in rpa_queue_pop() that they may continue consuming sockets.
*/
bool rpa_queue_push(rpa_queue_t *queue, void *data)
{
return rpa_queue_timedpush(queue, data, RPA_WAIT_FOREVER);
}
bool rpa_queue_timedpush(rpa_queue_t *queue, void *data, int wait_ms)
{
bool rv;
if (wait_ms == RPA_WAIT_NONE) return rpa_queue_trypush(queue, data);
if (queue->terminated) {
return false; /* no more elements ever again */
}
rv = pthread_mutex_lock(queue->one_big_mutex);
if (rv != 0) {
Q_DBG("failed to lock mutex", queue);
return false;
}
if (rpa_queue_full(queue)) {
if (!queue->terminated) {
queue->full_waiters++;
if (wait_ms == RPA_WAIT_FOREVER) {
rv = pthread_cond_wait(queue->not_full, queue->one_big_mutex);
} else {
struct timespec abstime;
set_timeout(&abstime, wait_ms);
rv = pthread_cond_timedwait(queue->not_full, queue->one_big_mutex,
&abstime);
}
queue->full_waiters--;
if (rv != 0) {
pthread_mutex_unlock(queue->one_big_mutex);
return false;
}
}
/* If we wake up and it's still empty, then we were interrupted */
if (rpa_queue_full(queue)) {
Q_DBG("queue full (intr)", queue);
rv = pthread_mutex_unlock(queue->one_big_mutex);
if (rv != 0) {
return false;
}
if (queue->terminated) {
return false; /* no more elements ever again */
} else {
return false; //EINTR;
}
}
}
queue->data[queue->in] = data;
queue->in++;
if (queue->in >= queue->bounds) {
queue->in -= queue->bounds;
}
queue->nelts++;
if (queue->empty_waiters) {
Q_DBG("sig !empty", queue);
rv = pthread_cond_signal(queue->not_empty);
if (rv != 0) {
pthread_mutex_unlock(queue->one_big_mutex);
return false;
}
}
pthread_mutex_unlock(queue->one_big_mutex);
return true;
}
/**
* Push new data onto the queue. If the queue is full, return RPA_EAGAIN. If
* the push operation completes successfully, it signals other threads
* waiting in rpa_queue_pop() that they may continue consuming sockets.
*/
bool rpa_queue_trypush(rpa_queue_t *queue, void *data)
{
bool rv;
if (queue->terminated) {
return false; /* no more elements ever again */
}
rv = pthread_mutex_lock(queue->one_big_mutex);
if (rv != 0) {
return false;
}
if (rpa_queue_full(queue)) {
rv = pthread_mutex_unlock(queue->one_big_mutex);
return false; //EAGAIN;
}
queue->data[queue->in] = data;
queue->in++;
if (queue->in >= queue->bounds) {
queue->in -= queue->bounds;
}
queue->nelts++;
if (queue->empty_waiters) {
Q_DBG("sig !empty", queue);
rv = pthread_cond_signal(queue->not_empty);
if (rv != 0) {
pthread_mutex_unlock(queue->one_big_mutex);
return false;
}
}
pthread_mutex_unlock(queue->one_big_mutex);
return true;
}
/**
* not thread safe
*/
uint32_t rpa_queue_size(rpa_queue_t *queue) {
return queue->nelts;
}
/**
* Retrieves the next item from the queue. If there are no
* items available, it will block until one becomes available.
* Once retrieved, the item is placed into the address specified by
* 'data'.
*/
bool rpa_queue_pop(rpa_queue_t *queue, void **data)
{
return rpa_queue_timedpop(queue, data, RPA_WAIT_FOREVER);
}
bool rpa_queue_timedpop(rpa_queue_t *queue, void **data, int wait_ms)
{
bool rv;
if (wait_ms == RPA_WAIT_NONE) return rpa_queue_trypop(queue, data);
if (queue->terminated) {
return false; /* no more elements ever again */
}
rv = pthread_mutex_lock(queue->one_big_mutex);
if (rv != 0) {
return false;
}
/* Keep waiting until we wake up and find that the queue is not empty. */
if (rpa_queue_empty(queue)) {
if (!queue->terminated) {
queue->empty_waiters++;
if (wait_ms == RPA_WAIT_FOREVER) {
rv = pthread_cond_wait(queue->not_empty, queue->one_big_mutex);
} else {
struct timespec abstime;
set_timeout(&abstime, wait_ms);
rv = pthread_cond_timedwait(queue->not_empty, queue->one_big_mutex,
&abstime);
}
queue->empty_waiters--;
if (rv != 0) {
pthread_mutex_unlock(queue->one_big_mutex);
return false;
}
}
/* If we wake up and it's still empty, then we were interrupted */
if (rpa_queue_empty(queue)) {
Q_DBG("queue empty (intr)", queue);
rv = pthread_mutex_unlock(queue->one_big_mutex);
if (rv != 0) {
return false;
}
if (queue->terminated) {
return false; /* no more elements ever again */
} else {
return false; //EINTR;
}
}
}
*data = queue->data[queue->out];
queue->nelts--;
queue->out++;
if (queue->out >= queue->bounds) {
queue->out -= queue->bounds;
}
if (queue->full_waiters) {
Q_DBG("signal !full", queue);
rv = pthread_cond_signal(queue->not_full);
if (rv != 0) {
pthread_mutex_unlock(queue->one_big_mutex);
return false;
}
}
pthread_mutex_unlock(queue->one_big_mutex);
return true;
}
/**
* Retrieves the next item from the queue. If there are no
* items available, return RPA_EAGAIN. Once retrieved,
* the item is placed into the address specified by 'data'.
*/
bool rpa_queue_trypop(rpa_queue_t *queue, void **data)
{
bool rv;
if (queue->terminated) {
return false; /* no more elements ever again */
}
rv = pthread_mutex_lock(queue->one_big_mutex);
if (rv != 0) {
return false;
}
if (rpa_queue_empty(queue)) {
rv = pthread_mutex_unlock(queue->one_big_mutex);
return false; //EAGAIN;
}
*data = queue->data[queue->out];
queue->nelts--;
queue->out++;
if (queue->out >= queue->bounds) {
queue->out -= queue->bounds;
}
if (queue->full_waiters) {
Q_DBG("signal !full", queue);
rv = pthread_cond_signal(queue->not_full);
if (rv != 0) {
pthread_mutex_unlock(queue->one_big_mutex);
return false;
}
}
pthread_mutex_unlock(queue->one_big_mutex);
return true;
}
bool rpa_queue_interrupt_all(rpa_queue_t *queue)
{
bool rv;
Q_DBG("intr all", queue);
if ((rv = pthread_mutex_lock(queue->one_big_mutex)) != 0) {
return false;
}
pthread_cond_broadcast(queue->not_empty);
pthread_cond_broadcast(queue->not_full);
if ((rv = pthread_mutex_unlock(queue->one_big_mutex)) != 0) {
return false;
}
return true;
}
bool rpa_queue_term(rpa_queue_t *queue)
{
bool rv;
if ((rv = pthread_mutex_lock(queue->one_big_mutex)) != 0) {
return false;
}
/* we must hold one_big_mutex when setting this... otherwise,
* we could end up setting it and waking everybody up just after a
* would-be popper checks it but right before they block
*/
queue->terminated = 1;
if ((rv = pthread_mutex_unlock(queue->one_big_mutex)) != 0) {
return false;
}
return rpa_queue_interrupt_all(queue);
}