Skip to content

Commit

Permalink
Merge pull request #18315 from JuliaLang/jn/tls-in_pure_context
Browse files Browse the repository at this point in the history
make in_pure_callback state thread-local
  • Loading branch information
vtjnash authored Sep 6, 2016
2 parents a049c69 + 324081a commit cbf0ecc
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 19 deletions.
9 changes: 5 additions & 4 deletions src/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,11 @@ static jl_lambda_info_t *jl_instantiate_staged(jl_method_t *generator, jl_tuplet
jl_svec_t *sparam_vals = env;
jl_lambda_info_t *func = generator->lambda_template;
JL_GC_PUSH4(&ex, &linenum, &sparam_vals, &func);
int last_in = in_pure_callback;
jl_ptls_t ptls = jl_get_ptls_states();
int last_in = ptls->in_pure_callback;
assert(jl_svec_len(func->sparam_syms) == jl_svec_len(sparam_vals));
JL_TRY {
in_pure_callback = 1;
ptls->in_pure_callback = 1;
ex = jl_exprn(lambda_sym, 2);

int nargs = func->nargs;
Expand Down Expand Up @@ -525,10 +526,10 @@ static jl_lambda_info_t *jl_instantiate_staged(jl_method_t *generator, jl_tuplet
for(i = 0, l = jl_array_len(stmts); i < l; i++) {
jl_array_ptr_set(stmts, i, jl_resolve_globals(jl_array_ptr_ref(stmts, i), func));
}
in_pure_callback = last_in;
ptls->in_pure_callback = last_in;
}
JL_CATCH {
in_pure_callback = last_in;
ptls->in_pure_callback = last_in;
jl_rethrow();
}
JL_GC_POP();
Expand Down
2 changes: 1 addition & 1 deletion src/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ jl_value_t *jl_parse_eval_all(const char *fname,
const char *content, size_t contentlen)
{
jl_ptls_t ptls = jl_get_ptls_states();
if (in_pure_callback)
if (ptls->in_pure_callback)
jl_error("cannot use include inside a generated function");
jl_ast_context_t *ctx = jl_ast_ctx_enter();
fl_context_t *fl_ctx = &ctx->fl;
Expand Down
2 changes: 1 addition & 1 deletion src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ jl_value_t *jl_toplevel_eval_in_warn(jl_module_t *m, jl_value_t *ex, int delay_w
jl_printf(JL_STDERR, "\n ** incremental compilation may be broken for these modules **\n\n");
}
}
if (in_pure_callback && !delay_warn)
if (ptls->in_pure_callback && !delay_warn)
jl_error("eval cannot be used in a generated function");
JL_TRY {
jl_warn_on_eval = delay_warn && (jl_warn_on_eval || m != last_m); // compute whether a warning was suppressed
Expand Down
13 changes: 6 additions & 7 deletions src/gf.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ JL_DLLEXPORT jl_value_t *jl_invoke(jl_lambda_info_t *meth, jl_value_t **args, ui

/// ----- Handling for Julia callbacks ----- ///

int in_pure_callback;

JL_DLLEXPORT int8_t jl_is_in_pure_context(void)
{
return in_pure_callback;
jl_ptls_t ptls = jl_get_ptls_states();
return ptls->in_pure_callback;
}

JL_DLLEXPORT void jl_trace_method(jl_method_t *m)
Expand Down Expand Up @@ -83,14 +82,14 @@ JL_DLLEXPORT void jl_register_linfo_tracer(void (*callback)(jl_lambda_info_t *tr
void jl_call_tracer(tracer_cb callback, jl_value_t *tracee)
{
jl_ptls_t ptls = jl_get_ptls_states();
int last_in = in_pure_callback;
int last_in = ptls->in_pure_callback;
JL_TRY {
in_pure_callback = 1;
ptls->in_pure_callback = 1;
callback(tracee);
in_pure_callback = last_in;
ptls->in_pure_callback = last_in;
}
JL_CATCH {
in_pure_callback = last_in;
ptls->in_pure_callback = last_in;
jl_printf(JL_STDERR, "WARNING: tracer callback function threw an error:\n");
jl_static_show(JL_STDERR, ptls->exception_in_transit);
jl_printf(JL_STDERR, "\n");
Expand Down
4 changes: 0 additions & 4 deletions src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ extern "C" {
// useful constants
extern jl_methtable_t *jl_type_type_mt;

// execution of certain certain unpure
// statements is prohibited from certain
// callbacks (such as generated functions)
extern int in_pure_callback;
typedef void (*tracer_cb)(jl_value_t *tracee);
void jl_call_tracer(tracer_cb callback, jl_value_t *tracee);

Expand Down
5 changes: 5 additions & 0 deletions src/julia_threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ typedef struct _jl_tls_states_t {
pthread_t system_id;
void *signal_stack;
#endif
// execution of certain certain impure
// statements is prohibited from certain
// callbacks (such as generated functions)
// as it may make compilation undecidable
int in_pure_callback;
// Counter to disable finalizer **on the current thread**
int finalizers_inhibited;
arraylist_t finalizers;
Expand Down
4 changes: 2 additions & 2 deletions src/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,8 @@ JL_DLLEXPORT jl_value_t *jl_switchto(jl_task_t *t, jl_value_t *arg)
}
if (ptls->in_finalizer)
jl_error("task switch not allowed from inside gc finalizer");
if (in_pure_callback)
jl_error("task switch not allowed from inside staged function");
if (ptls->in_pure_callback)
jl_error("task switch not allowed from inside staged nor pure functions");
sig_atomic_t defer_signal = ptls->defer_signal;
int8_t gc_state = jl_gc_unsafe_enter(ptls);
ptls->task_arg_in_transit = arg;
Expand Down

1 comment on commit cbf0ecc

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

Please sign in to comment.