Skip to content

Commit

Permalink
[IO] Stop event loop thread
Browse files Browse the repository at this point in the history
Stop loop with uv_stop(uv_loop) and join the thread
  • Loading branch information
bdw committed Sep 11, 2018
1 parent 33c71bc commit 2dd82ec
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
34 changes: 32 additions & 2 deletions src/io/eventloop.c
Expand Up @@ -92,9 +92,8 @@ static void enter_loop(MVMThreadContext *tc, MVMCallsite *callsite, MVMRegister
/* Signal that the event loop is ready for processing. */
uv_sem_post(&(tc->instance->sem_event_loop_started));

/* Enter event loop; should never leave it. */
/* Enter event loop */
uv_run(tc->loop, UV_RUN_DEFAULT);
MVM_panic(1, "Supposedly unending event loop thread ended");
}

/* Sees if we have an event loop processing thread set up already, and
Expand Down Expand Up @@ -263,3 +262,34 @@ void MVM_io_eventloop_remove_active_work(MVMThreadContext *tc, int *work_idx_to_
MVM_panic(1, "cannot remove invalid eventloop work item index %d", work_idx);
}
}


/* Stop active event loop if present */
void MVM_io_eventloop_stop(MVMThreadContext *tc) {
MVMInstance *instance = tc->instance;
MVMThreadContext *event_loop_thread = instance->event_loop_thread;
if (!event_loop_thread)
return;

MVM_gc_mark_thread_blocked(tc);
uv_mutex_lock(&instance->mutex_event_loop_start);
MVM_gc_mark_thread_unblocked(tc);

event_loop_thread = instance->event_loop_thread;
if (event_loop_thread) {
/* Stop the loop */
uv_loop_t *loop = event_loop_thread->loop;
uv_stop(loop);

MVM_gc_mark_thread_blocked(tc);
MVM_thread_join(tc, (MVMObject*)event_loop_thread->thread_obj);
MVM_gc_mark_thread_unblocked(tc);

/* release allocated resources, this may be */
uv_close((uv_handle_t*)instance->event_loop_wakeup, NULL);
MVM_free(instance->event_loop_wakeup);
instance->event_loop_wakeup = NULL;
}
instance->event_loop_thread = NULL;
uv_mutex_unlock(&instance->mutex_event_loop_start);
}
1 change: 1 addition & 0 deletions src/io/eventloop.h
Expand Up @@ -28,3 +28,4 @@ void MVM_io_eventloop_send_cancellation_notification(MVMThreadContext *tc, MVMAs
int MVM_io_eventloop_add_active_work(MVMThreadContext *tc, MVMObject *async_task);
MVMAsyncTask * MVM_io_eventloop_get_active_work(MVMThreadContext *tc, int work_idx);
void MVM_io_eventloop_remove_active_work(MVMThreadContext *tc, int *work_idx_to_clear);
void MVM_io_eventloop_stop(MVMThreadContext *tc);
4 changes: 3 additions & 1 deletion src/moar.c
Expand Up @@ -500,8 +500,10 @@ static void cleanup_callsite_interns(MVMInstance *instance) {
* should clear up all resources and free all memory; in practice, it falls
* short of this goal at the moment. */
void MVM_vm_destroy_instance(MVMInstance *instance) {
/* Stop spesh worker */
/* Stop system threads */
MVM_spesh_worker_teardown(instance->main_thread);
MVM_io_eventloop_stop(instance->main_thread);

/* Join any foreground threads and flush standard handles. */
MVM_thread_join_foreground(instance->main_thread);
MVM_io_flush_standard_handles(instance->main_thread);
Expand Down

0 comments on commit 2dd82ec

Please sign in to comment.