/home/travis/build/MoarVM/MoarVM/src/6model/reprs/ConditionVariable.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "moar.h" |
2 | | |
3 | | /* This representation's function pointer table. */ |
4 | | static const MVMREPROps ConditionVariable_this_repr; |
5 | | |
6 | | /* Creates a new type object of this representation, and associates it with |
7 | | * the given HOW. */ |
8 | 1 | static MVMObject * type_object_for(MVMThreadContext *tc, MVMObject *HOW) { |
9 | 1 | MVMSTable *st = MVM_gc_allocate_stable(tc, &ConditionVariable_this_repr, HOW); |
10 | 1 | |
11 | 1 | MVMROOT(tc, st, { |
12 | 1 | MVMObject *obj = MVM_gc_allocate_type_object(tc, st); |
13 | 1 | MVM_ASSIGN_REF(tc, &(st->header), st->WHAT, obj); |
14 | 1 | st->size = sizeof(MVMConditionVariable); |
15 | 1 | }); |
16 | 1 | |
17 | 1 | return st->WHAT; |
18 | 1 | } |
19 | | |
20 | | /* Copies the body of one object to another. */ |
21 | 0 | static void copy_to(MVMThreadContext *tc, MVMSTable *st, void *src, MVMObject *dest_root, void *dest) { |
22 | 0 | MVM_exception_throw_adhoc(tc, "Cannot copy object with representation ConditionVariable"); |
23 | 0 | } |
24 | | |
25 | | /* Called by the VM to mark any GCable items. */ |
26 | 9 | static void gc_mark(MVMThreadContext *tc, MVMSTable *st, void *data, MVMGCWorklist *worklist) { |
27 | 9 | MVMConditionVariableBody *cv = (MVMConditionVariableBody *)data; |
28 | 9 | MVM_gc_worklist_add(tc, worklist, &cv->mutex); |
29 | 9 | } |
30 | | |
31 | | /* Called by the VM in order to free memory associated with this object. */ |
32 | 0 | static void gc_free(MVMThreadContext *tc, MVMObject *obj) { |
33 | 0 | MVMConditionVariable *cv = (MVMConditionVariable *)obj; |
34 | 0 | if (cv->body.condvar) { |
35 | 0 | uv_cond_destroy(cv->body.condvar); |
36 | 0 | MVM_free(cv->body.condvar); |
37 | 0 | cv->body.condvar = NULL; |
38 | 0 | } |
39 | 0 | } |
40 | | |
41 | | |
42 | | static const MVMStorageSpec storage_spec = { |
43 | | MVM_STORAGE_SPEC_REFERENCE, /* inlineable */ |
44 | | 0, /* bits */ |
45 | | 0, /* align */ |
46 | | MVM_STORAGE_SPEC_BP_NONE, /* boxed_primitive */ |
47 | | 0, /* can_box */ |
48 | | 0, /* is_unsigned */ |
49 | | }; |
50 | | |
51 | | |
52 | | /* Gets the storage specification for this representation. */ |
53 | 0 | static const MVMStorageSpec * get_storage_spec(MVMThreadContext *tc, MVMSTable *st) { |
54 | 0 | return &storage_spec; |
55 | 0 | } |
56 | | |
57 | | /* Compose the representation. */ |
58 | 1 | static void compose(MVMThreadContext *tc, MVMSTable *st, MVMObject *info) { |
59 | 1 | /* Nothing to do for this REPR. */ |
60 | 1 | } |
61 | | |
62 | | /* Set the size of the STable. */ |
63 | 0 | static void deserialize_stable_size(MVMThreadContext *tc, MVMSTable *st, MVMSerializationReader *reader) { |
64 | 0 | st->size = sizeof(MVMConditionVariable); |
65 | 0 | } |
66 | | |
67 | | /* Initializes the representation. */ |
68 | 144 | const MVMREPROps * MVMConditionVariable_initialize(MVMThreadContext *tc) { |
69 | 144 | return &ConditionVariable_this_repr; |
70 | 144 | } |
71 | | |
72 | | static const MVMREPROps ConditionVariable_this_repr = { |
73 | | type_object_for, |
74 | | MVM_gc_allocate_object, |
75 | | NULL, /* initialize */ |
76 | | copy_to, |
77 | | MVM_REPR_DEFAULT_ATTR_FUNCS, |
78 | | MVM_REPR_DEFAULT_BOX_FUNCS, |
79 | | MVM_REPR_DEFAULT_POS_FUNCS, |
80 | | MVM_REPR_DEFAULT_ASS_FUNCS, |
81 | | MVM_REPR_DEFAULT_ELEMS, |
82 | | get_storage_spec, |
83 | | NULL, /* change_type */ |
84 | | NULL, /* serialize */ |
85 | | NULL, /* deserialize */ |
86 | | NULL, /* serialize_repr_data */ |
87 | | NULL, /* deserialize_repr_data */ |
88 | | deserialize_stable_size, |
89 | | gc_mark, |
90 | | gc_free, |
91 | | NULL, /* gc_cleanup */ |
92 | | NULL, /* gc_mark_repr_data */ |
93 | | NULL, /* gc_free_repr_data */ |
94 | | compose, |
95 | | NULL, /* spesh */ |
96 | | "ConditionVariable", /* name */ |
97 | | MVM_REPR_ID_ConditionVariable, |
98 | | NULL, /* unmanaged_size */ |
99 | | NULL, /* describe_refs */ |
100 | | }; |
101 | | |
102 | | /* Given a reentrant mutex, produces an associated condition variable. */ |
103 | 3 | MVMObject * MVM_conditionvariable_from_lock(MVMThreadContext *tc, MVMReentrantMutex *lock, MVMObject *type) { |
104 | 3 | MVMConditionVariable *cv; |
105 | 3 | int init_stat; |
106 | 3 | |
107 | 3 | if (REPR(type)->ID != MVM_REPR_ID_ConditionVariable) |
108 | 0 | MVM_exception_throw_adhoc(tc, "Condition variable must have ConditionVariable REPR"); |
109 | 3 | |
110 | 3 | MVMROOT(tc, lock, { |
111 | 3 | cv = (MVMConditionVariable *)MVM_gc_allocate_object(tc, STABLE(type)); |
112 | 3 | }); |
113 | 3 | cv->body.condvar = MVM_malloc(sizeof(uv_cond_t)); |
114 | 3 | if ((init_stat = uv_cond_init(cv->body.condvar)) < 0) |
115 | 0 | MVM_exception_throw_adhoc(tc, "Failed to initialize condition variable: %s", |
116 | 0 | uv_strerror(init_stat)); |
117 | 3 | MVM_ASSIGN_REF(tc, &(cv->common.header), cv->body.mutex, (MVMObject *)lock); |
118 | 3 | |
119 | 3 | return (MVMObject *)cv; |
120 | 3 | } |
121 | | |
122 | | /* Adds the current thread to the queue of waiters on the condition variable, |
123 | | * releasing, waiting, and then re-acquiring the lock. */ |
124 | 6 | void MVM_conditionvariable_wait(MVMThreadContext *tc, MVMConditionVariable *cv) { |
125 | 6 | MVMReentrantMutex *rm = (MVMReentrantMutex *)cv->body.mutex; |
126 | 6 | AO_t orig_rec_level; |
127 | 6 | unsigned int interval_id; |
128 | 6 | |
129 | 6 | if (MVM_load(&rm->body.holder_id) != tc->thread_id) |
130 | 0 | MVM_exception_throw_adhoc(tc, |
131 | 0 | "Can only wait on a condition variable when holding mutex"); |
132 | 6 | |
133 | 6 | interval_id = MVM_telemetry_interval_start(tc, "ConditionVariable.wait"); |
134 | 6 | MVM_telemetry_interval_annotate((uintptr_t)cv->body.condvar, interval_id, "this condition variable"); |
135 | 6 | orig_rec_level = MVM_load(&rm->body.lock_count); |
136 | 6 | MVM_store(&rm->body.holder_id, 0); |
137 | 6 | MVM_store(&rm->body.lock_count, 0); |
138 | 6 | |
139 | 6 | MVMROOT2(tc, cv, rm, { |
140 | 6 | MVM_gc_mark_thread_blocked(tc); |
141 | 6 | uv_cond_wait(cv->body.condvar, rm->body.mutex); |
142 | 6 | MVM_gc_mark_thread_unblocked(tc); |
143 | 6 | }); |
144 | 6 | |
145 | 6 | MVM_store(&rm->body.holder_id, tc->thread_id); |
146 | 6 | MVM_store(&rm->body.lock_count, orig_rec_level); |
147 | 6 | MVM_telemetry_interval_stop(tc, interval_id, "ConditionVariable.wait"); |
148 | 6 | } |
149 | | |
150 | | /* Signals one thread waiting on the condition. */ |
151 | 3 | void MVM_conditionvariable_signal_one(MVMThreadContext *tc, MVMConditionVariable *cv) { |
152 | 3 | MVM_telemetry_timestamp(tc, "ConditionVariable.signal_one"); |
153 | 3 | uv_cond_signal(cv->body.condvar); |
154 | 3 | } |
155 | | |
156 | | /* Signals all threads waiting on the condition. */ |
157 | 2 | void MVM_conditionvariable_signal_all(MVMThreadContext *tc, MVMConditionVariable *cv) { |
158 | 2 | MVM_telemetry_timestamp(tc, "ConditionVariable.signal_all"); |
159 | 2 | uv_cond_broadcast(cv->body.condvar); |
160 | 2 | } |