Skip to content

Commit

Permalink
Add Repr Semaphore
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuomingliang committed Feb 22, 2014
1 parent d50b471 commit 12682ab
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 1 deletion.
2 changes: 2 additions & 0 deletions build/Makefile.in
Expand Up @@ -120,6 +120,7 @@ OBJECTS = src/core/args@obj@ \
src/6model/reprs/MVMMultiCache@obj@ \
src/6model/reprs/MVMContinuation@obj@ \
src/6model/reprs/ReentrantMutex@obj@ \
src/6model/reprs/Semaphore@obj@ \
src/6model/6model@obj@ \
src/6model/bootstrap@obj@ \
src/6model/sc@obj@ \
Expand Down Expand Up @@ -210,6 +211,7 @@ HEADERS = src/moar.h \
src/6model/reprs/MVMMultiCache.h \
src/6model/reprs/MVMContinuation.h \
src/6model/reprs/ReentrantMutex.h \
src/6model/reprs/Semaphore.h \
src/6model/sc.h \
src/mast/compiler.h \
src/mast/driver.h \
Expand Down
1 change: 1 addition & 0 deletions src/6model/reprs.c
Expand Up @@ -207,6 +207,7 @@ void MVM_repr_initialize_registry(MVMThreadContext *tc) {
register_core_repr(MultiCache);
register_core_repr(Continuation);
register_core_repr(ReentrantMutex);
register_core_repr(Semaphore);

tc->instance->num_reprs = MVM_REPR_CORE_COUNT;
}
Expand Down
4 changes: 3 additions & 1 deletion src/6model/reprs.h
Expand Up @@ -28,6 +28,7 @@
#include "6model/reprs/MVMMultiCache.h"
#include "6model/reprs/MVMContinuation.h"
#include "6model/reprs/ReentrantMutex.h"
#include "6model/reprs/Semaphore.h"

/* REPR related functions. */
void MVM_repr_initialize_registry(MVMThreadContext *tc);
Expand Down Expand Up @@ -66,8 +67,9 @@ const MVMREPROps * MVM_repr_get_by_name(MVMThreadContext *tc, MVMString *name);
#define MVM_REPR_ID_MVMMultiCache 26
#define MVM_REPR_ID_MVMContinuation 27
#define MVM_REPR_ID_ReentrantMutex 28
#define MVM_REPR_ID_Semaphore 29

#define MVM_REPR_CORE_COUNT 29
#define MVM_REPR_CORE_COUNT 30
#define MVM_REPR_MAX_COUNT 64

/* Default attribute functions for a REPR that lacks them. */
Expand Down
118 changes: 118 additions & 0 deletions src/6model/reprs/Semaphore.c
@@ -0,0 +1,118 @@
#include "moar.h"

/* This representation's function pointer table. */
static const MVMREPROps 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, &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(MVMSemaphore);
});

return st->WHAT;
}

/* Creates a new instance based on the type object. */
static MVMObject * allocate(MVMThreadContext *tc, MVMSTable *st) {
return MVM_gc_allocate_object(tc, st);
}

/* 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 Semaphore");
}

/* set a new Semaphore value. */
static void set_int(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data, MVMint64 value) {
MVMSemaphoreBody *body = (MVMSemaphoreBody *)data;
int r;
if ((r = uv_sem_init(&body->sem, (MVMuint32) value)) < 0)
MVM_exception_throw_adhoc(tc, "Failed to initialize Semaphore: %s",
uv_strerror(r));
}

/* Called by the VM in order to free memory associated with this object. */
static void gc_free(MVMThreadContext *tc, MVMObject *obj) {
/* The ThreadContext has already been destroyed by the GC. */
MVMSemaphore *sem = (MVMSemaphore *)obj;
uv_sem_destroy(&sem->body.sem);
}

/* Gets the storage specification for this representation. */
static MVMStorageSpec get_storage_spec(MVMThreadContext *tc, MVMSTable *st) {
MVMStorageSpec spec;
spec.inlineable = MVM_STORAGE_SPEC_REFERENCE;
spec.boxed_primitive = MVM_STORAGE_SPEC_BP_NONE;
spec.can_box = 0;
return spec;
}

