Skip to content

Commit

Permalink
Release controller lock while processing events
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rom1v committed May 26, 2018
1 parent ec02823 commit bb3a7f0
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions app/src/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,32 @@ static SDL_bool process_event(struct controller *controller, const struct contro
static int run_controller(void *data) {
struct controller *controller = data;

mutex_lock(controller->mutex);
for (;;) {
mutex_lock(controller->mutex);
while (!controller->stopped && control_event_queue_is_empty(&controller->queue)) {
cond_wait(controller->event_cond, controller->mutex);
}
if (controller->stopped) {
// stop immediately, do not process further events
mutex_unlock(controller->mutex);
break;
}
struct control_event event;
while (control_event_queue_take(&controller->queue, &event)) {
SDL_bool ok = process_event(controller, &event);
control_event_destroy(&event);
if (!ok) {
LOGD("Cannot write event to socket");
goto end;
}
#ifdef BUILD_DEBUG
bool non_empty = control_event_queue_take(&controller->queue, &event);
SDL_assert(non_empty);
#else
control_event_queue_take(&controller->queue, &event);
#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;
}

Expand Down

0 comments on commit bb3a7f0

Please sign in to comment.