Skip to content

Commit

Permalink
fixup! Fix race condition in main loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Sep 5, 2023
1 parent 7d22963 commit ee2a4fe
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions port/esp32/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ static EventGroupHandle_t wifi_event_group;
static const int IPV4_CONNECTED_BIT = BIT0;
static const int IPV6_CONNECTED_BIT = BIT1;

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t g_cv = PTHREAD_COND_INITIALIZER;
static struct timespec ts;
static int quit = 0;
static bool light_state = false;
Expand Down Expand Up @@ -172,7 +172,9 @@ register_resources(void)
static void
signal_event_loop(void)
{
pthread_cond_signal(&cv);
pthread_mutex_lock(&g_mutex);
pthread_cond_signal(&g_cv);
pthread_mutex_unlock(&g_mutex);
}

static void
Expand Down Expand Up @@ -571,14 +573,18 @@ server_main(void *pvParameter)

while (quit != 1) {
oc_clock_time_t next_event = oc_main_poll();
pthread_mutex_lock(&mutex);
pthread_mutex_lock(&g_mutex);
if (oc_process_nevents() > 0) {
pthread_mutex_unlock(&g_mutex);
continue;
}
if (next_event == 0) {
pthread_cond_wait(&cv, &mutex);
pthread_cond_wait(&g_cv, &g_mutex);
} else {
ts = oc_clock_time_to_timespec(next_event);
pthread_cond_timedwait(&cv, &mutex, &ts);
pthread_cond_timedwait(&g_cv, &g_mutex, &ts);
}
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&g_mutex);
}

#ifdef OC_HAS_FEATURE_PLGD_HAWKBIT
Expand All @@ -603,7 +609,7 @@ app_main(void)
gpio_reset_pin(BLINK_GPIO);
gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);

pthread_cond_init(&cv, NULL);
pthread_cond_init(&g_cv, NULL);

print_macro_info();

Expand Down

0 comments on commit ee2a4fe

Please sign in to comment.