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.
This commit is contained in:
h2zero 2019-05-11 02:03:09 -06:00 committed by Me No Dev
parent bea7bd1852
commit 43bf393dbf

View File

@ -61,15 +61,13 @@ uint32_t FreeRTOS::getTimeSinceStart() {
*/ */
uint32_t FreeRTOS::Semaphore::wait(std::string owner) { uint32_t FreeRTOS::Semaphore::wait(std::string owner) {
log_v(">> wait: Semaphore waiting: %s for %s", toString().c_str(), owner.c_str()); log_v(">> wait: Semaphore waiting: %s for %s", toString().c_str(), owner.c_str());
if (m_usePthreads) { if (m_usePthreads) {
pthread_mutex_lock(&m_pthread_mutex); pthread_mutex_lock(&m_pthread_mutex);
} else { } else {
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) {
} }
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) {
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() {
*/ */
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() {
// FreeRTOS::sleep(10); // FreeRTOS::sleep(10);
// #endif // #endif
m_owner = std::string("<N/A>");
} // Semaphore::give } // Semaphore::give