Commit 43bf393d authored by h2zero's avatar h2zero Committed by Me No Dev

Fix semaphores in IDF & std::string assert (#2728)

* Fix semaphores in IDF & std::string assert 

Fixes the problem of giving a mutex from a callback with the latest IDF. Also addresses an occasional assert that happens when the btc_task callback gives the semaphore and causes an assert due to both cores potentially writing m_owner concurrently.

* Restored m_owner position in wait() as requested

* Reapply assert fix and move setting m_owner in ::give() 

Revert previous revert commit and move setting of m_owner in ::give to before giving the semaphore to prevent race condition possibility.
parent bea7bd18
...@@ -68,8 +68,6 @@ uint32_t FreeRTOS::Semaphore::wait(std::string owner) { ...@@ -68,8 +68,6 @@ uint32_t FreeRTOS::Semaphore::wait(std::string owner) {
xSemaphoreTake(m_semaphore, portMAX_DELAY); xSemaphoreTake(m_semaphore, portMAX_DELAY);
} }
m_owner = owner;
if (m_usePthreads) { if (m_usePthreads) {
pthread_mutex_unlock(&m_pthread_mutex); pthread_mutex_unlock(&m_pthread_mutex);
} else { } else {
...@@ -77,7 +75,6 @@ uint32_t FreeRTOS::Semaphore::wait(std::string owner) { ...@@ -77,7 +75,6 @@ uint32_t FreeRTOS::Semaphore::wait(std::string owner) {
} }
log_v("<< wait: Semaphore released: %s", toString().c_str()); log_v("<< wait: Semaphore released: %s", toString().c_str());
m_owner = std::string("<N/A>");
return m_value; return m_value;
} // wait } // wait
...@@ -87,7 +84,8 @@ FreeRTOS::Semaphore::Semaphore(std::string name) { ...@@ -87,7 +84,8 @@ FreeRTOS::Semaphore::Semaphore(std::string name) {
if (m_usePthreads) { if (m_usePthreads) {
pthread_mutex_init(&m_pthread_mutex, nullptr); pthread_mutex_init(&m_pthread_mutex, nullptr);
} else { } else {
m_semaphore = xSemaphoreCreateMutex(); m_semaphore = xSemaphoreCreateBinary();
xSemaphoreGive(m_semaphore);
} }
m_name = name; m_name = name;
...@@ -111,6 +109,8 @@ FreeRTOS::Semaphore::~Semaphore() { ...@@ -111,6 +109,8 @@ FreeRTOS::Semaphore::~Semaphore() {
*/ */
void FreeRTOS::Semaphore::give() { void FreeRTOS::Semaphore::give() {
log_v("Semaphore giving: %s", toString().c_str()); log_v("Semaphore giving: %s", toString().c_str());
m_owner = std::string("<N/A>");
if (m_usePthreads) { if (m_usePthreads) {
pthread_mutex_unlock(&m_pthread_mutex); pthread_mutex_unlock(&m_pthread_mutex);
} else { } else {
...@@ -120,7 +120,6 @@ void FreeRTOS::Semaphore::give() { ...@@ -120,7 +120,6 @@ void FreeRTOS::Semaphore::give() {
// FreeRTOS::sleep(10); // FreeRTOS::sleep(10);
// #endif // #endif
m_owner = std::string("<N/A>");
} // Semaphore::give } // Semaphore::give
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment