Skip to content

Commit

Permalink
Handle gcanalyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
vchuravy committed Oct 20, 2023
1 parent d958458 commit 78a817d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
13 changes: 8 additions & 5 deletions src/jl_uv.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static void jl_uv_cb_walk_print(uv_handle_t *h, void *arg)
jl_safe_printf(" %s[%zd] %s@%p->%p\n", type, (size_t)fd, pad, (void*)h, (void*)h->data);
}

static void jl_uv_cb_wait_empty(uv_timer_t *t)
static void jl_uv_cb_wait_empty(uv_timer_t *t) JL_NOTSAFEPOINT_ENTER
{
// make sure this is hidden now, since we would auto-unref it later
uv_unref((uv_handle_t*)&signal_async);
Expand All @@ -69,7 +69,7 @@ static void jl_uv_cb_wait_empty(uv_timer_t *t)
jl_ptls_t ptls = jl_current_task->ptls;
int old_state = jl_gc_unsafe_enter(ptls);
jl_gc_collect(JL_GC_FULL);
jl_gc_unsafe_leave(ptls,old_state);
jl_gc_unsafe_leave(ptls, old_state);
}

void jl_wait_empty_begin(void)
Expand Down Expand Up @@ -154,7 +154,7 @@ JL_DLLEXPORT void jl_iolock_end(void)
}


static void jl_uv_call_hook_close(jl_value_t *val)
static void jl_uv_call_hook_close(jl_value_t *val) JL_NOTSAFEPOINT_ENTER
{
jl_ptls_t ptls = jl_current_task->ptls;
int old_state = jl_gc_unsafe_enter(ptls);
Expand All @@ -166,7 +166,7 @@ static void jl_uv_call_hook_close(jl_value_t *val)
assert(args[0]);
jl_apply(args, 2); // TODO: wrap in try-catch?
JL_GC_POP();
jl_gc_unsafe_leave(ptls,old_state);
jl_gc_unsafe_leave(ptls, old_state);
}

static void jl_uv_cb_close_handle(uv_handle_t *handle)
Expand Down Expand Up @@ -282,7 +282,10 @@ JL_DLLEXPORT void *jl_uv_handle_data(uv_handle_t *handle) { return handle->data;
JL_DLLEXPORT void *jl_uv_write_handle(uv_write_t *req) { return req->handle; }


int jl_process_events_locked(void) {
// This is JL_NOTSAFEPOINT, but the analyzer complains about uv_run.
// Callabacks need to handle their GC transitions themselves.
int jl_process_events_locked(void) // JL_NOTSAFEPOINT
{
uv_loop_t *loop = jl_io_loop;
loop->stop_flag = 0;
uv_ref((uv_handle_t*)&signal_async); // force the loop alive
Expand Down
4 changes: 2 additions & 2 deletions src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ static uv_loop_t *const unused_uv_loop_arg = (uv_loop_t *)0xBAD10;
extern jl_mutex_t jl_uv_mutex;
extern _Atomic(int) jl_uv_n_waiters;
void JL_UV_LOCK(void);
int JL_UV_TRYLOCK_NOGC(void);
void JL_UV_UNLOCK_NOGC(void);
int JL_UV_TRYLOCK_NOGC(void) JL_NOTSAFEPOINT;
void JL_UV_UNLOCK_NOGC(void) JL_NOTSAFEPOINT;
#define JL_UV_UNLOCK() JL_UNLOCK(&jl_uv_mutex)

#ifdef __cplusplus
Expand Down
16 changes: 9 additions & 7 deletions src/threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,21 +702,22 @@ void jl_init_threading(void)
gc_first_tid = nthreads;
}

int jl_process_events_locked(void);
void jl_utility_io_threadfun(void *arg) {

int jl_process_events_locked(void) JL_NOTSAFEPOINT;
void jl_utility_io_threadfun(void *arg)
{
jl_adopt_thread();
jl_ptls_t ptls = jl_current_task->ptls;
jl_gc_safe_enter(ptls);
int8_t gc_state = jl_gc_safe_enter(ptls);
while (1) {
// Only reader of the rwlock, according to libuv lock is writer-biased
if (jl_atomic_load_relaxed(&jl_uv_n_waiters) == 0)
if (JL_UV_TRYLOCK_NOGC())
{
jl_process_events_locked();
JL_UV_UNLOCK_NOGC();
}
// TODO: jl_fence(); // [^store_buffering_2]
}
jl_gc_safe_leave(ptls, gc_state);
return;
}

Expand Down Expand Up @@ -781,10 +782,11 @@ void jl_start_threads(void)
}
uv_thread_detach(&uvtid);
}
uv_barrier_wait(&thread_init_done);

// utility thread uses jl_adopt_thread
uv_thread_create(&uvtid, jl_utility_io_threadfun, NULL);
uv_thread_detach(&uvtid);

uv_barrier_wait(&thread_init_done);
}

// Profiling stubs
Expand Down

0 comments on commit 78a817d

Please sign in to comment.