From 8a86044fc0b5baa7e00b2081557ff6201a431e0d Mon Sep 17 00:00:00 2001 From: Max Horn Date: Fri, 24 Jul 2020 14:50:51 +0200 Subject: [PATCH] Add accessors for current_task and safe_restore in TLS 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`. --- src/julia.h | 5 +++++ src/julia_internal.h | 1 - src/task.c | 12 ++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/julia.h b/src/julia.h index afa4f36394632..1d901875e674b 100644 --- a/src/julia.h +++ b/src/julia.h @@ -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 diff --git a/src/julia_internal.h b/src/julia_internal.h index 2afc51bcbe108..930ec96252f8e 100644 --- a/src/julia_internal.h +++ b/src/julia_internal.h @@ -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 -- // diff --git a/src/task.c b/src/task.c index 9b112e0b66f25..b7ed209df7072 100644 --- a/src/task.c +++ b/src/task.c @@ -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) {