From 7bbb509d0d0e00b998c0b253574c39da9e0ff56b Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Fri, 13 Nov 2015 08:50:45 +0000 Subject: [PATCH] Fix race condition when calling meshlink_stop() immediately after meshlink_start(). The event loop didn't react to event_loop_stop() if it wasn't started yet (because that is done in mesh->thread). The solution is to add a event_loop_start() function that is to be called by meshlink_start(). --- src/event.c | 6 ++++-- src/event.h | 1 + src/meshlink.c | 2 ++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/event.c b/src/event.c index 5a131b64..1f6ea186 100644 --- a/src/event.c +++ b/src/event.c @@ -183,8 +183,6 @@ void idle_set(event_loop_t *loop, idle_cb_t cb, void *data) { } bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) { - loop->running = true; - fd_set readable; fd_set writable; @@ -266,6 +264,10 @@ void event_flush_output(event_loop_t *loop) { io->cb(loop, io->data, IO_WRITE); } +void event_loop_start(event_loop_t *loop) { + loop->running = true; +} + void event_loop_stop(event_loop_t *loop) { loop->running = false; } diff --git a/src/event.h b/src/event.h index 0b664e17..02d19583 100644 --- a/src/event.h +++ b/src/event.h @@ -95,6 +95,7 @@ extern void event_loop_init(event_loop_t *loop); extern void event_loop_exit(event_loop_t *loop); extern bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex); extern void event_loop_flush_output(event_loop_t *loop); +extern void event_loop_start(event_loop_t *loop); extern void event_loop_stop(event_loop_t *loop); #endif diff --git a/src/meshlink.c b/src/meshlink.c index 1c763d6a..b3b18074 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -965,6 +965,8 @@ bool meshlink_start(meshlink_handle_t *mesh) { // Start the main thread + event_loop_start(&mesh->loop); + if(pthread_create(&mesh->thread, NULL, meshlink_main_loop, mesh) != 0) { logger(mesh, MESHLINK_DEBUG, "Could not start thread: %s\n", strerror(errno)); memset(&mesh->thread, 0, sizeof mesh->thread); -- 2.39.5