Coverage Report

Created: 2017-04-15 07:07

/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
0
static MVMObject * type_object_for(MVMThreadContext *tc, MVMObject *HOW) {
9
0
    MVMSTable *st  = MVM_gc_allocate_stable(tc, &ConditionVariable_this_repr, HOW);
10
0
11
0
    MVMROOT(tc, st, {
12
0
        MVMObject *obj = MVM_gc_allocate_type_object(tc, st);
13
0
        MVM_ASSIGN_REF(tc, &(st->header), st->WHAT, obj);
14
0
        st->size = sizeof(MVMConditionVariable);
15
0
    });
16
0
17
0
    return st->WHAT;
18
0
}
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
0
static void gc_mark(MVMThreadContext *tc, MVMSTable *st, void *data, MVMGCWorklist *worklist) {
27
0
    MVMConditionVariableBody *cv = (MVMConditionVariableBody *)data;
28
0
    MVM_gc_worklist_add(tc, worklist, &cv->mutex);
29
0
}
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
0
static void compose(MVMThreadContext *tc, MVMSTable *st, MVMObject *info) {
59
0
    /* Nothing to do for this REPR. */
60
0
}
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
130
const MVMREPROps * MVMConditionVariable_initialize(MVMThreadContext *tc) {
69
130
    return &ConditionVariable_this_repr;
70
130
}
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
0
MVMObject * MVM_conditionvariable_from_lock(MVMThreadContext *tc, MVMReentrantMutex *lock, MVMObject *type) {
104
0
    MVMConditionVariable *cv;
105
0
    int init_stat;
106
0
107
0
    if (REPR(type)->ID != MVM_REPR_ID_ConditionVariable)
108
0
        MVM_exception_throw_adhoc(tc, "Condition variable must have ConditionVariable REPR");
109
0
110
0
    MVMROOT(tc, lock, {
111
0
        cv = (MVMConditionVariable *)MVM_gc_allocate_object(tc, STABLE(type));
112
0
    });
113
0
    cv->body.condvar = MVM_malloc(sizeof(uv_cond_t));
114
0
    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
0
    MVM_ASSIGN_REF(tc, &(cv->common.header), cv->body.mutex, (MVMObject *)lock);
118
0
119
0
    return (MVMObject *)cv;
120
0
}
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
0
void MVM_conditionvariable_wait(MVMThreadContext *tc, MVMConditionVariable *cv) {
125
0
    MVMReentrantMutex *rm = (MVMReentrantMutex *)cv->body.mutex;
126
0
    AO_t orig_rec_level;
127
0
128
0
    if (MVM_load(&rm->body.holder_id) != tc->thread_id)
129
0
        MVM_exception_throw_adhoc(tc,
130
0
            "Can only wait on a condition variable when holding mutex");
131
0
    orig_rec_level = MVM_load(&rm->body.lock_count);
132
0
    MVM_store(&rm->body.holder_id, 0);
133
0
    MVM_store(&rm->body.lock_count, 0);
134
0
135
0
    MVMROOT(tc, cv, {
136
0
    MVMROOT(tc, rm, {
137
0
        MVM_gc_mark_thread_blocked(tc);
138
0
        uv_cond_wait(cv->body.condvar, rm->body.mutex);
139
0
        MVM_gc_mark_thread_unblocked(tc);
140
0
    });
141
0
    });
142
0
143
0
    MVM_store(&rm->body.holder_id, tc->thread_id);
144
0
    MVM_store(&rm->body.lock_count, orig_rec_level);
145
0
}
146
147
/* Signals one thread waiting on the condition. */
148
0
void MVM_conditionvariable_signal_one(MVMThreadContext *tc, MVMConditionVariable *cv) {
149
0
    uv_cond_signal(cv->body.condvar);
150
0
}
151
152
/* Signals all threads waiting on the condition. */
153
0
void MVM_conditionvariable_signal_all(MVMThreadContext *tc, MVMConditionVariable *cv) {
154
0
    uv_cond_broadcast(cv->body.condvar);
155
0
}