-
Notifications
You must be signed in to change notification settings - Fork 171
/
ConcBlockingQueue.c
307 lines (266 loc) · 10.6 KB
/
ConcBlockingQueue.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
#include "moar.h"
/* This representation's function pointer table. */
static const MVMREPROps ConcBlockingQueue_this_repr;
/* Creates a new type object of this representation, and associates it with
* the given HOW. */
static MVMObject * type_object_for(MVMThreadContext *tc, MVMObject *HOW) {
MVMSTable *st = MVM_gc_allocate_stable(tc, &ConcBlockingQueue_this_repr, HOW);
MVMROOT(tc, st, {
MVMObject *obj = MVM_gc_allocate_type_object(tc, st);
MVM_ASSIGN_REF(tc, &(st->header), st->WHAT, obj);
st->size = sizeof(MVMConcBlockingQueue);
});
return st->WHAT;
}
/* Initializes a new instance. */
static void initialize(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data) {
MVMConcBlockingQueue *cbq = (MVMConcBlockingQueue*)root;
MVMConcBlockingQueueBody *body = MVM_calloc(1, sizeof(MVMConcBlockingQueueBody));
/* Initialize locks. */
int init_stat;
if ((init_stat = uv_mutex_init(&body->head_lock)) < 0)
MVM_exception_throw_adhoc(tc, "Failed to initialize mutex: %s",
uv_strerror(init_stat));
if ((init_stat = uv_mutex_init(&body->tail_lock)) < 0)
MVM_exception_throw_adhoc(tc, "Failed to initialize mutex: %s",
uv_strerror(init_stat));
if ((init_stat = uv_cond_init(&body->head_cond)) < 0)
MVM_exception_throw_adhoc(tc, "Failed to initialize condition variable: %s",
uv_strerror(init_stat));
/* Head and tail point to a null node. */
body->tail = body->head = MVM_calloc(1, sizeof(MVMConcBlockingQueueNode));
cbq->body = body;
}
/* Copies the body of one object to another. */
static void copy_to(MVMThreadContext *tc, MVMSTable *st, void *src, MVMObject *dest_root, void *dest) {
MVM_exception_throw_adhoc(tc, "Cannot copy object with representation ConcBlockingQueue");
}
/* Called by the VM to mark any GCable items. */
static void gc_mark(MVMThreadContext *tc, MVMSTable *st, void *data, MVMGCWorklist *worklist) {
/* At this point we know the world is stopped, and thus we can safely do a
* traversal of the data structure without needing locks. */
MVMConcBlockingQueueBody *cbq = *(MVMConcBlockingQueueBody **)data;
MVMConcBlockingQueueNode *cur = cbq->head;
while (cur) {
MVM_gc_worklist_add(tc, worklist, &cur->value);
cur = cur->next;
}
}
/* Called by the VM in order to free memory associated with this object. */
static void gc_free(MVMThreadContext *tc, MVMObject *obj) {
MVMConcBlockingQueue *cbq = (MVMConcBlockingQueue *)obj;
/* First, free all the nodes. */
MVMConcBlockingQueueBody *body = cbq->body;
MVMConcBlockingQueueNode *cur = body->head;
while (cur) {
MVMConcBlockingQueueNode *next = cur->next;
MVM_free(cur);
cur = next;
}
body->head = body->tail = NULL;
/* Clean up */
uv_mutex_destroy(&body->head_lock);
uv_mutex_destroy(&body->tail_lock);
uv_cond_destroy(&body->head_cond);
/* Clean up body */
MVM_free(body);
}
static const MVMStorageSpec storage_spec = {
MVM_STORAGE_SPEC_REFERENCE, /* inlineable */
0, /* bits */
0, /* align */
MVM_STORAGE_SPEC_BP_NONE, /* boxed_primitive */
0, /* can_box */
0, /* is_unsigned */
};
/* Gets the storage specification for this representation. */
static const MVMStorageSpec * get_storage_spec(MVMThreadContext *tc, MVMSTable *st) {
return &storage_spec;
}
/* Compose the representation. */
static void compose(MVMThreadContext *tc, MVMSTable *st, MVMObject *info) {
/* Nothing to do for this REPR. */
}
static void at_pos(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMint64 index, MVMRegister *value, MVMuint16 kind) {
MVMConcBlockingQueueBody *body = *(MVMConcBlockingQueueBody **)data;
if (index != 0)
MVM_exception_throw_adhoc(tc,
"Can only request (peek) head of a concurrent blocking queue");
if (kind != MVM_reg_obj)
MVM_exception_throw_adhoc(tc,
"Can only get objects from a concurrent blocking queue");
if (MVM_load(&body->elems) > 0) {
MVMConcBlockingQueueNode *peeked;
unsigned int interval_id;
interval_id = MVM_telemetry_interval_start(tc, "ConcBlockingQueue.at_pos");
MVMROOT(tc, root, {
MVM_gc_mark_thread_blocked(tc);
uv_mutex_lock(&body->head_lock);
MVM_gc_mark_thread_unblocked(tc);
});
peeked = body->head->next;
value->o = peeked ? peeked->value : tc->instance->VMNull;
uv_mutex_unlock(&body->head_lock);
MVM_telemetry_interval_stop(tc, interval_id, "ConcBlockingQueue.at_pos");
}
else {
value->o = tc->instance->VMNull;
}
}
static MVMuint64 elems(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data) {
MVMConcBlockingQueueBody *cbq = *(MVMConcBlockingQueueBody **)data;
return MVM_load(&(cbq->elems));
}
static void push(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMRegister value, MVMuint16 kind) {
MVMConcBlockingQueueBody *body = *(MVMConcBlockingQueueBody**)data;
MVMConcBlockingQueueNode *add;
AO_t orig_elems;
MVMObject *to_add = value.o;
unsigned int interval_id;
if (kind != MVM_reg_obj)
MVM_exception_throw_adhoc(tc,
"Can only push objects to a concurrent blocking queue");
if (value.o == NULL)
MVM_exception_throw_adhoc(tc,
"Cannot store a null value in a concurrent blocking queue");
add = MVM_calloc(1, sizeof(MVMConcBlockingQueueNode));
interval_id = MVM_telemetry_interval_start(tc, "ConcBlockingQueue.push");
MVMROOT2(tc, root, to_add, {
MVM_gc_mark_thread_blocked(tc);
uv_mutex_lock(&body->tail_lock);
MVM_gc_mark_thread_unblocked(tc);
});
MVM_ASSIGN_REF(tc, &(root->header), add->value, to_add);
body->tail->next = add;
body->tail = add;
orig_elems = MVM_incr(&body->elems);
uv_mutex_unlock(&body->tail_lock);
if (orig_elems == 0) {
MVMROOT(tc, root, {
MVM_gc_mark_thread_blocked(tc);
uv_mutex_lock(&body->head_lock);
MVM_gc_mark_thread_unblocked(tc);
});
uv_cond_signal(&body->head_cond);
uv_mutex_unlock(&body->head_lock);
}
MVM_telemetry_interval_stop(tc, interval_id, "ConcBlockingQueue.push");
}
static void shift(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMRegister *value, MVMuint16 kind) {
MVMConcBlockingQueueBody *body = *(MVMConcBlockingQueueBody**)data;
MVMConcBlockingQueueNode *taken;
unsigned int interval_id;
if (kind != MVM_reg_obj)
MVM_exception_throw_adhoc(tc, "Can only shift objects from a ConcBlockingQueue");
interval_id = MVM_telemetry_interval_start(tc, "ConcBlockingQueue.shift");
MVMROOT(tc, root, {
MVM_gc_mark_thread_blocked(tc);
uv_mutex_lock(&body->head_lock);
MVM_gc_mark_thread_unblocked(tc);
while (MVM_load(&body->elems) == 0) {
MVM_gc_mark_thread_blocked(tc);
uv_cond_wait(&body->head_cond, &body->head_lock);
MVM_gc_mark_thread_unblocked(tc);
}
});
taken = body->head->next;
MVM_free(body->head);
body->head = taken;
MVM_barrier();
value->o = taken->value;
taken->value = NULL;
MVM_barrier();
if (MVM_decr(&body->elems) > 1)
uv_cond_signal(&body->head_cond);
uv_mutex_unlock(&body->head_lock);
MVM_telemetry_interval_stop(tc, interval_id, "ConcBlockingQueue.shift");
}
/* Set the size of the STable. */
static void deserialize_stable_size(MVMThreadContext *tc, MVMSTable *st, MVMSerializationReader *reader) {
st->size = sizeof(MVMConcBlockingQueue);
}
/* Initializes the representation. */
const MVMREPROps * MVMConcBlockingQueue_initialize(MVMThreadContext *tc) {
return &ConcBlockingQueue_this_repr;
}
static const MVMREPROps ConcBlockingQueue_this_repr = {
type_object_for,
MVM_gc_allocate_object,
initialize,
copy_to,
MVM_REPR_DEFAULT_ATTR_FUNCS,
MVM_REPR_DEFAULT_BOX_FUNCS,
{
at_pos,
MVM_REPR_DEFAULT_BIND_POS,
MVM_REPR_DEFAULT_SET_ELEMS,
push,
MVM_REPR_DEFAULT_POP,
MVM_REPR_DEFAULT_UNSHIFT,
shift,
MVM_REPR_DEFAULT_SLICE,
MVM_REPR_DEFAULT_SPLICE,
MVM_REPR_DEFAULT_AT_POS_MULTIDIM,
MVM_REPR_DEFAULT_BIND_POS_MULTIDIM,
MVM_REPR_DEFAULT_DIMENSIONS,
MVM_REPR_DEFAULT_SET_DIMENSIONS,
MVM_REPR_DEFAULT_GET_ELEM_STORAGE_SPEC,
MVM_REPR_DEFAULT_POS_AS_ATOMIC,
MVM_REPR_DEFAULT_POS_AS_ATOMIC_MULTIDIM
}, /* pos_funcs */
MVM_REPR_DEFAULT_ASS_FUNCS,
elems,
get_storage_spec,
NULL, /* change_type */
NULL, /* serialize */
NULL, /* deserialize */
NULL, /* serialize_repr_data */
NULL, /* deserialize_repr_data */
deserialize_stable_size,
gc_mark,
gc_free,
NULL, /* gc_cleanup */
NULL, /* gc_mark_repr_data */
NULL, /* gc_free_repr_data */
compose,
NULL, /* spesh */
"ConcBlockingQueue", /* name */
MVM_REPR_ID_ConcBlockingQueue,
NULL, /* unmanaged_size */
NULL, /* describe_refs */
};
MVMObject * MVM_concblockingqueue_jit_poll(MVMThreadContext *tc, MVMObject *queue) {
if (REPR(queue)->ID == MVM_REPR_ID_ConcBlockingQueue && IS_CONCRETE(queue))
return MVM_concblockingqueue_poll(tc, (MVMConcBlockingQueue *)queue);
else
MVM_exception_throw_adhoc(tc,
"queuepoll requires a concrete object with REPR ConcBlockingQueue");
}
/* Polls a queue for a value, returning NULL if none is available. */
MVMObject * MVM_concblockingqueue_poll(MVMThreadContext *tc, MVMConcBlockingQueue *queue) {
MVMConcBlockingQueue *cbq = (MVMConcBlockingQueue *)queue;
MVMConcBlockingQueueBody *body = cbq->body;
MVMConcBlockingQueueNode *taken;
MVMObject *result = tc->instance->VMNull;
unsigned int interval_id;
interval_id = MVM_telemetry_interval_start(tc, "ConcBlockingQueue.poll");
MVMROOT(tc, cbq, {
MVM_gc_mark_thread_blocked(tc);
uv_mutex_lock(&body->head_lock);
MVM_gc_mark_thread_unblocked(tc);
});
if (MVM_load(&body->elems) > 0) {
taken = body->head->next;
MVM_free(body->head);
body->head = taken;
MVM_barrier();
result = taken->value;
taken->value = NULL;
MVM_barrier();
if (MVM_decr(&body->elems) > 1)
uv_cond_signal(&body->head_cond);
}
uv_mutex_unlock(&body->head_lock);
MVM_telemetry_interval_stop(tc, interval_id, "ConcBlockingQueue.poll");
return result;
}