Skip to content

Commit

Permalink
Accessors for root_task, current_task, 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 accessor function `jl_get_current_task` and `jl_get_root_task` avoid
this.

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

No alternatives exist for `jl_get_safe_restore` and
`jl_set_safe_restore`.
  • Loading branch information
fingolfin committed May 29, 2020
1 parent 38a373a commit 493949d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,12 @@ 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_value_t *jl_get_root_task(void);

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

// codegen interface ----------------------------------------------------------

typedef struct {
Expand Down
1 change: 0 additions & 1 deletion src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,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
22 changes: 17 additions & 5 deletions src/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -609,16 +609,28 @@ JL_DLLEXPORT jl_value_t *jl_get_current_task(void)
return (jl_value_t*)ptls->current_task;
}

#ifdef JL_HAVE_ASYNCIFY
JL_DLLEXPORT jl_ucontext_t *task_ctx_ptr(jl_task_t *t)
JL_DLLEXPORT jl_value_t *jl_get_root_task(void)
{
return &t->ctx;
jl_ptls_t ptls = jl_get_ptls_states();
return (jl_value_t*)ptls->root_task;
}

JL_DLLEXPORT jl_value_t *jl_get_root_task(void)
JL_DLLEXPORT jl_jmp_buf *jl_get_safe_restore(void)
{
jl_ptls_t ptls = jl_get_ptls_states();
return (jl_value_t*)ptls->root_task;
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)
{
return &t->ctx;
}

void JL_DLLEXPORT jl_task_wait()
Expand Down

0 comments on commit 493949d

Please sign in to comment.