Release controller lock while processing events

Once the controller took a control event, release the mutex before
processing it, so that the main thread is not blocked to push a new
event.
This commit is contained in:
Romain Vimont 2018-05-26 15:14:46 +02:00
parent ec02823045
commit bb3a7f05ac

View file

@ -54,27 +54,32 @@ static SDL_bool process_event(struct controller *controller, const struct contro
static int run_controller(void *data) { static int run_controller(void *data) {
struct controller *controller = data; struct controller *controller = data;
mutex_lock(controller->mutex);
for (;;) { for (;;) {
mutex_lock(controller->mutex);
while (!controller->stopped && control_event_queue_is_empty(&controller->queue)) { while (!controller->stopped && control_event_queue_is_empty(&controller->queue)) {
cond_wait(controller->event_cond, controller->mutex); cond_wait(controller->event_cond, controller->mutex);
} }
if (controller->stopped) { if (controller->stopped) {
// stop immediately, do not process further events // stop immediately, do not process further events
mutex_unlock(controller->mutex);
break; break;
} }
struct control_event event; struct control_event event;
while (control_event_queue_take(&controller->queue, &event)) { #ifdef BUILD_DEBUG
SDL_bool ok = process_event(controller, &event); bool non_empty = control_event_queue_take(&controller->queue, &event);
control_event_destroy(&event); SDL_assert(non_empty);
if (!ok) { #else
LOGD("Cannot write event to socket"); control_event_queue_take(&controller->queue, &event);
goto end; #endif
} mutex_unlock(controller->mutex);
SDL_bool ok = process_event(controller, &event);
control_event_destroy(&event);
if (!ok) {
LOGD("Cannot write event to socket");
break;
} }
} }
end:
mutex_unlock(controller->mutex);
return 0; return 0;
} }