-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmt-cirque.c
273 lines (256 loc) · 6.6 KB
/
mt-cirque.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
#include "mt-cirque.h"
queue *make_queue(char *name, int size, int mt_safe, int is_one_use_only)
{
queue *new = malloc(sizeof(queue));
if (new == NULL)
{
fprintf(stderr, "Error in malloc");
exit(1);
}
new->name = name;
new->head = 0;
new->tail = 0;
new->count = 0;
if (size == 0)
new->capacity = DEFAULT_QUEUE_CAPACITY;
else
new->capacity = size;
/* new->data is a pointer to first (which is a) string */
new->data = malloc(new->capacity * sizeof(*new->data));
if (new->data == NULL)
{
fprintf(stderr, "Error in malloc");
exit(1);
}
new->is_mt_safe = mt_safe;
new->is_one_use_only = is_one_use_only;
if (mt_safe)
{
if (sem_init(&new->mutex,
0 /* shared between threads */,
1 /* Only 1 use at a time */) != 0)
{
fputs("Unable to create semaphore\n", stderr);
exit(1);
}
if (sem_init(&new->items_available,
0 /* shared between threads */,
0 /* Start at 0 items*/) != 0)
{
fputs("Unable to create semaphore\n", stderr);
exit(1);
}
if (sem_init(&new->space_available,
0 /* shared between threads */,
size /* Start at max space */) != 0)
{
fputs("Unable to create semaphore\n", stderr);
exit(1);
}
}
return new;
}
void destroy_queue(queue *q)
{
if (q->is_mt_safe)
{
int rv = sem_destroy(&q->items_available);
if (rv == -1)
{
fprintf(stderr, "Error in sem_destroy");
}
rv = sem_destroy(&q->space_available);
if (rv == -1)
{
fprintf(stderr, "Error in sem_destroy");
}
rv = sem_destroy(&q->mutex);
if (rv == -1)
{
fprintf(stderr, "Error in sem_destroy");
}
}
free(q->data);
free(q);
}
void queue_display(queue *q)
{
printf("head -> ");
int count = q->head + q->count;
for (int i = q->head; i < count; i++)
{
printf("%s (%d) ", q->data[i], i);
if (i != count - 1)
{
printf("-> ");
}
}
puts("<- tail");
}
int queue_has_items_available(queue *q)
{
int result;
int rv;
if (q->is_mt_safe)
{
rv = sem_getvalue(&q->items_available, &result);
if (rv == -1)
{
fprintf(stderr, "Error in sem_getvalue");
exit(1);
}
}
else
result = q->count;
return result;
}
void _queue_expand_if_needed(queue *q)
{
void *new_data;
/* Check if needs to grow */
//printf("queue bounds: %d, %d\n", q->count + q->head + 1, q->capacity);
if ((q->count + q->head + 1) == q->capacity)
{
// puts("growing");
q->capacity *= 2;
new_data = realloc(q->data, sizeof(*q->data) * q->capacity);
q->data = new_data;
}
}
void queue_push(queue *q, char *str, char *caller_name)
{
int rv;
UNUSED(caller_name);
if (q->is_mt_safe)
{
// printf("in %s: trying to acquire space_available for %s (start push)\n",
// q->name, caller_name);
// rv = fflush(stdout); // necessary for some reason
// if (rv == EOF)
// {
// fprintf(stderr, "Error in fflush");
// }
rv = sem_wait(&q->space_available);
if (rv == -1)
{
fprintf(stderr, "Error in sem_wait");
exit(1);
}
rv = sem_wait(&q->mutex);
if (rv == -1)
{
fprintf(stderr, "Error in sem_wait");
exit(1);
}
// printf("in %s: Now acquired space_available for %s (start push)\n",
// q->name, caller_name);
// rv = fflush(stdout); // necessary for some reason
// if (rv == EOF)
// {
// fprintf(stderr, "Error in fflush");
// }
}
else
{
// printf("in %s: about to pop for %s \n", q->name, caller_name);
}
/* Make queue dynamically grow if needed*/
_queue_expand_if_needed(q);
// the queue grows from its tail
strcpy(q->data[q->tail], str);
q->tail++;
q->count++;
if (q->is_mt_safe)
{
// printf("in %s: signaling items_available for %s (end push)\n",
// q->name, caller_name);
rv = sem_post(&q->mutex);
if (rv == -1)
{
fprintf(stderr, "Error in sem_post");
exit(1);
}
rv = sem_post(&q->items_available);
if (rv == -1)
{
fprintf(stderr, "Error in sem_post");
exit(1);
}
}
}
char *queue_pop(queue *q, char *result, char *caller_name)
{
int rv;
UNUSED(caller_name);
if (q->is_mt_safe)
{
// printf("in %s: trying to acquire items_available for %s (start pop)\n",
// q->name, caller_name);
if (q->is_one_use_only)
{
rv = sem_trywait(&q->items_available);
if (rv == -1)
{
if (errno == EAGAIN)
{
return NULL;
}
else
{
fprintf(stderr, "Error in sem_wait");
exit(1);
}
}
}
else
{
rv = sem_wait(&q->items_available);
if (rv == -1)
{
fprintf(stderr, "Error in sem_wait");
exit(1);
}
}
rv = sem_wait(&q->mutex);
if (rv == -1)
{
fprintf(stderr, "Error in sem_wait");
exit(1);
}
}
else
{
if (q->head == q->tail)
{
return NULL;
}
}
// printf("in %s: Now acquired items_available for %s\n",
// q->name, caller_name);
// queue_display(q);
strcpy(result, q->data[q->head]);
// We pop from the head of the queue;
// This has the desirable property that we could in theory
// pop and push independently from the different
// sides
q->head++;
q->count--;
if (q->is_mt_safe)
{
// printf("in %s: signaling space_available for %s (end pop)\n",
// q->name, caller_name);
rv = sem_post(&q->mutex);
if (rv == -1)
{
fprintf(stderr, "Error in sem_post");
exit(1);
}
rv = sem_post(&q->space_available);
if (rv == -1)
{
fprintf(stderr, "Error in sem_post");
exit(1);
}
}
return result;
}