Skip to content

Commit

Permalink
Add accessors for current_task and safe_restore in TLS
Browse files Browse the repository at this point in the history
In code which may be compiled against one Julia version but then gets
loaded in another (e.g. due to an update), it is problematic to directly
access members of jl_ptls_t, as this structure frequently changes
between Julia versions

The existing accessor function `jl_get_current_task` helps to avoid this,
so make it public.

Note that the public macro `jl_current_task` exist, but since macros are
compiled into the code which includes `julia.h`, they do not deal with the
situation described above.

No alternatives currently exist for `jl_get_safe_restore` and
`jl_set_safe_restore`.
  • Loading branch information
fingolfin committed Jul 24, 2020
1 parent 3dc49ca commit 8a86044
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -2078,6 +2078,11 @@ typedef struct {
#define jl_current_task (jl_get_ptls_states()->current_task)
#define jl_root_task (jl_get_ptls_states()->root_task)

JL_DLLEXPORT jl_value_t *jl_get_current_task(void);

JL_DLLEXPORT jl_jmp_buf *jl_get_safe_restore(void);
JL_DLLEXPORT void jl_set_safe_restore(jl_jmp_buf *);

// codegen interface ----------------------------------------------------------
// The root propagation here doesn't have to be literal, but callers should
// ensure that the return value outlives the MethodInstance
Expand Down
1 change: 0 additions & 1 deletion src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,6 @@ JL_DLLEXPORT jl_value_t *(jl_array_data_owner)(jl_array_t *a);
JL_DLLEXPORT int jl_array_isassigned(jl_array_t *a, size_t i);

JL_DLLEXPORT uintptr_t jl_object_id_(jl_value_t *tv, jl_value_t *v) JL_NOTSAFEPOINT;
JL_DLLEXPORT jl_value_t *jl_get_current_task(void);
JL_DLLEXPORT void jl_set_next_task(jl_task_t *task);

// -- synchronization utilities -- //
Expand Down
12 changes: 12 additions & 0 deletions src/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,18 @@ JL_DLLEXPORT jl_value_t *jl_get_current_task(void)
return (jl_value_t*)ptls->current_task;
}

JL_DLLEXPORT jl_jmp_buf *jl_get_safe_restore(void)
{
jl_ptls_t ptls = jl_get_ptls_states();
return (jl_value_t*)ptls->safe_restore;
}

JL_DLLEXPORT void jl_set_safe_restore(jl_jmp_buf *sr)
{
jl_ptls_t ptls = jl_get_ptls_states();
ptls->safe_restore = sr;
}

#ifdef JL_HAVE_ASYNCIFY
JL_DLLEXPORT jl_ucontext_t *task_ctx_ptr(jl_task_t *t)
{
Expand Down

0 comments on commit 8a86044

Please sign in to comment.