-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasyncqueue.c
231 lines (175 loc) · 4.43 KB
/
asyncqueue.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
/***************************************************************************
begin........: June 2012
copyright....: Sebastian Fedrau
email........: [email protected]
***************************************************************************/
/***************************************************************************
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License v3 as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License v3 for more details.
***************************************************************************/
/**
* \file asyncqueue.c
* \brief Asynchronous queue.
* \author Sebastian Fedrau <[email protected]>
*/
#ifdef WITH_PTHREAD
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <assert.h>
#include <err.h>
#include "asyncqueue.h"
AsyncQueue *
async_queue_new(CompareFunc compare, FreeFunc free, Pool *pool)
{
assert(compare);
AsyncQueue *queue = (AsyncQueue *)malloc(sizeof(AsyncQueue));
if(!queue)
{
perror("malloc()");
abort();
}
async_queue_init(queue, compare, free, pool);
return queue;
}
#define PTHREAD_CRITICAL_CALL(fn, ...) \
{ \
int rc = fn(__VA_ARGS__); \
if (rc) \
{ \
err(rc, "%s()", #fn); \
abort(); \
} \
}
void
async_queue_init(AsyncQueue *queue, CompareFunc compare, FreeFunc free, Pool *pool)
{
assert(queue != NULL);
assert(compare != NULL);
queue_init(&queue->queue, compare, free, pool);
queue->waiting = 0;
PTHREAD_CRITICAL_CALL(pthread_mutex_init, &queue->mutex, NULL);
PTHREAD_CRITICAL_CALL(pthread_cond_init, &queue->cond, NULL);
}
void
async_queue_destroy(AsyncQueue *queue)
{
assert(queue != NULL);
async_queue_free(queue);
free(queue);
}
#define PTHREAD_CALL(fn, ...) \
{ \
int rc = fn(__VA_ARGS__); \
if (rc) \
{ \
err(rc, "%s()", #fn); \
} \
}
void
async_queue_free(AsyncQueue *queue)
{
assert(queue != NULL);
queue_free(&queue->queue);
PTHREAD_CALL(pthread_mutex_destroy, &queue->mutex);
PTHREAD_CALL(pthread_cond_destroy, &queue->cond);
}
void
async_queue_push(AsyncQueue *queue, void *data)
{
assert(queue != NULL);
PTHREAD_CRITICAL_CALL(pthread_mutex_lock, &queue->mutex);
queue_push(&queue->queue, data);
if(queue->waiting)
{
PTHREAD_CALL(pthread_cond_signal, &queue->cond);
}
PTHREAD_CRITICAL_CALL(pthread_mutex_unlock, &queue->mutex);
}
static bool
_async_queue_try_lock(AsyncQueue *queue)
{
assert(queue != NULL);
int rc = pthread_mutex_lock(&queue->mutex);
if(rc)
{
err(rc, "pthread_mutex_lock()");
}
return !rc;
}
static bool
_async_queue_cond_wait(AsyncQueue *queue, uint32_t ms)
{
assert(queue != NULL);
bool success = false;
if(queue->waiting < SIZE_MAX)
{
++queue->waiting;
if(ms)
{
struct timespec val;
struct timeval now;
gettimeofday(&now, NULL);
val.tv_sec = now.tv_sec + (ms / 1000);
val.tv_nsec = 0;
success = !pthread_cond_timedwait(&queue->cond, &queue->mutex, &val);
}
else
{
success = !pthread_cond_wait(&queue->cond, &queue->mutex);
}
--queue->waiting;
}
else
{
fprintf(stderr, "%s(): integer overflow.\n", __func__);
}
return success;
}
static bool
_async_queue_pop(AsyncQueue *queue, void *data, uint32_t ms)
{
assert(queue != NULL);
bool success = false;
if(_async_queue_try_lock(queue))
{
success = queue_pop(&queue->queue, data)
|| (_async_queue_cond_wait(queue, ms) && queue_pop(&queue->queue, data));
PTHREAD_CRITICAL_CALL(pthread_mutex_unlock, &queue->mutex);
}
return success;
}
bool
async_queue_pop(AsyncQueue *queue, void *data)
{
assert(queue != NULL);
return _async_queue_pop(queue, data, 0);
}
bool
async_queue_pop_timeout(AsyncQueue *queue, void *data, uint32_t ms)
{
return _async_queue_pop(queue, data, ms);
}
void
async_queue_clear(AsyncQueue *queue)
{
assert(queue != NULL);
PTHREAD_CRITICAL_CALL(pthread_mutex_lock, &queue->mutex);
queue_clear(&queue->queue);
PTHREAD_CRITICAL_CALL(pthread_mutex_unlock, &queue->mutex);
}
size_t
async_queue_count(AsyncQueue *queue)
{
assert(queue != NULL);
PTHREAD_CRITICAL_CALL(pthread_mutex_lock, &queue->mutex);
size_t count = queue_count(&queue->queue);
PTHREAD_CRITICAL_CALL(pthread_mutex_unlock, &queue->mutex);
return count;
}
#endif /* WITH_PTHREAD */