/* Compose the representation. */
static void compose(MVMThreadContext *tc, MVMSTable *st, MVMObject *info) {
/* Nothing to do for this REPR. */
}

/* Set the size of the STable. */
static void deserialize_stable_size(MVMThreadContext *tc, MVMSTable *st, MVMSerializationReader *reader) {
st->size = sizeof(MVMSemaphore);
}

/* Initializes the representation. */
const MVMREPROps * MVMSemaphore_initialize(MVMThreadContext *tc) {
return &this_repr;
}

static const MVMREPROps this_repr = {
type_object_for,
allocate,
NULL, /* initialize */
copy_to,
MVM_REPR_DEFAULT_ATTR_FUNCS,
{
set_int,
MVM_REPR_DEFAULT_GET_INT,
MVM_REPR_DEFAULT_SET_NUM,
MVM_REPR_DEFAULT_GET_NUM,
MVM_REPR_DEFAULT_SET_STR,
MVM_REPR_DEFAULT_GET_STR,
MVM_REPR_DEFAULT_GET_BOXED_REF
}, /* box_funcs */
MVM_REPR_DEFAULT_POS_FUNCS,
MVM_REPR_DEFAULT_ASS_FUNCS,
MVM_REPR_DEFAULT_ELEMS,
get_storage_spec,
NULL, /* change_type */
NULL, /* serialize */
NULL, /* deserialize */
NULL, /* serialize_repr_data */
NULL, /* deserialize_repr_data */
deserialize_stable_size,
NULL, /* gc_mark */
gc_free,
NULL, /* gc_cleanup */
NULL, /* gc_mark_repr_data */
NULL, /* gc_free_repr_data */
compose,
"Semaphore", /* name */
MVM_REPR_ID_ReentrantMutex,
0, /* Semaphore */
};

MVMint64 MVM_semaphore_tryacquire(MVMThreadContext *tc, MVMSemaphore *sem) {
int r;
while ((r = uv_sem_trywait(&sem->body.sem)) == UV_EAGAIN);
return !r;
}

void MVM_semaphore_acquire(MVMThreadContext *tc, MVMSemaphore *sem) {
uv_sem_wait(&sem->body.sem);
}

void MVM_semaphore_release(MVMThreadContext *tc, MVMSemaphore *sem) {
uv_sem_post(&sem->body.sem);
}
16 changes: 16 additions & 0 deletions src/6model/reprs/Semaphore.h
@@ -0,0 +1,16 @@
/* Representation used for VM thread handles. */
struct MVMSemaphoreBody {
uv_sem_t sem;
};
struct MVMSemaphore {
MVMObject common;
MVMSemaphoreBody body;
};

/* Function for REPR setup. */
const MVMREPROps * MVMSemaphore_initialize(MVMThreadContext *tc);

/* acquire and release functions. */
MVMint64 MVM_semaphore_tryacquire(MVMThreadContext *tc, MVMSemaphore *sem);
void MVM_semaphore_acquire(MVMThreadContext *tc, MVMSemaphore *sem);
void MVM_semaphore_release(MVMThreadContext *tc, MVMSemaphore *sem);
2 changes: 2 additions & 0 deletions src/types.h
Expand Up @@ -70,6 +70,8 @@ typedef struct MVMContinuation MVMContinuation;
typedef struct MVMContinuationBody MVMContinuationBody;
typedef struct MVMReentrantMutex MVMReentrantMutex;
typedef struct MVMReentrantMutexBody MVMReentrantMutexBody;
typedef struct MVMSemaphore MVMSemaphore;
typedef struct MVMSemaphoreBody MVMSemaphoreBody;
typedef struct MVMObject MVMObject;
typedef struct MVMObjectStooge MVMObjectStooge;
typedef struct MVMOpInfo MVMOpInfo;
Expand Down

0 comments on commit 12682ab

Please sign in to comment.