Skip to content

Commit

Permalink
🐛 Fix race and an abandoned mutex (#180)
Browse files Browse the repository at this point in the history
NOTE: CI build fails only due to what I assume is a plumbing issue. Inspecting the logs reveals that everything compiles correctly.

* Added volatile loads and fixed an abondoned mutex

* Moved early out earlier

* Fix indent

* Fix understanding of memory model

* Fix indentation

* Fix side of memory barrier

* Fix warning
  • Loading branch information
nathan-moore authored and HotelCalifornia committed Nov 21, 2019
1 parent 7fa5c61 commit a02cefc
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/display/lv_misc/lv_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,27 @@ void lv_task_init(void)
LV_ATTRIBUTE_TASK_HANDLER void lv_task_handler(void)
{
LV_LOG_TRACE("lv_task_handler started");
bool task_run = __atomic_load_n(&lv_task_run, __ATOMIC_ACQUIRE);

if(task_run == false)
{
LV_LOG_TRACE("lv_task_handler bailed early, task run false");
return;
}

/*Avoid concurrent running of the task handler*/
static bool task_handler_mutex = false;
if(task_handler_mutex) return;
task_handler_mutex = true;

bool expected = false;
bool toSet = true;
bool alreadyTaken = __atomic_compare_exchange(&task_handler_mutex, &expected, &toSet,/* weak*/ false, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE);

if (alreadyTaken) return;

static uint32_t idle_period_start = 0;
static uint32_t handler_start = 0;
static uint32_t busy_time = 0;

if(lv_task_run == false) return;

handler_start = lv_tick_get();

Expand Down Expand Up @@ -148,7 +158,7 @@ LV_ATTRIBUTE_TASK_HANDLER void lv_task_handler(void)

}

task_handler_mutex = false; /*Release the mutex*/
__atomic_store_n(&task_handler_mutex, false, __ATOMIC_RELEASE);

LV_LOG_TRACE("lv_task_handler ready");
}
Expand Down Expand Up @@ -284,7 +294,7 @@ void lv_task_reset(lv_task_t * lv_task_p)
*/
void lv_task_enable(bool en)
{
lv_task_run = en;
__atomic_store_n(&lv_task_run, en, __ATOMIC_RELAXED);
}

/**
Expand Down

0 comments on commit a02cefc

Please sign in to comment.