Skip to content

Commit 7adb9ce

Browse files
authored
Add accessors for current_task and safe_restore in TLS (#36064)
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`.
1 parent 90cc95e commit 7adb9ce

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/julia.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,6 +2077,11 @@ typedef struct {
20772077
#define jl_current_task (jl_get_ptls_states()->current_task)
20782078
#define jl_root_task (jl_get_ptls_states()->root_task)
20792079

2080+
JL_DLLEXPORT jl_value_t *jl_get_current_task(void);
2081+
2082+
JL_DLLEXPORT jl_jmp_buf *jl_get_safe_restore(void);
2083+
JL_DLLEXPORT void jl_set_safe_restore(jl_jmp_buf *);
2084+
20802085
// codegen interface ----------------------------------------------------------
20812086
// The root propagation here doesn't have to be literal, but callers should
20822087
// ensure that the return value outlives the MethodInstance

src/julia_internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,6 @@ JL_DLLEXPORT jl_value_t *(jl_array_data_owner)(jl_array_t *a);
10211021
JL_DLLEXPORT int jl_array_isassigned(jl_array_t *a, size_t i);
10221022

10231023
JL_DLLEXPORT uintptr_t jl_object_id_(jl_value_t *tv, jl_value_t *v) JL_NOTSAFEPOINT;
1024-
JL_DLLEXPORT jl_value_t *jl_get_current_task(void);
10251024
JL_DLLEXPORT void jl_set_next_task(jl_task_t *task);
10261025

10271026
// -- synchronization utilities -- //

src/task.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,18 @@ JL_DLLEXPORT jl_value_t *jl_get_current_task(void)
612612
return (jl_value_t*)ptls->current_task;
613613
}
614614

615+
JL_DLLEXPORT jl_jmp_buf *jl_get_safe_restore(void)
616+
{
617+
jl_ptls_t ptls = jl_get_ptls_states();
618+
return (jl_value_t*)ptls->safe_restore;
619+
}
620+
621+
JL_DLLEXPORT void jl_set_safe_restore(jl_jmp_buf *sr)
622+
{
623+
jl_ptls_t ptls = jl_get_ptls_states();
624+
ptls->safe_restore = sr;
625+
}
626+
615627
#ifdef JL_HAVE_ASYNCIFY
616628
JL_DLLEXPORT jl_ucontext_t *task_ctx_ptr(jl_task_t *t)
617629
{

0 commit comments

Comments
 (0)