Skip to content

Commit 1be1deb

Browse files
authored
small fixes to libuv locks (#31454)
- use normal locking instead of nogc - remove locks before threads have started
1 parent ea0c6e9 commit 1be1deb

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

src/init.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -410,31 +410,23 @@ static void *init_stdio_handle(const char *stdio, uv_os_fd_t fd, int readable)
410410
break;
411411
case UV_NAMED_PIPE:
412412
handle = malloc(sizeof(uv_pipe_t));
413-
JL_UV_LOCK();
414413
if ((err = uv_pipe_init(jl_io_loop, (uv_pipe_t*)handle, 0))) {
415-
// JL_UV_UNLOCK() equivalent is done during unwinding
416414
jl_errorf("error initializing %s in uv_pipe_init: %s (%s %d)", stdio, uv_strerror(err), uv_err_name(err), err);
417415
}
418416
if ((err = uv_pipe_open((uv_pipe_t*)handle, fd))) {
419-
// JL_UV_UNLOCK() equivalent is done during unwinding
420417
jl_errorf("error initializing %s in uv_pipe_open: %s (%s %d)", stdio, uv_strerror(err), uv_err_name(err), err);
421418
}
422419
((uv_pipe_t*)handle)->data = NULL;
423-
JL_UV_UNLOCK();
424420
break;
425421
case UV_TCP:
426422
handle = malloc(sizeof(uv_tcp_t));
427-
JL_UV_LOCK();
428423
if ((err = uv_tcp_init(jl_io_loop, (uv_tcp_t*)handle))) {
429-
// JL_UV_UNLOCK() equivalent is done during unwinding
430424
jl_errorf("error initializing %s in uv_tcp_init: %s (%s %d)", stdio, uv_strerror(err), uv_err_name(err), err);
431425
}
432426
if ((err = uv_tcp_open((uv_tcp_t*)handle, (uv_os_sock_t)fd))) {
433-
// JL_UV_UNLOCK() equivalent is done during unwinding
434427
jl_errorf("error initializing %s in uv_tcp_open: %s (%s %d)", stdio, uv_strerror(err), uv_err_name(err), err);
435428
}
436429
((uv_tcp_t*)handle)->data = NULL;
437-
JL_UV_UNLOCK();
438430
break;
439431
}
440432
return handle;
@@ -673,13 +665,6 @@ void _julia_init(JL_IMAGE_SEARCH rel)
673665
jl_init_uv();
674666
restore_signals();
675667

676-
jl_resolve_sysimg_location(rel);
677-
// loads sysimg if available, and conditionally sets jl_options.cpu_target
678-
if (jl_options.image_file)
679-
jl_preload_sysimg_so(jl_options.image_file);
680-
if (jl_options.cpu_target == NULL)
681-
jl_options.cpu_target = "native";
682-
683668
jl_page_size = jl_getpagesize();
684669
uint64_t total_mem = uv_get_total_memory();
685670
if (total_mem >= (size_t)-1) {
@@ -752,6 +737,13 @@ void _julia_init(JL_IMAGE_SEARCH rel)
752737

753738
jl_init_threading();
754739

740+
jl_resolve_sysimg_location(rel);
741+
// loads sysimg if available, and conditionally sets jl_options.cpu_target
742+
if (jl_options.image_file)
743+
jl_preload_sysimg_so(jl_options.image_file);
744+
if (jl_options.cpu_target == NULL)
745+
jl_options.cpu_target = "native";
746+
755747
jl_gc_init();
756748
jl_gc_enable(0);
757749
jl_init_types();

src/jloptions.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,13 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
296296
break;
297297
case 'v': // version
298298
jl_printf(JL_STDOUT, "julia version %s\n", JULIA_VERSION_STRING);
299-
jl_exit(0);
299+
exit(0);
300300
case 'h': // help
301301
jl_printf(JL_STDOUT, "%s%s", usage, opts);
302-
jl_exit(0);
302+
exit(0);
303303
case opt_help_hidden:
304304
jl_printf(JL_STDOUT, "%s%s", usage, opts_hidden);
305-
jl_exit(0);
305+
exit(0);
306306
case 'g': // debug info
307307
if (optarg != NULL) {
308308
if (!strcmp(optarg,"0"))

src/julia_internal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ static inline void jl_assume_(int cond)
115115
static uv_loop_t *const unused_uv_loop_arg = (uv_loop_t *)0xBAD10;
116116

117117
extern jl_mutex_t jl_uv_mutex;
118-
#define JL_UV_LOCK() JL_LOCK_NOGC(&jl_uv_mutex)
119-
#define JL_UV_UNLOCK() JL_UNLOCK_NOGC(&jl_uv_mutex)
118+
#define JL_UV_LOCK() JL_LOCK(&jl_uv_mutex)
119+
#define JL_UV_UNLOCK() JL_UNLOCK(&jl_uv_mutex)
120120

121121
#ifdef __cplusplus
122122
extern "C" {

src/locks.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@ static inline int jl_mutex_trylock_nogc(jl_mutex_t *lock)
121121
return 0;
122122
}
123123

124+
static inline int jl_mutex_trylock(jl_mutex_t *lock)
125+
{
126+
int got = jl_mutex_trylock_nogc(lock);
127+
if (got) {
128+
jl_ptls_t ptls = jl_get_ptls_states();
129+
JL_SIGATOMIC_BEGIN();
130+
jl_lock_frame_push(lock);
131+
jl_gc_enable_finalizers(ptls, 0);
132+
}
133+
return got;
134+
}
135+
124136
/* Call this function for code that could be called from either a managed
125137
or an unmanaged thread */
126138
static inline void jl_mutex_lock_maybe_nogc(jl_mutex_t *lock)

0 commit comments

Comments
 (0)