Skip to content

Commit

Permalink
Rename mutex for more clarity over what it covers
Browse files Browse the repository at this point in the history
  • Loading branch information
jnthn committed Mar 30, 2018
1 parent 9a8d2fc commit 47a5203
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/6model/reprs/SCRef.c
Expand Up @@ -90,10 +90,10 @@ static void gc_free(MVMThreadContext *tc, MVMObject *obj) {
return;

/* Remove from weakref lookup hash (which doesn't count as a root). */
uv_mutex_lock(&tc->instance->mutex_sc_weakhash);
uv_mutex_lock(&tc->instance->mutex_sc_registry);
HASH_DELETE(hash_handle, tc->instance->sc_weakhash, sc->body);
tc->instance->all_scs[sc->body->sc_idx] = NULL;
uv_mutex_unlock(&tc->instance->mutex_sc_weakhash);
uv_mutex_unlock(&tc->instance->mutex_sc_registry);

/* Free manually managed object and STable root list memory. */
MVM_free(sc->body->root_objects);
Expand Down
10 changes: 5 additions & 5 deletions src/6model/sc.c
Expand Up @@ -12,14 +12,14 @@ MVMObject * MVM_sc_create(MVMThreadContext *tc, MVMString *handle) {
sc = (MVMSerializationContext *)REPR(tc->instance->SCRef)->allocate(tc, STABLE(tc->instance->SCRef));
MVMROOT(tc, sc, {
/* Add to weak lookup hash. */
uv_mutex_lock(&tc->instance->mutex_sc_weakhash);
uv_mutex_lock(&tc->instance->mutex_sc_registry);
MVM_HASH_GET(tc, tc->instance->sc_weakhash, handle, scb);
if (!scb) {
sc->body = scb = MVM_calloc(1, sizeof(MVMSerializationContextBody));
MVM_ASSIGN_REF(tc, &(sc->common.header), scb->handle, handle);
MVM_HASH_BIND(tc, tc->instance->sc_weakhash, handle, scb);
/* Calling repr_init will allocate, BUT if it does so, and we
* get unlucky, the GC will try to acquire mutex_sc_weakhash.
* get unlucky, the GC will try to acquire mutex_sc_registry.
* This deadlocks. Thus, we force allocation in gen2, which
* can never trigger GC. Note that releasing the mutex early
* is not a good way to fix this, as it leaves a race to
Expand All @@ -43,7 +43,7 @@ MVMObject * MVM_sc_create(MVMThreadContext *tc, MVMString *handle) {
MVM_repr_init(tc, (MVMObject *)sc);
MVM_gc_allocate_gen2_default_clear(tc);
}
uv_mutex_unlock(&tc->instance->mutex_sc_weakhash);
uv_mutex_unlock(&tc->instance->mutex_sc_registry);
});
});

Expand Down Expand Up @@ -319,9 +319,9 @@ MVMObject * MVM_sc_get_code(MVMThreadContext *tc, MVMSerializationContext *sc, M
/* Resolves an SC handle using the SC weakhash. */
MVMSerializationContext * MVM_sc_find_by_handle(MVMThreadContext *tc, MVMString *handle) {
MVMSerializationContextBody *scb;
uv_mutex_lock(&tc->instance->mutex_sc_weakhash);
uv_mutex_lock(&tc->instance->mutex_sc_registry);
MVM_HASH_GET(tc, tc->instance->sc_weakhash, handle, scb);
uv_mutex_unlock(&tc->instance->mutex_sc_weakhash);
uv_mutex_unlock(&tc->instance->mutex_sc_registry);
return scb && scb->sc ? scb->sc : NULL;
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/bytecode.c
Expand Up @@ -282,7 +282,7 @@ static void deserialize_sc_deps(MVMThreadContext *tc, MVMCompUnit *cu, ReaderSta
handle = MVM_cu_string(tc, cu, sh_idx);

/* See if we can resolve it. */
uv_mutex_lock(&tc->instance->mutex_sc_weakhash);
uv_mutex_lock(&tc->instance->mutex_sc_registry);
MVM_HASH_GET(tc, tc->instance->sc_weakhash, handle, scb);
if (scb && scb->sc) {
cu_body->scs_to_resolve[i] = NULL;
Expand All @@ -299,7 +299,7 @@ static void deserialize_sc_deps(MVMThreadContext *tc, MVMCompUnit *cu, ReaderSta
cu_body->scs_to_resolve[i] = scb;
cu_body->scs[i] = NULL;
}
uv_mutex_unlock(&tc->instance->mutex_sc_weakhash);
uv_mutex_unlock(&tc->instance->mutex_sc_registry);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/core/instance.h
Expand Up @@ -229,12 +229,13 @@ struct MVMInstance {
* the item is unresolved. Also, array of all SCs, used for the
* index stored in object headers. When an SC goes away this is
* simply nulled. That makes it a small memory leak if a lot of
* SCs are created and go away over time. */
* SCs are created and go away over time. The mutex protects all
* the weakhash and all SCs list. */
MVMSerializationContextBody *sc_weakhash;
uv_mutex_t mutex_sc_weakhash;
MVMSerializationContextBody **all_scs;
MVMuint32 all_scs_next_idx;
MVMuint32 all_scs_alloc;
uv_mutex_t mutex_sc_registry;

/* Mutex to serialize additions of type parameterizations. Global rather
* than per STable, as this doesn't happen often. */
Expand Down
6 changes: 3 additions & 3 deletions src/moar.c
Expand Up @@ -127,8 +127,8 @@ MVMInstance * MVM_vm_create_instance(void) {
/* Set up extension op registry mutex. */
init_mutex(instance->mutex_extop_registry, "extension op registry");

/* Set up weak reference hash mutex. */
init_mutex(instance->mutex_sc_weakhash, "sc weakhash");
/* Set up SC registry mutex. */
init_mutex(instance->mutex_sc_registry, "sc registry");

/* Set up loaded compunits hash mutex. */
init_mutex(instance->mutex_loaded_compunits, "loaded compunits");
Expand Down Expand Up @@ -528,7 +528,7 @@ void MVM_vm_destroy_instance(MVMInstance *instance) {
MVM_HASH_DESTROY(hash_handle, MVMExtOpRegistry, instance->extop_registry);

/* Clean up Hash of all known serialization contexts, along with list. */
uv_mutex_destroy(&instance->mutex_sc_weakhash);
uv_mutex_destroy(&instance->mutex_sc_registry);
MVM_HASH_DESTROY(hash_handle, MVMSerializationContextBody, instance->sc_weakhash);
MVM_free(instance->all_scs);

Expand Down

0 comments on commit 47a5203

Please sign in to comment.