-
Notifications
You must be signed in to change notification settings - Fork 34
/
zn_deque.h
381 lines (302 loc) · 8.98 KB
/
zn_deque.h
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
#ifndef zn_deque_h
#define zn_deque_h
#ifndef ZN_NS_BEGIN
# ifdef __cplusplus
# define ZN_NS_BEGIN extern "C" {
# define ZN_NS_END }
# else
# define ZN_NS_BEGIN
# define ZN_NS_END
# endif
#endif /* ZN_NS_BEGIN */
#if !defined(ZN_API) && defined(_WIN32)
# ifdef ZN_IMPLEMENTATION
# define ZN_API __declspec(dllexport)
# else
# define ZN_API __declspec(dllimport)
# endif
#endif
#ifndef ZN_API
# define ZN_API extern
#endif
#include <stddef.h>
ZN_NS_BEGIN
typedef struct zn_Deque zn_Deque;
typedef struct zn_DequeItem zn_DequeItem;
typedef void zn_DequeHandler(void *ud, zn_DequeItem *item);
ZN_API zn_Deque *zn_newdeque (void);
ZN_API void zn_deldeque (zn_Deque *d, zn_DequeItem *head);
ZN_API void zn_pushfront (zn_Deque *d, zn_DequeItem *item);
ZN_API void zn_pushback (zn_Deque *d, zn_DequeItem *item);
ZN_API zn_DequeItem *zn_front (zn_Deque *d);
ZN_API zn_DequeItem *zn_back (zn_Deque *d);
ZN_API zn_DequeItem *zn_popfront (zn_Deque *d, int waitms);
ZN_API zn_DequeItem *zn_popback (zn_Deque *d, int waitms);
/* deque item routines */
ZN_API zn_DequeItem *zn_newitem (zn_Deque *d, size_t size);
ZN_API void zn_delitem (zn_DequeItem *item);
ZN_API void zn_inititem (zn_DequeItem *item);
ZN_API void zn_detachitem (zn_DequeItem *item);
ZN_API void zn_cleardeque (zn_Deque *d, zn_DequeItem *head);
ZN_API void zn_visititems (zn_DequeItem *head, zn_DequeHandler *h, void *ud);
struct zn_DequeItem {
zn_DequeItem *next;
zn_DequeItem *prev;
zn_Deque *deque;
};
ZN_NS_END
#endif /* zn_deque_h */
#if defined(ZN_IMPLEMENTATION) && !defined(zn_deque_implemented)
#define zn_deque_implemented
#include <stdlib.h>
ZN_NS_BEGIN
/* platform independency routines */
ZN_API void zn_inititem(zn_DequeItem *item)
{ item->next = item->prev = item; item->deque = NULL; }
static void znD_detach(zn_DequeItem *item) {
if (item != NULL) {
item->prev->next = item->next;
item->next->prev = item->prev;
item->prev = item->next = item;
item->deque = NULL;
}
}
static void znD_insert(zn_DequeItem *head, zn_DequeItem *item) {
item->next = head;
item->prev = head->prev;
head->prev->next = item;
head->prev = item;
}
ZN_API zn_DequeItem *zn_newitem(zn_Deque *d, size_t size) {
zn_DequeItem *item;
if (size < sizeof(zn_DequeItem))
size = sizeof(zn_DequeItem);
item = (zn_DequeItem*)malloc(size);
if (item == NULL) return NULL;
zn_inititem(item);
item->deque = d;
return item;
}
ZN_API void zn_delitem(zn_DequeItem *item) {
zn_detachitem(item);
free(item);
}
ZN_API void zn_visititems(zn_DequeItem *head, zn_DequeHandler *h, void *ud) {
zn_DequeItem *i, *next;
for (i = head->next, next = i->next;
i != head;
i = next, next = next->next)
h(ud, i);
}
#define ZN_DS_NORMAL 0
#define ZN_DS_EXIT 1
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
#endif /* WIN32_LEAN_AND_MEAN */
#include <Windows.h>
struct zn_Deque {
zn_DequeItem head;
CRITICAL_SECTION lock;
HANDLE event;
int wait_threads;
int status;
};
ZN_API zn_Deque *zn_newdeque(void) {
zn_Deque *d = (zn_Deque*)malloc(sizeof(zn_Deque));
if (d == NULL) return NULL;
d->event = CreateSemaphore(NULL, 0, LONG_MAX, 0);
if (d->event == NULL) {
free(d);
return NULL;
}
d->status = ZN_DS_NORMAL;
d->head.deque = d;
d->wait_threads = 0;
zn_inititem(&d->head);
InitializeCriticalSection(&d->lock);
return d;
}
ZN_API void zn_deldeque(zn_Deque *d, zn_DequeItem *head) {
int wait_threads;
EnterCriticalSection(&d->lock);
d->status = ZN_DS_EXIT;
wait_threads = d->wait_threads;
ReleaseSemaphore(d->event, wait_threads, NULL);
LeaveCriticalSection(&d->lock);
while (wait_threads != 0) {
EnterCriticalSection(&d->lock);
wait_threads = d->wait_threads;
LeaveCriticalSection(&d->lock);
}
EnterCriticalSection(&d->lock);
if (head) *head = d->head;
zn_inititem(&d->head);
LeaveCriticalSection(&d->lock);
CloseHandle(d->event);
DeleteCriticalSection(&d->lock);
free(d);
}
static zn_DequeItem *zn_pop(zn_Deque *d, int waitms, zn_DequeItem *(*f)(zn_Deque *d)) {
int status, should_wait = 0;
zn_DequeItem *item;
EnterCriticalSection(&d->lock);
status = d->status;
item = f(d);
znD_detach(item);
if (item == NULL && waitms != 0 && status == ZN_DS_NORMAL) {
should_wait = 1;
++d->wait_threads;
}
LeaveCriticalSection(&d->lock);
if (should_wait) {
DWORD time = waitms >= 0 ? (DWORD)waitms : INFINITE;
WaitForSingleObject(d->event, time);
EnterCriticalSection(&d->lock);
--d->wait_threads;
item = f(d);
znD_detach(item);
LeaveCriticalSection(&d->lock);
}
return item;
}
static void znD_lock(zn_Deque *d) { EnterCriticalSection(&d->lock); }
static void znD_unlock(zn_Deque *d) { LeaveCriticalSection(&d->lock); }
static void znD_signal(zn_Deque *d) { ReleaseSemaphore(d->event, 1, NULL); }
#else
#include <pthread.h>
struct zn_Deque {
zn_DequeItem head;
pthread_mutex_t lock;
pthread_cond_t event;
int wait_threads;
int status;
};
ZN_API zn_Deque *zn_newdeque(void) {
zn_Deque *d = (zn_Deque*)malloc(sizeof(zn_Deque));
if (d == NULL) return NULL;
if (pthread_mutex_init(&d->lock, NULL) != 0) goto err;
if (pthread_cond_init(&d->event, NULL) != 0) goto err;
d->status = ZN_DS_NORMAL;
d->head.deque = d;
d->wait_threads = 0;
zn_inititem(&d->head);
return d;
err:
free(d);
return NULL;
}
ZN_API void zn_deldeque(zn_Deque *d, zn_DequeItem *head) {
int wait_threads;
pthread_mutex_lock(&d->lock);
d->status = ZN_DS_EXIT;
wait_threads = d->wait_threads;
pthread_mutex_unlock(&d->lock);
while (wait_threads != 0) {
pthread_mutex_lock(&d->lock);
pthread_cond_broadcast(&d->event);
wait_threads = d->wait_threads;
pthread_mutex_unlock(&d->lock);
}
if (head) *head = d->head;
zn_inititem(&d->head);
pthread_mutex_unlock(&d->lock);
pthread_cond_destroy(&d->event);
pthread_mutex_destroy(&d->lock);
free(d);
}
static zn_DequeItem *zn_pop(zn_Deque *d, int waitms, zn_DequeItem *(*f)(zn_Deque *d)) {
int status;
zn_DequeItem *item;
pthread_mutex_lock(&d->lock);
status = d->status;
item = f(d);
znD_detach(item);
if (item == NULL && status == ZN_DS_NORMAL && waitms != 0) {
++d->wait_threads;
if (waitms < 0)
pthread_cond_wait(&d->event, &d->lock);
else {
struct timespec ts;
ts.tv_sec = waitms / 1000;
ts.tv_nsec = (waitms % 1000) * 1000000;
pthread_cond_timedwait(&d->event, &d->lock, &ts);
}
--d->wait_threads;
item = f(d);
znD_detach(item);
}
pthread_mutex_unlock(&d->lock);
return item;
}
static void znD_lock(zn_Deque *d) { pthread_mutex_lock(&d->lock); }
static void znD_unlock(zn_Deque *d) { pthread_mutex_unlock(&d->lock); }
static void znD_signal(zn_Deque *d) { pthread_cond_signal(&d->event); }
#endif
/* generated locked routines */
ZN_API zn_DequeItem *zn_popfront(zn_Deque *d, int waitms)
{ return zn_pop(d, waitms, znD_frontU); }
ZN_API zn_DequeItem *zn_popback(zn_Deque *d, int waitms)
{ return zn_pop(d, waitms, znD_backU); }
static zn_DequeItem *znD_frontU(zn_Deque *d) {
zn_DequeItem *item = d->head.next;
if (item == &d->head)
return NULL;
return item;
}
static zn_DequeItem *znD_backU(zn_Deque *d) {
zn_DequeItem *item = d->head.prev;
if (item == &d->head)
return NULL;
return item;
}
ZN_API void zn_cleardeque(zn_Deque *d, zn_DequeItem *head) {
znD_lock(d);
if (head) *head = d->head;
zn_inititem(&d->head);
znD_unlock(d);
}
ZN_API void zn_detachitem(zn_DequeItem *item) {
if (item) {
zn_Deque *d = item->deque;
if (d == NULL) return;
znD_lock(d);
if (item->deque == d)
znD_detach(item);
znD_unlock(d);
}
}
ZN_API void zn_pushfront(zn_Deque *d, zn_DequeItem *item) {
if (d->status == ZN_DS_EXIT || item->deque != NULL) return;
znD_lock(d);
item->deque = d;
znD_insert(d->head.next, item);
znD_signal(d);
znD_unlock(d);
}
ZN_API void zn_pushback(zn_Deque *d, zn_DequeItem *item) {
if (d->status == ZN_DS_EXIT || item->deque != NULL) return;
znD_lock(d);
item->deque = d;
znD_insert(&d->head, item);
znD_signal(d);
znD_unlock(d);
}
ZN_API zn_DequeItem *zn_front(zn_Deque *d) {
zn_DequeItem *item;
znD_lock(d);
item = znD_frontU(d);
znD_unlock(d);
return item;
}
ZN_API zn_DequeItem *zn_back(zn_Deque *d) {
zn_DequeItem *item;
znD_lock(d);
item = znD_backU(d);
znD_unlock(d);
return item;
}
ZN_NS_END
#endif /* ZN_IMPLEMENTATION */
/* win32cc: flags+='-s -O3 -mdll -DZN_IMPLEMENTATION -xc' output='zn_deque.dll'
unixcc: flags+='-O3 -shared -fPIC -DZN_IMPLEMENTATION -xc' output='zn_deque.so' */