-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtmq.c
executable file
·315 lines (261 loc) · 7.69 KB
/
mtmq.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
/* Multi-Threaded Memory-based message Queue.
*
*/
#include "mtmq.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
/* Clock type to use when waiting on condition variable.
*
* Use of CLOCK_MONOTONIC ensures waiting does not hang if
* host time is set back in time. But it isn't supported on MinGW.
*/
#ifdef _WIN32
# define MTMQ_CLOCK_TYPE CLOCK_REALTIME
#else
# define MTMQ_CLOCK_TYPE CLOCK_MONOTONIC
#endif
// Queue element.
typedef struct mtmq_elt {
int code;
void *data;
} mtmq_elt_t;
// Queue.
struct mtmq {
pthread_mutex_t mtx; // mutex
pthread_cond_t cond_rd; // condition variable for readers
pthread_cond_t cond_wr; // condition variable for writers
int num_rd; // number of currently waiting readers
int num_wr; // number of currently waiting writers
int fin; // finalized flag
int num; // number of elements in queue
int first; // index of first element in queue
int last; // index of last element in queue + 1
int size; // queue max size
struct mtmq_elt *arr; // queue elements array
};
// Internal helper function for timeout calculation.
static void calc_abs_timeout(struct timespec *ts, int timeout_ms)
{
struct timespec t;
t.tv_nsec = timeout_ms; // store to wider type
t.tv_nsec *= 1000000; // convert milliseconds to nanosecons
clock_gettime(MTMQ_CLOCK_TYPE, ts);
ts->tv_nsec += t.tv_nsec;
ts->tv_sec += ts->tv_nsec / 1000000000;
ts->tv_nsec = ts->tv_nsec % 1000000000;
}
/* Create queue.
* In:
* size - queue size
* Out:
* NULL - create failed
* not NULL - pointer to created queue
*/
mtmq_t *mtmq_create(int size)
{
mtmq_t *ret;
int rc;
size_t mtmq_size = sizeof(mtmq_t);
mtmq_size += (~mtmq_size + 1) & (sizeof(void*)-1);
size_t arr_size = sizeof(mtmq_elt_t) * size;
ret = malloc(mtmq_size + arr_size);
if (ret == NULL)
return NULL;
memset(ret, 0, mtmq_size + arr_size);
ret->size = size;
ret->arr = (mtmq_elt_t*)(ret+1);
pthread_mutex_init(&ret->mtx, NULL);
pthread_condattr_t ca;
pthread_condattr_init(&ca);
rc = pthread_condattr_setclock(&ca, MTMQ_CLOCK_TYPE);
if (rc != 0) {
pthread_condattr_destroy(&ca);
pthread_mutex_destroy(&ret->mtx);
free(ret);
return NULL;
}
pthread_cond_init(&ret->cond_rd, &ca);
pthread_cond_init(&ret->cond_wr, &ca);
pthread_condattr_destroy(&ca);
return ret;
}
/* Destroy queue.
* In:
* q - queue pointer
* Out:
* MTMQ_RC_OK - queue deleted
* MTMQ_RC_ERROR - some error occured
*/
int mtmq_destroy(mtmq_t *q)
{
if (!q)
return MTMQ_RC_ERROR;
int rc = pthread_cond_destroy(&q->cond_wr);
if (rc)
return MTMQ_RC_ERROR;
rc = pthread_cond_destroy(&q->cond_rd);
if (rc)
return MTMQ_RC_ERROR;
rc = pthread_mutex_destroy(&q->mtx);
if (rc)
return MTMQ_RC_ERROR;
free(q);
return MTMQ_RC_OK;
}
/* Push message to queue.
* In:
* q - queue
* code - integer code to put to queue
* data - pointer to some application data to put to queue
* timeout - timeout in milliseconds for operation to complete
* if timeout < 0, then wait indefinately for queue to become
* available for writing or is finalized.
* Out:
* MTMQ_RC_OK - done
* MTMQ_RC_FINALIZED - not done because queue is finalized
* MTMQ_RC_TIMEDOUT - not done because of timeout
* MTMQ_RC_INTERRUPTED - not done because waiting on condition variable was
* interrupted by signal (note that on posix-compliant system this should
* never happen)
* MTMQ_RC_ERROR - some error occured that requires investigation and debugging.
*/
int mtmq_push(mtmq_t *q, int code, void *data, int timeout)
{
int ret, rc;
if (!q)
return MTMQ_RC_ERROR;
rc = pthread_mutex_lock(&q->mtx);
if (rc != 0)
return MTMQ_RC_ERROR;
q->num_wr++;
if (timeout < 0) {
for (rc=0; !q->fin && q->num==q->size && rc==0; ) {
rc = pthread_cond_wait(&q->cond_wr, &q->mtx);
}
} else {
struct timespec to;
calc_abs_timeout(&to, timeout);
for (rc=0; !q->fin && q->num==q->size && rc==0; ) {
rc = pthread_cond_timedwait(&q->cond_wr, &q->mtx, &to);
}
}
q->num_wr--;
if (q->fin)
ret = MTMQ_RC_FINALIZED;
else if (q->num < q->size) {
mtmq_elt_t *e = &q->arr[q->last];
e->code = code;
e->data = data;
q->last = (q->last+1 < q->size) ? (q->last+1) : 0;
q->num++;
if (q->num_rd)
pthread_cond_signal(&q->cond_rd);
ret = MTMQ_RC_OK;
} else if (rc == ETIMEDOUT)
ret = MTMQ_RC_TIMEDOUT;
else if (rc == EINTR)
ret = MTMQ_RC_INTERRUPTED;
else
ret = MTMQ_RC_ERROR;
pthread_mutex_unlock(&q->mtx);
return ret;
}
/* Pop message from queue.
* In:
* q - queue
* [out]code - integer code to retrieve from queue
* [out]data - pointer to some application data to retrieve from queue
* timeout - timeout in milliseconds for operation to complete
* if timeout < 0, then wait indefinately for queue to become
* available for reading or is finalized.
* Out:
* MTMQ_RC_OK - done
* MTMQ_RC_FINALIZED - not done because queue is finalized
* MTMQ_RC_TIMEDOUT - not done because of timeout
* MTMQ_RC_INTERRUPTED - not done because waiting on condition variable was
* interrupted by signal (note that on posix-compliant system this should
* never happen)
* MTMQ_RC_ERROR - some error occured that requires investigation and debugging.
* Note:
* When queue is finalized consumer(s) will retrieve all available data from
* queue before it(they) get MTMQ_RC_FINALIZED return code.
*/
int mtmq_pop(mtmq_t *q, int *code, void **data, int timeout)
{
int ret, rc;
if (!q)
return MTMQ_RC_ERROR;
rc = pthread_mutex_lock(&q->mtx);
if (rc != 0)
return MTMQ_RC_ERROR;
q->num_rd++;
if (timeout < 0) {
for (rc=0; !q->num && !q->fin && rc==0; ) {
rc = pthread_cond_wait(&q->cond_rd, &q->mtx);
}
} else {
struct timespec to;
calc_abs_timeout(&to, timeout);
for (rc=0; !q->num && !q->fin && rc==0; ) {
rc = pthread_cond_timedwait(&q->cond_rd, &q->mtx, &to);
}
}
q->num_rd--;
if (q->num) {
mtmq_elt_t *e = &q->arr[q->first];
*code = e->code;
*data = e->data;
q->first = (q->first+1 < q->size) ? (q->first+1) : 0;
q->num--;
if (q->num_wr)
pthread_cond_signal(&q->cond_wr);
ret = MTMQ_RC_OK;
} else if (q->fin)
ret = MTMQ_RC_FINALIZED;
else if (rc == ETIMEDOUT)
ret = MTMQ_RC_TIMEDOUT;
else if (rc == EINTR)
ret = MTMQ_RC_INTERRUPTED;
else
ret = MTMQ_RC_ERROR;
pthread_mutex_unlock(&q->mtx);
return ret;
}
/* Finalize message processing by this queue.
* In:
* q - queue
* Note:
* After finalizing queue caller should make sure all producers and consumers
* are not using this queue any more and then destroy queue by calling mtmq_destroy(q).
*/
void mtmq_finalize(mtmq_t *q)
{
if (!q)
return;
pthread_mutex_lock(&q->mtx);
if (!q->fin) {
q->fin = 1;
if (q->num_rd) pthread_cond_broadcast(&q->cond_rd);
if (q->num_wr) pthread_cond_broadcast(&q->cond_wr);
}
pthread_mutex_unlock(&q->mtx);
}
/* Check if queue is finalized.
* In:
* q - queue
* Out:
* 0 - queue is not finalized
* 1 - queue is finalized
*/
int mtmq_is_finalized(mtmq_t *q)
{
int ret = 1;
if (q) {
pthread_mutex_lock(&q->mtx);
ret = q->fin;
pthread_mutex_unlock(&q->mtx);
}
return ret;
}