From 1be1deb10c90b612a57d3541d3bf1aae1a42e104 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Mon, 25 Mar 2019 22:46:04 -0400 Subject: [PATCH] small fixes to libuv locks (#31454) - use normal locking instead of nogc - remove locks before threads have started --- src/init.c | 22 +++++++--------------- src/jloptions.c | 6 +++--- src/julia_internal.h | 4 ++-- src/locks.h | 12 ++++++++++++ 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/init.c b/src/init.c index 971732d2a92c9..e78dd4cfcd809 100644 --- a/src/init.c +++ b/src/init.c @@ -410,31 +410,23 @@ static void *init_stdio_handle(const char *stdio, uv_os_fd_t fd, int readable) break; case UV_NAMED_PIPE: handle = malloc(sizeof(uv_pipe_t)); - JL_UV_LOCK(); if ((err = uv_pipe_init(jl_io_loop, (uv_pipe_t*)handle, 0))) { - // JL_UV_UNLOCK() equivalent is done during unwinding jl_errorf("error initializing %s in uv_pipe_init: %s (%s %d)", stdio, uv_strerror(err), uv_err_name(err), err); } if ((err = uv_pipe_open((uv_pipe_t*)handle, fd))) { - // JL_UV_UNLOCK() equivalent is done during unwinding jl_errorf("error initializing %s in uv_pipe_open: %s (%s %d)", stdio, uv_strerror(err), uv_err_name(err), err); } ((uv_pipe_t*)handle)->data = NULL; - JL_UV_UNLOCK(); break; case UV_TCP: handle = malloc(sizeof(uv_tcp_t)); - JL_UV_LOCK(); if ((err = uv_tcp_init(jl_io_loop, (uv_tcp_t*)handle))) { - // JL_UV_UNLOCK() equivalent is done during unwinding jl_errorf("error initializing %s in uv_tcp_init: %s (%s %d)", stdio, uv_strerror(err), uv_err_name(err), err); } if ((err = uv_tcp_open((uv_tcp_t*)handle, (uv_os_sock_t)fd))) { - // JL_UV_UNLOCK() equivalent is done during unwinding jl_errorf("error initializing %s in uv_tcp_open: %s (%s %d)", stdio, uv_strerror(err), uv_err_name(err), err); } ((uv_tcp_t*)handle)->data = NULL; - JL_UV_UNLOCK(); break; } return handle; @@ -673,13 +665,6 @@ void _julia_init(JL_IMAGE_SEARCH rel) jl_init_uv(); restore_signals(); - jl_resolve_sysimg_location(rel); - // loads sysimg if available, and conditionally sets jl_options.cpu_target - if (jl_options.image_file) - jl_preload_sysimg_so(jl_options.image_file); - if (jl_options.cpu_target == NULL) - jl_options.cpu_target = "native"; - jl_page_size = jl_getpagesize(); uint64_t total_mem = uv_get_total_memory(); if (total_mem >= (size_t)-1) { @@ -752,6 +737,13 @@ void _julia_init(JL_IMAGE_SEARCH rel) jl_init_threading(); + jl_resolve_sysimg_location(rel); + // loads sysimg if available, and conditionally sets jl_options.cpu_target + if (jl_options.image_file) + jl_preload_sysimg_so(jl_options.image_file); + if (jl_options.cpu_target == NULL) + jl_options.cpu_target = "native"; + jl_gc_init(); jl_gc_enable(0); jl_init_types(); diff --git a/src/jloptions.c b/src/jloptions.c index 8c9ea3f63879a..4b11093433045 100644 --- a/src/jloptions.c +++ b/src/jloptions.c @@ -296,13 +296,13 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp) break; case 'v': // version jl_printf(JL_STDOUT, "julia version %s\n", JULIA_VERSION_STRING); - jl_exit(0); + exit(0); case 'h': // help jl_printf(JL_STDOUT, "%s%s", usage, opts); - jl_exit(0); + exit(0); case opt_help_hidden: jl_printf(JL_STDOUT, "%s%s", usage, opts_hidden); - jl_exit(0); + exit(0); case 'g': // debug info if (optarg != NULL) { if (!strcmp(optarg,"0")) diff --git a/src/julia_internal.h b/src/julia_internal.h index df627221ce6bc..65284d3591830 100644 --- a/src/julia_internal.h +++ b/src/julia_internal.h @@ -115,8 +115,8 @@ static inline void jl_assume_(int cond) static uv_loop_t *const unused_uv_loop_arg = (uv_loop_t *)0xBAD10; extern jl_mutex_t jl_uv_mutex; -#define JL_UV_LOCK() JL_LOCK_NOGC(&jl_uv_mutex) -#define JL_UV_UNLOCK() JL_UNLOCK_NOGC(&jl_uv_mutex) +#define JL_UV_LOCK() JL_LOCK(&jl_uv_mutex) +#define JL_UV_UNLOCK() JL_UNLOCK(&jl_uv_mutex) #ifdef __cplusplus extern "C" { diff --git a/src/locks.h b/src/locks.h index bb53887164723..a30f231634dee 100644 --- a/src/locks.h +++ b/src/locks.h @@ -121,6 +121,18 @@ static inline int jl_mutex_trylock_nogc(jl_mutex_t *lock) return 0; } +static inline int jl_mutex_trylock(jl_mutex_t *lock) +{ + int got = jl_mutex_trylock_nogc(lock); + if (got) { + jl_ptls_t ptls = jl_get_ptls_states(); + JL_SIGATOMIC_BEGIN(); + jl_lock_frame_push(lock); + jl_gc_enable_finalizers(ptls, 0); + } + return got; +} + /* Call this function for code that could be called from either a managed or an unmanaged thread */ static inline void jl_mutex_lock_maybe_nogc(jl_mutex_t *lock)