smart-fursuit-tail-esp32/embedded/src/mutex.cpp

27 lines
546 B
C++

#include "mutex.hpp"
Mutex::Mutex(){
this->_mutex = xSemaphoreCreateMutex();
}
bool Mutex::acquire(){
return xSemaphoreTake(this->_mutex, portMAX_DELAY) != pdFALSE;
}
void Mutex::release(){
xSemaphoreGive(this->_mutex);
}
bool Mutex::run(std::function<void()> f, bool blocking){
if(xSemaphoreTake(this->_mutex, (blocking ? portMAX_DELAY : 0)) != pdFALSE){
f();
this->release();
return true;
} else
return false;
}
bool Mutex::taken(){
return this->run([](){return;}, false) ? 0 : 1;
}