From 690dc9948bfbf140d45edb6ead7550024df049ad Mon Sep 17 00:00:00 2001 From: Guillaume Munch-Maccagnoni Date: Thu, 11 Aug 2022 01:55:09 +0200 Subject: Assert Caml_state != NULL --- otherlibs/systhreads/st_stubs.c | 2 +- runtime/caml/domain_state.h | 10 +++++++--- runtime/misc.c | 5 +++-- testsuite/tests/c-api/test_c_thread_has_lock_cstubs.c | 4 ++-- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/otherlibs/systhreads/st_stubs.c b/otherlibs/systhreads/st_stubs.c index 4eb6e616ab..3873f511e9 100644 --- a/otherlibs/systhreads/st_stubs.c +++ b/otherlibs/systhreads/st_stubs.c @@ -622,7 +622,7 @@ CAMLexport int caml_c_thread_register(void) /* Already registered? */ if (st_tls_get(caml_thread_key) != NULL) return 0; - CAMLassert(Caml_state == NULL); + CAMLassert(Caml_state_opt == NULL); caml_init_domain_self(Dom_c_threads); /* Take master lock to protect access to the runtime */ diff --git a/runtime/caml/domain_state.h b/runtime/caml/domain_state.h index 4e87008b85..acfe18d0cd 100644 --- a/runtime/caml/domain_state.h +++ b/runtime/caml/domain_state.h @@ -55,17 +55,21 @@ CAML_STATIC_ASSERT( CAMLextern pthread_key_t caml_domain_state_key; CAMLextern void caml_init_domain_state_key(void); #define CAML_INIT_DOMAIN_STATE caml_init_domain_state_key() - #define Caml_state \ + #define Caml_state_opt \ ((caml_domain_state*) pthread_getspecific(caml_domain_state_key)) #define SET_Caml_state(x) \ (pthread_setspecific(caml_domain_state_key, x)) #else CAMLextern __thread caml_domain_state* caml_state; - #define Caml_state caml_state + #define Caml_state_opt caml_state #define CAML_INIT_DOMAIN_STATE - #define SET_Caml_state(x) (Caml_state = (x)) + #define SET_Caml_state(x) (caml_state = (x)) #endif +#define Caml_state (CAMLassert(Caml_state_opt != NULL), Caml_state_opt) + + + #define Caml_state_field(field) (Caml_state->field) #endif /* CAML_STATE_H */ diff --git a/runtime/misc.c b/runtime/misc.c index 93a3c5cac5..00e783c12e 100644 --- a/runtime/misc.c +++ b/runtime/misc.c @@ -50,7 +50,7 @@ void caml_failed_assert (char * expr, char_os * file_os, int line) { char* file = caml_stat_strdup_of_os(file_os); fprintf(stderr, "[%02d] file %s; line %d ### Assertion failed: %s\n", - Caml_state ? Caml_state->id : -1, file, line, expr); + (Caml_state_opt != NULL) ? Caml_state->id : -1, file, line, expr); fflush(stderr); caml_stat_free(file); abort(); @@ -80,7 +80,8 @@ void caml_gc_log (char *msg, ...) char fmtbuf[512]; va_list args; va_start (args, msg); - sprintf(fmtbuf, "[%02d] %s\n", Caml_state ? Caml_state->id : -1, msg); + sprintf(fmtbuf, "[%02d] %s\n", + (Caml_state_opt != NULL) ? Caml_state->id : -1, msg); vfprintf(stderr, fmtbuf, args); va_end (args); fflush(stderr); diff --git a/testsuite/tests/c-api/test_c_thread_has_lock_cstubs.c b/testsuite/tests/c-api/test_c_thread_has_lock_cstubs.c index f6795a6a1f..b598959fe4 100644 --- a/testsuite/tests/c-api/test_c_thread_has_lock_cstubs.c +++ b/testsuite/tests/c-api/test_c_thread_has_lock_cstubs.c @@ -4,14 +4,14 @@ value with_lock(value unit) { - return Val_bool(Caml_state != NULL); + return Val_bool(Caml_state_opt != NULL); } value without_lock(value unit) { int res; caml_enter_blocking_section(); - res = (Caml_state != NULL); + res = (Caml_state_opt != NULL); caml_leave_blocking_section(); return Val_bool(res); } -- cgit v1.2.1