Skip to content

Commit

Permalink
Don't share cached Lexotics over threads.
Browse files Browse the repository at this point in the history
Doing so leads to a race on the result slot, which can cause all kinds
of trouble.
  • Loading branch information
jnthn committed Jun 30, 2014
1 parent 772a216 commit 8730ef5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
7 changes: 0 additions & 7 deletions src/6model/reprs/MVMStaticFrame.c
Expand Up @@ -140,13 +140,6 @@ static void gc_mark(MVMThreadContext *tc, MVMSTable *st, void *data, MVMGCWorkli
MVM_gc_worklist_add(tc, worklist, &body->static_env[i].o);
}

/* Lexotics cache. */
if (body->num_lexotics) {
MVMint32 i;
for (i = 0; i < body->num_lexotics; i++)
MVM_gc_worklist_add(tc, worklist, &body->lexotics[i]);
}

/* Spesh slots. */
if (body->num_spesh_candidates) {
MVMint32 i, j;
Expand Down
4 changes: 0 additions & 4 deletions src/6model/reprs/MVMStaticFrame.h
Expand Up @@ -61,10 +61,6 @@ struct MVMStaticFrameBody {
/* The number of exception handlers this frame has. */
MVMuint32 num_handlers;

/* Lexotics cache. */
MVMint32 num_lexotics;
MVMLexotic **lexotics;

/* The compilation unit unique ID of this frame. */
MVMString *cuuid;

Expand Down
27 changes: 16 additions & 11 deletions src/core/exceptions.c
Expand Up @@ -528,24 +528,29 @@ MVMObject * MVM_exception_newlexotic(MVMThreadContext *tc, MVMuint32 offset) {
MVM_exception_throw_adhoc(tc, "Label with no handler passed to newlexotic");

/* See if we've got this lexotic cached; return it if so. */
lexotic = NULL;
for (i = 0; i < sf->body.num_lexotics; i++)
if (sf->body.lexotics[i]->body.handler_idx == handler_idx)
return (MVMObject *)sf->body.lexotics[i];
if (sf->body.pool_index < tc->lexotic_cache_size) {
lexotic = tc->lexotic_cache[sf->body.pool_index];
if (lexotic && lexotic->body.handler_idx == handler_idx)
return (MVMObject *)lexotic;
}

/* Allocate lexotic object, set it up, and cache it. */
MVMROOT(tc, sf, {
lexotic = (MVMLexotic *)MVM_repr_alloc_init(tc, tc->instance->Lexotic);
});
lexotic->body.handler_idx = handler_idx;
MVM_ASSIGN_REF(tc, &(lexotic->common.header), lexotic->body.sf, sf);
if (sf->body.num_lexotics)
sf->body.lexotics = realloc(sf->body.lexotics,
(sf->body.num_lexotics + 1) * sizeof(MVMLexotic *));
else
sf->body.lexotics = malloc(sizeof(MVMLexotic *));
MVM_ASSIGN_REF(tc, &(sf->common.header), sf->body.lexotics[sf->body.num_lexotics], lexotic);
sf->body.num_lexotics++;
if (sf->body.pool_index >= tc->lexotic_cache_size) {
MVMuint32 orig_size = tc->lexotic_cache_size;
tc->lexotic_cache_size = sf->body.pool_index + 1;
tc->lexotic_cache = orig_size
? realloc(tc->lexotic_cache, tc->lexotic_cache_size * sizeof(MVMLexotic *))
: malloc(tc->lexotic_cache_size * sizeof(MVMLexotic *));
memset(tc->lexotic_cache + orig_size, 0,
(tc->lexotic_cache_size - orig_size) * sizeof(MVMLexotic *));
}
if (!tc->lexotic_cache[sf->body.pool_index])
tc->lexotic_cache[sf->body.pool_index] = lexotic;

return (MVMObject *)lexotic;
}
Expand Down
11 changes: 9 additions & 2 deletions src/core/threadcontext.h
Expand Up @@ -161,8 +161,15 @@ struct MVMThreadContext {
/* Pool table of chains of frames for each static frame. */
MVMFrame **frame_pool_table;

/* Size of the pool table, so it can grow on demand. */
MVMuint32 frame_pool_table_size;
/* Pool of Lexotics for various static frames, held per thread since the
* result being returned is per thread. Indexes are same as used in the
* frame_pool_table above. */
MVMLexotic **lexotic_cache;

/* Size of the frame pool table and lexotic cache, so they can grow on
* demand. */
MVMuint32 frame_pool_table_size;
MVMuint32 lexotic_cache_size;

/* Serialization context write barrier disabled depth (anything non-zero
* means disabled). */
Expand Down
8 changes: 8 additions & 0 deletions src/gc/roots.c
Expand Up @@ -87,6 +87,14 @@ void MVM_gc_root_add_tc_roots_to_worklist(MVMThreadContext *tc, MVMGCWorklist *w
if (tc->interp_cu)
MVM_gc_worklist_add(tc, worklist, tc->interp_cu);

/* Lexotics cache. */
if (tc->lexotic_cache_size) {
MVMuint32 i;
for (i = 0; i < tc->lexotic_cache_size; i++)
if (tc->lexotic_cache[i])
MVM_gc_worklist_add(tc, worklist, &(tc->lexotic_cache[i]));
}

/* Current dispatcher. */
MVM_gc_worklist_add(tc, worklist, &tc->cur_dispatcher);

Expand Down

0 comments on commit 8730ef5

Please sign in to comment.