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
|
///usr/bin/true; gcc -std=c2x -Wall -Wextra -Wpedantic -pedantic-errors -fanalyzer -Wanalyzer-too-complex -ggdb -g3 -O0 main.c -o main && ./main; exit $?
#include <asm-generic/errno-base.h>
#include <stdlib.h>
#include <errno.h>
#include <error.h>
#include <threads.h>
#define RETURNWERR(eval, rval) do { \
errno = (eval); \
return (rval); \
} while (0)
#define ERROR(exitval, errval, msg, __VA_ARGS__) do { \
error((exitval), (errval), (msg)__VA_ARGS__); \
abort(); \
} while (0)
void * xcalloc(size_t nmemb, size_t size) {
void *mem = calloc(nmemb, size);
if(!mem)
ERROR(-1, errno, "<xcalloc> failed to allocate memory ", );
return mem;
}
typedef int (*gcallback)(void*);
typedef void (*fcallback)(void*);
typedef struct gdata {
void *data;
fcallback fcb;
} gdata;
gdata * gdata_init(void *data, fcallback fcb) {
gdata *gd = xcalloc(1, sizeof(*gd));
gd->data = data;
gd->fcb = fcb;
return gd;
}
void gdata_free(void *gd) {
if(!gd)
return;
gdata *real = (gdata*)gd;
if(real->fcb != NULL)
real->fcb(real->data);
free(real);
return;
}
void * gdata_getdata(gdata * const gd) {
if(!gd)
RETURNWERR(EINVAL, NULL);
return gd->data;
}
void * gdata_destruct(gdata *gd) {
if(!gd)
RETURNWERR(EINVAL, NULL);
void *data = gdata_getdata(gd);
free(gd);
return data;
}
typedef struct stack {
gdata **arr;
int size;
int used;
} stack;
stack * stack_init(int size) {
if(size < 1)
RETURNWERR(EINVAL, NULL);
stack *st = xcalloc(1, sizeof(*st));
st->size = size;
st->used = 0;
st->arr = xcalloc(st->size, sizeof(gdata*));
return st;
}
void stack_free(void *st) {
if(!st)
return;
stack *real = (stack*)st;
for(int i = 0; i < real->used; i++)
gdata_free(real->arr[i]);
free(real->arr);
free(real);
return;
}
int stack_push(stack *st, gdata *gd) {
if(!st || !gd)
RETURNWERR(EINVAL, -1);
if(!(st->used < st->size))
RETURNWERR(ENOMEM, 0);
st->arr[st->used++] = gd;
return st->used;
}
gdata * stack_pop(stack *st) {
if(!st)
RETURNWERR(EINVAL, NULL);
if(st->used <= 0)
RETURNWERR(ENODATA, 0);
return st->arr[--st->used];
}
int stack_pushd(stack *st, void *data, fcallback fcb) {
if(!st)
RETURNWERR(EINVAL, -1);
gdata *gd = gdata_init(data, fcb);
int retval = stack_push(st, gd);
if(retval <= 0)
gdata_free(gd);
return retval;
}
void * stack_popd(stack *st) {
if(!st)
RETURNWERR(EINVAL, NULL);
gdata *gd = stack_pop(st);
if(!gd)
return NULL;
return gdata_destruct(gd);
}
typedef struct cstack {
stack *st;
mtx_t mutex;
cnd_t cond;
unsigned char canceled;
} cstack;
cstack * cstack_init(int size) {
if(size < 1)
RETURNWERR(EINVAL, NULL);
cstack *cst = xcalloc(1, sizeof(*cst));
cst->st = stack_init(size);
cst->canceled = 0;
mtx_init(&cst->mutex, mtx_plain);
cnd_init(&cst->cond);
return cst;
}
int cstack_cancel(cstack * const cst) {
if(!cst)
RETURNWERR(EINVAL, -1);
mtx_lock(&cst->mutex);
cst->canceled = 1;
mtx_unlock(&cst->mutex);
cnd_broadcast(&cst->cond);
return 0;
}
void cstack_free(void *cst) {
if(!cst)
return;
cstack *real = (cstack*)cst;
cstack_cancel(real);
// Ok I don't think there's a good way to wait for all threads to die without registering the threads
return;
}
|