Skip to content

Commit

Permalink
update atomics.h to be more fully C11/C++11 compliant (JuliaLang#42152)
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash authored and LilithHafner committed Mar 8, 2022
1 parent 46ffff1 commit 841bb99
Show file tree
Hide file tree
Showing 45 changed files with 785 additions and 577 deletions.
26 changes: 17 additions & 9 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ endif

# headers are used for dependency tracking, while public headers will be part of the dist
UV_HEADERS :=
HEADERS := $(BUILDDIR)/julia_version.h $(wildcard $(SRCDIR)/support/*.h) $(addprefix $(SRCDIR)/,julia.h julia_assert.h julia_threads.h julia_fasttls.h locks.h atomics.h julia_internal.h options.h timing.h) $(addprefix $(BUILDDIR)/, $(DTRACE_HEADERS))
PUBLIC_HEADERS := $(BUILDDIR)/julia_version.h $(wildcard $(SRCDIR)/support/*.h) $(addprefix $(SRCDIR)/,julia.h julia_assert.h julia_threads.h julia_fasttls.h locks.h atomics.h julia_gcext.h)
HEADERS := $(BUILDDIR)/julia_version.h $(wildcard $(SRCDIR)/support/*.h) $(addprefix $(SRCDIR)/,julia.h julia_assert.h julia_threads.h julia_fasttls.h julia_locks.h julia_atomics.h julia_internal.h options.h timing.h) $(addprefix $(BUILDDIR)/, $(DTRACE_HEADERS))
PUBLIC_HEADERS := $(BUILDDIR)/julia_version.h $(wildcard $(SRCDIR)/support/*.h) $(addprefix $(SRCDIR)/,julia.h julia_assert.h julia_threads.h julia_fasttls.h julia_locks.h julia_atomics.h julia_gcext.h)
ifeq ($(USE_SYSTEM_LIBUV),0)
UV_HEADERS += uv.h
UV_HEADERS += uv/*.h
Expand Down Expand Up @@ -376,15 +376,23 @@ endif

clangsa: $(build_shlibdir)/libGCCheckerPlugin.$(SHLIB_EXT)

clang-sa-%: $(SRCDIR)/%.c $(build_shlibdir)/libGCCheckerPlugin.$(SHLIB_EXT) | analyzegc-deps-check
@$(call PRINT_ANALYZE, $(build_bindir)/clang --analyze -Xanalyzer -analyzer-werror -Xanalyzer -analyzer-output=text -Xclang -load -Xclang $(build_shlibdir)/libGCCheckerPlugin.$(SHLIB_EXT) $(CLANGSA_FLAGS) $(JCPPFLAGS) $(JCFLAGS) $(DEBUGFLAGS) -Xclang -analyzer-checker=core$(COMMA)julia.GCChecker --analyzer-no-default-checks -fcolor-diagnostics -Werror -x c $<)
clang-sa-%: $(SRCDIR)/%.cpp $(build_shlibdir)/libGCCheckerPlugin.$(SHLIB_EXT) | analyzegc-deps-check
@$(call PRINT_ANALYZE, $(build_bindir)/clang --analyze -Xanalyzer -analyzer-werror -Xanalyzer -analyzer-output=text -Xclang -load -Xclang $(build_shlibdir)/libGCCheckerPlugin.$(SHLIB_EXT) $(CLANGSA_FLAGS) $(CLANGSA_CXXFLAGS) $(LLVM_CXXFLAGS) $(JCPPFLAGS) $(JCXXFLAGS) $(DEBUGFLAGS) -Xclang -analyzer-checker=core$(COMMA)julia.GCChecker --analyzer-no-default-checks -fcolor-diagnostics -Werror -x c++ $<)
clang-sagc-%: $(SRCDIR)/%.c $(build_shlibdir)/libGCCheckerPlugin.$(SHLIB_EXT) .FORCE | analyzegc-deps-check
@$(call PRINT_ANALYZE, $(build_bindir)/clang -D__clang_gcanalyzer__ --analyze -Xanalyzer -analyzer-werror -Xanalyzer -analyzer-output=text -Xclang -load -Xclang $(build_shlibdir)/libGCCheckerPlugin.$(SHLIB_EXT) $(CLANGSA_FLAGS) $(JCPPFLAGS) $(JCFLAGS) $(DEBUGFLAGS) -Xclang -analyzer-checker=core$(COMMA)julia.GCChecker --analyzer-no-default-checks -fcolor-diagnostics -Werror -x c $<)
clang-sagc-%: $(SRCDIR)/%.cpp $(build_shlibdir)/libGCCheckerPlugin.$(SHLIB_EXT) .FORCE | analyzegc-deps-check
@$(call PRINT_ANALYZE, $(build_bindir)/clang -D__clang_gcanalyzer__ --analyze -Xanalyzer -analyzer-werror -Xanalyzer -analyzer-output=text -Xclang -load -Xclang $(build_shlibdir)/libGCCheckerPlugin.$(SHLIB_EXT) $(CLANGSA_FLAGS) $(CLANGSA_CXXFLAGS) $(LLVM_CXXFLAGS) $(JCPPFLAGS) $(JCXXFLAGS) $(DEBUGFLAGS) -Xclang -analyzer-checker=core$(COMMA)julia.GCChecker --analyzer-no-default-checks -fcolor-diagnostics -Werror -x c++ $<)

# Add C files as a target of `analyzegc`
analyzegc: $(addprefix clang-sa-,$(RUNTIME_SRCS))
clang-sa-%: $(SRCDIR)/%.c .FORCE | analyzegc-deps-check
@$(call PRINT_ANALYZE, $(build_bindir)/clang --analyze -Xanalyzer -analyzer-werror -Xanalyzer -analyzer-output=text $(CLANGSA_FLAGS) $(JCPPFLAGS) $(JCFLAGS) $(DEBUGFLAGS) --analyzer-no-default-checks -fcolor-diagnostics -Werror -x c $<)
clang-sa-%: $(SRCDIR)/%.cpp .FORCE | analyzegc-deps-check
@$(call PRINT_ANALYZE, $(build_bindir)/clang --analyze -Xanalyzer -analyzer-werror -Xanalyzer -analyzer-output=text $(CLANGSA_FLAGS) $(CLANGSA_CXXFLAGS) $(LLVM_CXXFLAGS) $(JCPPFLAGS) $(JCXXFLAGS) $(DEBUGFLAGS) --analyzer-no-default-checks -fcolor-diagnostics -Werror -x c++ $<)


# Add C files as a target of `analyzesrc` and `analyzegc`
analyzesrc: $(addprefix clang-sa-,$(SRCS))
analyzegc: analyzesrc $(addprefix clang-sagc-,$(RUNTIME_SRCS))

clean-analyzegc:
rm -f $(build_shlibdir)/libGCCheckerPlugin.$(SHLIB_EXT)

.PHONY: default all debug release clean cleanall clean-* libccalltest libllvmcalltest julia_flisp.boot.inc.phony analyzegc clang-sa-*
.FORCE:
.PHONY: default all debug release clean cleanall clean-* libccalltest libllvmcalltest julia_flisp.boot.inc.phony analyzegc analyzesrc .FORCE
22 changes: 13 additions & 9 deletions src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static inline void arrayassign_safe(int hasptr, jl_value_t *parent, char *dst, c
assert(nb >= jl_datatype_size(jl_typeof(src))); // nb might move some undefined bits, but we should be okay with that
if (hasptr) {
size_t nptr = nb / sizeof(void*);
memmove_refs((void**)dst, (void**)src, nptr);
memmove_refs((void**)dst, (void* const*)src, nptr);
jl_gc_multi_wb(parent, src);
}
else {
Expand Down Expand Up @@ -588,7 +588,7 @@ JL_DLLEXPORT jl_value_t *jl_ptrarrayref(jl_array_t *a JL_PROPAGATES_ROOT, size_t
{
assert(i < jl_array_len(a));
assert(a->flags.ptrarray);
jl_value_t *elt = jl_atomic_load_relaxed(((jl_value_t**)a->data) + i);
jl_value_t *elt = jl_atomic_load_relaxed(((_Atomic(jl_value_t*)*)a->data) + i);
if (elt == NULL)
jl_throw(jl_undefref_exception);
return elt;
Expand Down Expand Up @@ -617,7 +617,7 @@ JL_DLLEXPORT jl_value_t *jl_arrayref(jl_array_t *a, size_t i)
JL_DLLEXPORT int jl_array_isassigned(jl_array_t *a, size_t i)
{
if (a->flags.ptrarray) {
return jl_atomic_load_relaxed(((jl_value_t**)jl_array_data(a)) + i) != NULL;
return jl_atomic_load_relaxed(((_Atomic(jl_value_t*)*)jl_array_data(a)) + i) != NULL;
}
else if (a->flags.hasptr) {
jl_datatype_t *eltype = (jl_datatype_t*)jl_tparam0(jl_typeof(a));
Expand Down Expand Up @@ -656,7 +656,7 @@ JL_DLLEXPORT void jl_arrayset(jl_array_t *a JL_ROOTING_ARGUMENT, jl_value_t *rhs
arrayassign_safe(hasptr, jl_array_owner(a), &((char*)a->data)[i * a->elsize], rhs, a->elsize);
}
else {
jl_atomic_store_relaxed(((jl_value_t**)a->data) + i, rhs);
jl_atomic_store_relaxed(((_Atomic(jl_value_t*)*)a->data) + i, rhs);
jl_gc_wb(jl_array_owner(a), rhs);
}
}
Expand All @@ -666,7 +666,7 @@ JL_DLLEXPORT void jl_arrayunset(jl_array_t *a, size_t i)
if (i >= jl_array_len(a))
jl_bounds_error_int((jl_value_t*)a, i + 1);
if (a->flags.ptrarray)
jl_atomic_store_relaxed(((jl_value_t**)a->data) + i, NULL);
jl_atomic_store_relaxed(((_Atomic(jl_value_t*)*)a->data) + i, NULL);
else if (a->flags.hasptr) {
size_t elsize = a->elsize;
jl_assume(elsize >= sizeof(void*) && elsize % sizeof(void*) == 0);
Expand Down Expand Up @@ -1243,9 +1243,11 @@ static NOINLINE ssize_t jl_array_ptr_copy_forward(jl_value_t *owner,
void **src_p, void **dest_p,
ssize_t n) JL_NOTSAFEPOINT
{
_Atomic(void*) *src_pa = (_Atomic(void*)*)src_p;
_Atomic(void*) *dest_pa = (_Atomic(void*)*)dest_p;
for (ssize_t i = 0; i < n; i++) {
void *val = jl_atomic_load_relaxed(src_p + i);
jl_atomic_store_relaxed(dest_p + i, val);
void *val = jl_atomic_load_relaxed(src_pa + i);
jl_atomic_store_relaxed(dest_pa + i, val);
// `val` is young or old-unmarked
if (val && !(jl_astaggedvalue(val)->bits.gc & GC_MARKED)) {
jl_gc_queue_root(owner);
Expand All @@ -1259,9 +1261,11 @@ static NOINLINE ssize_t jl_array_ptr_copy_backward(jl_value_t *owner,
void **src_p, void **dest_p,
ssize_t n) JL_NOTSAFEPOINT
{
_Atomic(void*) *src_pa = (_Atomic(void*)*)src_p;
_Atomic(void*) *dest_pa = (_Atomic(void*)*)dest_p;
for (ssize_t i = 0; i < n; i++) {
void *val = jl_atomic_load_relaxed(src_p + n - i - 1);
jl_atomic_store_relaxed(dest_p + n - i - 1, val);
void *val = jl_atomic_load_relaxed(src_pa + n - i - 1);
jl_atomic_store_relaxed(dest_pa + n - i - 1, val);
// `val` is young or old-unmarked
if (val && !(jl_astaggedvalue(val)->bits.gc & GC_MARKED)) {
jl_gc_queue_root(owner);
Expand Down
2 changes: 1 addition & 1 deletion src/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ typedef struct _jl_ast_context_t {

static jl_ast_context_t jl_ast_main_ctx;

#ifdef __clang_analyzer__
#ifdef __clang_gcanalyzer__
jl_ast_context_t *jl_ast_ctx(fl_context_t *fl) JL_GLOBALLY_ROOTED JL_NOTSAFEPOINT;
#else
#define jl_ast_ctx(fl_ctx) container_of(fl_ctx, jl_ast_context_t, fl)
Expand Down
163 changes: 0 additions & 163 deletions src/atomics.h

This file was deleted.

2 changes: 1 addition & 1 deletion src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ static jl_value_t *do_apply( jl_value_t **args, uint32_t nargs, jl_value_t *iter
}
if (arg_heap) {
// optimization: keep only the first root, free the others
#ifndef __clang_analyzer__
#ifndef __clang_gcanalyzer__
((void**)roots)[-2] = (void*)JL_GC_ENCODE_PUSHARGS(1);
#endif
}
Expand Down
4 changes: 2 additions & 2 deletions src/cgmemmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ static intptr_t get_anon_hdl(void)
return -1;
}

static size_t map_offset = 0;
static _Atomic(size_t) map_offset{0};
// Multiple of 128MB.
// Hopefully no one will set a ulimit for this to be a problem...
static constexpr size_t map_size_inc_default = 128 * 1024 * 1024;
Expand Down Expand Up @@ -239,7 +239,7 @@ static intptr_t init_shared_map()
anon_hdl = get_anon_hdl();
if (anon_hdl == -1)
return -1;
map_offset = 0;
jl_atomic_store_relaxed(&map_offset, 0);
map_size = get_map_size_inc();
int ret = ftruncate(anon_hdl, map_size);
if (ret != 0) {
Expand Down
Loading

0 comments on commit 841bb99

Please sign in to comment.