Coverage Report

Created: 2017-04-15 07:07

/home/travis/build/MoarVM/MoarVM/src/6model/reprs/Semaphore.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 Semaphore_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, &Semaphore_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(MVMSemaphore);
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 Semaphore");
23
0
}
24
25
/* Set up the Semaphore with its initial value. */
26
0
static void set_int(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMint64 value) {
27
0
    MVMSemaphoreBody *body = (MVMSemaphoreBody *)data;
28
0
    int r;
29
0
    body->sem = MVM_malloc(sizeof(uv_sem_t));
30
0
    if ((r = uv_sem_init(body->sem, (MVMuint32) value)) < 0) {
31
0
        MVM_free(body->sem);
32
0
        body->sem = NULL;
33
0
        MVM_exception_throw_adhoc(tc, "Failed to initialize Semaphore: %s",
34
0
            uv_strerror(r));
35
0
    }
36
0
}
37
38
/* Called by the VM in order to free memory associated with this object. */
39
0
static void gc_free(MVMThreadContext *tc, MVMObject *obj) {
40
0
    MVMSemaphore *sem = (MVMSemaphore *)obj;
41
0
    if (sem->body.sem) {
42
0
        uv_sem_destroy(sem->body.sem);
43
0
        MVM_free(sem->body.sem);
44
0
    }
45
0
}
46
47
static const MVMStorageSpec storage_spec = {
48
    MVM_STORAGE_SPEC_REFERENCE, /* inlineable */
49
    0,                          /* bits */
50
    0,                          /* align */
51
    MVM_STORAGE_SPEC_BP_NONE,   /* boxed_primitive */
52
    0,                          /* can_box */
53
    0,                          /* is_unsigned */
54
};
55
56
57
/* Gets the storage specification for this representation. */
58
0
static const MVMStorageSpec * get_storage_spec(MVMThreadContext *tc, MVMSTable *st) {
59
0
    return &storage_spec;
60
0
}
61
62
/* Compose the representation. */
63
0
static void compose(MVMThreadContext *tc, MVMSTable *st, MVMObject *info) {
64
0
    /* Nothing to do for this REPR. */
65
0
}
66
67
/* Set the size of the STable. */
68
0
static void deserialize_stable_size(MVMThreadContext *tc, MVMSTable *st, MVMSerializationReader *reader) {
69
0
    st->size = sizeof(MVMSemaphore);
70
0
}
71
72
/* Initializes the representation. */
73
130
const MVMREPROps * MVMSemaphore_initialize(MVMThreadContext *tc) {
74
130
    return &Semaphore_this_repr;
75
130
}
76
77
static const MVMREPROps Semaphore_this_repr = {
78
    type_object_for,
79
    MVM_gc_allocate_object,
80
    NULL, /* initialize */
81
    copy_to,
82
    MVM_REPR_DEFAULT_ATTR_FUNCS,
83
    {
84
        set_int,
85
        MVM_REPR_DEFAULT_GET_INT,
86
        MVM_REPR_DEFAULT_SET_NUM,
87
        MVM_REPR_DEFAULT_GET_NUM,
88
        MVM_REPR_DEFAULT_SET_STR,
89
        MVM_REPR_DEFAULT_GET_STR,
90
        MVM_REPR_DEFAULT_SET_UINT,
91
        MVM_REPR_DEFAULT_GET_UINT,
92
        MVM_REPR_DEFAULT_GET_BOXED_REF
93
    },    /* box_funcs */
94
    MVM_REPR_DEFAULT_POS_FUNCS,
95
    MVM_REPR_DEFAULT_ASS_FUNCS,
96
    MVM_REPR_DEFAULT_ELEMS,
97
    get_storage_spec,
98
    NULL, /* change_type */
99
    NULL, /* serialize */
100
    NULL, /* deserialize */
101
    NULL, /* serialize_repr_data */
102
    NULL, /* deserialize_repr_data */
103
    deserialize_stable_size,
104
    NULL, /* gc_mark */
105
    gc_free,
106
    NULL, /* gc_cleanup */
107
    NULL, /* gc_mark_repr_data */
108
    NULL, /* gc_free_repr_data */
109
    compose,
110
    NULL, /* spesh */
111
    "Semaphore", /* name */
112
    MVM_REPR_ID_Semaphore,
113
    NULL, /* unmanaged_size */
114
    NULL, /* describe_refs */
115
};
116
117
0
MVMint64 MVM_semaphore_tryacquire(MVMThreadContext *tc, MVMSemaphore *sem) {
118
0
    int r = uv_sem_trywait(sem->body.sem);
119
0
    return !r;
120
0
}
121
122
0
void MVM_semaphore_acquire(MVMThreadContext *tc, MVMSemaphore *sem) {
123
0
    MVMROOT(tc, sem, {
124
0
        MVM_gc_mark_thread_blocked(tc);
125
0
        uv_sem_wait(sem->body.sem);
126
0
        MVM_gc_mark_thread_unblocked(tc);
127
0
    });
128
0
}
129
130
0
void MVM_semaphore_release(MVMThreadContext *tc, MVMSemaphore *sem) {
131
0
    uv_sem_post(sem->body.sem);
132
0
}