-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We can't rely on libuv ones to be reentrant, so we need to build the recursion support ourselves.
- Loading branch information
Showing
12 changed files
with
208 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#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(MVMReentrantMutex); | ||
}); | ||
|
||
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); | ||
} | ||
|
||
/* Initializes a new instance. */ | ||
static void initialize(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data) { | ||
MVMReentrantMutexBody *rm = (MVMReentrantMutexBody *)data; | ||
int init_stat; | ||
rm->mutex = malloc(sizeof(uv_mutex_t)); | ||
if ((init_stat = uv_mutex_init(rm->mutex)) < 0) | ||
MVM_exception_throw_adhoc(tc, "Failed to initialize mutex: %s", | ||
uv_strerror(init_stat)); | ||
} | ||
|
||
/* 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 ReentrantMutex"); | ||
} | ||
|
||
/* 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. */ | ||
MVMReentrantMutex *rm = (MVMReentrantMutex *)obj; | ||
uv_mutex_destroy(rm->body.mutex); | ||
free(rm->body.mutex); | ||
} | ||
|
||
/* 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(MVMReentrantMutex); | ||
} | ||
|
||
/* Initializes the representation. */ | ||
const MVMREPROps * MVMReentrantMutex_initialize(MVMThreadContext *tc) { | ||
return &this_repr; | ||
} | ||
|
||
static const MVMREPROps this_repr = { | ||
type_object_for, | ||
allocate, | ||
initialize, | ||
copy_to, | ||
MVM_REPR_DEFAULT_ATTR_FUNCS, | ||
MVM_REPR_DEFAULT_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, | ||
"ReentrantMutex", /* name */ | ||
MVM_REPR_ID_ReentrantMutex, | ||
0, /* refs_frames */ | ||
}; | ||
|
||
/* Locks the mutex. */ | ||
void MVM_reentrantmutex_lock(MVMThreadContext *tc, MVMReentrantMutex *rm) { | ||
if (MVM_load(&rm->body.holder_id) == tc->thread_id) { | ||
/* We already hold the lock; bump the count. */ | ||
MVM_incr(&rm->body.lock_count); | ||
} | ||
else { | ||
/* Not holding the lock; obtain it. */ | ||
MVMROOT(tc, rm, { | ||
MVM_gc_mark_thread_blocked(tc); | ||
uv_mutex_lock(rm->body.mutex); | ||
MVM_gc_mark_thread_unblocked(tc); | ||
}); | ||
MVM_store(&rm->body.holder_id, tc->thread_id); | ||
MVM_store(&rm->body.lock_count, 1); | ||
} | ||
} | ||
|
||
/* Unlocks the mutex. */ | ||
void MVM_reentrantmutex_unlock(MVMThreadContext *tc, MVMReentrantMutex *rm) { | ||
/* Ensure we hold the lock. */ | ||
if (MVM_load(&rm->body.holder_id) == tc->thread_id) { | ||
if (MVM_decr(&rm->body.lock_count) == 1) { | ||
/* Decremented the last recursion count; really unlock. */ | ||
MVM_store(&rm->body.holder_id, 0); | ||
uv_mutex_unlock(rm->body.mutex); | ||
} | ||
} | ||
else { | ||
MVM_exception_throw_adhoc(tc, "Attempt to unlock mutex by thread not holding it"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* Representation used for VM thread handles. */ | ||
struct MVMReentrantMutexBody { | ||
/* The (non-reentrant) mutex supplied by libuv. Sadly, we have to hold it | ||
* at a level of indirection - at least on Windows - because if the object | ||
* is moved it causes confusion. */ | ||
uv_mutex_t *mutex; | ||
|
||
/* Who currently holds the mutex, if anyone. */ | ||
AO_t holder_id; | ||
|
||
/* How many times we've taken the lock. */ | ||
AO_t lock_count; | ||
}; | ||
struct MVMReentrantMutex { | ||
MVMObject common; | ||
MVMReentrantMutexBody body; | ||
}; | ||
|
||
/* Function for REPR setup. */ | ||
const MVMREPROps * MVMReentrantMutex_initialize(MVMThreadContext *tc); | ||
|
||
/* Lock and unlock functions. */ | ||
void MVM_reentrantmutex_lock(MVMThreadContext *tc, MVMReentrantMutex *rm); | ||
void MVM_reentrantmutex_unlock(MVMThreadContext *tc, MVMReentrantMutex *rm); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -592,3 +592,5 @@ threadrun r(obj) | |
threadid w(int64) r(obj) | ||
threadyield | ||
currentthread w(obj) | ||
lock r(obj) | ||
unlock r(obj) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters