Skip to content

Commit 7cbea5f

Browse files
authored
Use relaxed atomics to load/update jl_lineno and jl_filename (#58939)
This is another small change to avoid ThreadSanitizer false positives when we run it on CI.
1 parent 38aff47 commit 7cbea5f

File tree

6 files changed

+34
-34
lines changed

6 files changed

+34
-34
lines changed

src/interpreter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ static jl_value_t *eval_body(jl_array_t *stmts, interpreter_state *s, size_t ip,
702702
s->locals[n - 1] = NULL;
703703
}
704704
else if (toplevel && jl_is_linenode(stmt)) {
705-
jl_lineno = jl_linenode_line(stmt);
705+
jl_atomic_store_relaxed(&jl_lineno, jl_linenode_line(stmt));
706706
}
707707
else {
708708
eval_stmt_value(stmt, s);

src/julia_internal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,8 @@ extern _Atomic(jl_typemap_entry_t*) call_cache[N_CALL_CACHE] JL_GLOBALLY_ROOTED;
424424

425425
void free_stack(void *stkbuf, size_t bufsz) JL_NOTSAFEPOINT;
426426

427-
JL_DLLEXPORT extern int jl_lineno;
428-
JL_DLLEXPORT extern const char *jl_filename;
427+
JL_DLLEXPORT extern _Atomic(int) jl_lineno;
428+
JL_DLLEXPORT extern _Atomic(const char *) jl_filename;
429429

430430
jl_value_t *jl_gc_small_alloc_noinline(jl_ptls_t ptls, int offset,
431431
int osize);

src/method.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ JL_DLLEXPORT jl_code_info_t *jl_code_for_staged(jl_method_instance_t *mi, size_t
720720
jl_code_instance_t *ci = NULL;
721721
JL_GC_PUSH5(&ex, &func, &uninferred, &ci, &kind);
722722
jl_task_t *ct = jl_current_task;
723-
int last_lineno = jl_lineno;
723+
int last_lineno = jl_atomic_load_relaxed(&jl_lineno);
724724
int last_in = ct->ptls->in_pure_callback;
725725
size_t last_age = ct->world_age;
726726

@@ -818,12 +818,12 @@ JL_DLLEXPORT jl_code_info_t *jl_code_for_staged(jl_method_instance_t *mi, size_t
818818
}
819819

820820
ct->ptls->in_pure_callback = last_in;
821-
jl_lineno = last_lineno;
821+
jl_atomic_store_relaxed(&jl_lineno, last_lineno);
822822
ct->world_age = last_age;
823823
}
824824
JL_CATCH {
825825
ct->ptls->in_pure_callback = last_in;
826-
jl_lineno = last_lineno;
826+
jl_atomic_store_relaxed(&jl_lineno, last_lineno);
827827
jl_rethrow();
828828
}
829829
JL_GC_POP();

src/module.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,8 +1145,8 @@ JL_DLLEXPORT int jl_is_imported(jl_module_t *m, jl_sym_t *var)
11451145
return b && jl_binding_kind(bpart) == PARTITION_KIND_IMPORTED;
11461146
}
11471147

1148-
extern const char *jl_filename;
1149-
extern int jl_lineno;
1148+
extern _Atomic(const char *) jl_filename;
1149+
extern _Atomic(int) jl_lineno;
11501150

11511151
static char const dep_message_prefix[] = "_dep_message_";
11521152

@@ -1872,8 +1872,8 @@ void jl_binding_deprecation_warning(jl_binding_t *b)
18721872
jl_binding_dep_message(b);
18731873

18741874
if (jl_options.depwarn != JL_OPTIONS_DEPWARN_ERROR) {
1875-
if (jl_lineno != 0) {
1876-
jl_printf(JL_STDERR, " likely near %s:%d\n", jl_filename, jl_lineno);
1875+
if (jl_atomic_load_relaxed(&jl_lineno) != 0) {
1876+
jl_printf(JL_STDERR, " likely near %s:%d\n", jl_atomic_load_relaxed(&jl_filename), jl_atomic_load_relaxed(&jl_lineno));
18771877
}
18781878
}
18791879

src/signal-handling.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ void jl_critical_error(int sig, int si_code, bt_context_t *context, jl_task_t *c
637637
else
638638
jl_safe_printf("\n[%d] signal %d: %s\n", getpid(), sig, strsignal(sig));
639639
}
640-
jl_safe_printf("in expression starting at %s:%d\n", jl_filename, jl_lineno);
640+
jl_safe_printf("in expression starting at %s:%d\n", jl_atomic_load_relaxed(&jl_filename), jl_atomic_load_relaxed(&jl_lineno));
641641
if (context && ct) {
642642
// Must avoid extended backtrace frames here unless we're sure bt_data
643643
// is properly rooted.

src/toplevel.c

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ extern "C" {
2626
#endif
2727

2828
// current line number in a file
29-
JL_DLLEXPORT int jl_lineno = 0; // need to update jl_critical_error if this is TLS
29+
JL_DLLEXPORT _Atomic(int) jl_lineno = 0; // need to update jl_critical_error if this is TLS
3030
// current file name
31-
JL_DLLEXPORT const char *jl_filename = "none"; // need to update jl_critical_error if this is TLS
31+
JL_DLLEXPORT _Atomic(const char *) jl_filename = "none"; // need to update jl_critical_error if this is TLS
3232

3333
htable_t jl_current_modules;
3434
jl_mutex_t jl_modules_mutex;
@@ -619,8 +619,8 @@ JL_DLLEXPORT jl_value_t *jl_toplevel_eval_flex(jl_module_t *JL_NONNULL m, jl_val
619619
*toplevel_filename = jl_symbol_name((jl_sym_t*)file);
620620
}
621621
// Not thread safe. For debugging and last resort error messages (jl_critical_error) only.
622-
jl_filename = *toplevel_filename;
623-
jl_lineno = *toplevel_lineno;
622+
jl_atomic_store_relaxed(&jl_filename, *toplevel_filename);
623+
jl_atomic_store_relaxed(&jl_lineno, *toplevel_lineno);
624624
return jl_nothing;
625625
}
626626
return jl_interpret_toplevel_expr_in(m, e, NULL, NULL);
@@ -780,8 +780,8 @@ JL_DLLEXPORT jl_value_t *jl_toplevel_eval_flex(jl_module_t *JL_NONNULL m, jl_val
780780

781781
JL_DLLEXPORT jl_value_t *jl_toplevel_eval(jl_module_t *m, jl_value_t *v)
782782
{
783-
const char *filename = jl_filename;
784-
int lineno = jl_lineno;
783+
const char *filename = jl_atomic_load_relaxed(&jl_filename);
784+
int lineno = jl_atomic_load_relaxed(&jl_lineno);
785785
return jl_toplevel_eval_flex(m, v, 1, 0, &filename, &lineno);
786786
}
787787

@@ -819,23 +819,23 @@ JL_DLLEXPORT jl_value_t *jl_toplevel_eval_in(jl_module_t *m, jl_value_t *ex)
819819
{
820820
jl_check_top_level_effect(m, "eval");
821821
jl_value_t *v = NULL;
822-
int last_lineno = jl_lineno;
823-
const char *last_filename = jl_filename;
822+
int last_lineno = jl_atomic_load_relaxed(&jl_lineno);
823+
const char *last_filename = jl_atomic_load_relaxed(&jl_filename);
824824
jl_task_t *ct = jl_current_task;
825-
jl_lineno = 1;
826-
jl_filename = "none";
825+
jl_atomic_store_relaxed(&jl_lineno, 1);
826+
jl_atomic_store_relaxed(&jl_filename, "none");
827827
size_t last_age = ct->world_age;
828828
JL_TRY {
829829
ct->world_age = jl_atomic_load_acquire(&jl_world_counter);
830830
v = jl_toplevel_eval(m, ex);
831831
}
832832
JL_CATCH {
833-
jl_lineno = last_lineno;
834-
jl_filename = last_filename;
833+
jl_atomic_store_relaxed(&jl_lineno, last_lineno);
834+
jl_atomic_store_relaxed(&jl_filename, last_filename);
835835
jl_rethrow();
836836
}
837-
jl_lineno = last_lineno;
838-
jl_filename = last_filename;
837+
jl_atomic_store_relaxed(&jl_lineno, last_lineno);
838+
jl_atomic_store_relaxed(&jl_filename, last_filename);
839839
ct->world_age = last_age;
840840
assert(v);
841841
return v;
@@ -867,12 +867,12 @@ static jl_value_t *jl_parse_eval_all(jl_module_t *module, jl_value_t *text,
867867
}
868868

869869
jl_task_t *ct = jl_current_task;
870-
int last_lineno = jl_lineno;
871-
const char *last_filename = jl_filename;
870+
int last_lineno = jl_atomic_load_relaxed(&jl_lineno);
871+
const char *last_filename = jl_atomic_load_relaxed(&jl_filename);
872872
int lineno = 0;
873-
jl_lineno = 0;
873+
jl_atomic_store_relaxed(&jl_lineno, 0);
874874
const char *filename_str = jl_string_data(filename);
875-
jl_filename = filename_str;
875+
jl_atomic_store_relaxed(&jl_filename, filename_str);
876876

877877
JL_TRY {
878878
size_t last_age = ct->world_age;
@@ -882,7 +882,7 @@ static jl_value_t *jl_parse_eval_all(jl_module_t *module, jl_value_t *text,
882882
if (jl_is_linenode(expression)) {
883883
// filename is already set above.
884884
lineno = jl_linenode_line(expression);
885-
jl_lineno = lineno;
885+
jl_atomic_store_relaxed(&jl_lineno, lineno);
886886
continue;
887887
}
888888
expression = jl_svecref(jl_lower(expression, module, jl_string_data(filename), lineno, ~(size_t)0, 1), 0);
@@ -893,16 +893,16 @@ static jl_value_t *jl_parse_eval_all(jl_module_t *module, jl_value_t *text,
893893
}
894894
JL_CATCH {
895895
result = jl_box_long(lineno); // (ab)use result to root error line
896-
jl_lineno = last_lineno;
897-
jl_filename = last_filename;
896+
jl_atomic_store_relaxed(&jl_lineno, last_lineno);
897+
jl_atomic_store_relaxed(&jl_filename, last_filename);
898898
if (jl_loaderror_type == NULL)
899899
jl_rethrow();
900900
else
901901
jl_rethrow_other(jl_new_struct(jl_loaderror_type, filename, result,
902902
jl_current_exception(ct)));
903903
}
904-
jl_lineno = last_lineno;
905-
jl_filename = last_filename;
904+
jl_atomic_store_relaxed(&jl_lineno, last_lineno);
905+
jl_atomic_store_relaxed(&jl_filename, last_filename);
906906
JL_GC_POP();
907907
return result;
908908
}

0 commit comments

Comments
 (0)