#include "mutex.hpp" #define TAG "zmutex" using namespace iflytop; /******************************************************************************* * zmutex * *******************************************************************************/ zmutex::zmutex(const char* name) { this->name = name; } zmutex::~zmutex() { vSemaphoreDelete(recursiveMutex); } void zmutex::init() { recursiveMutex = xSemaphoreCreateRecursiveMutex(); ZASSERT_INFO(recursiveMutex != NULL, "zmutex (%s) init failed", name); } bool zmutex::isInit() { return recursiveMutex != NULL; } void zmutex::lock() { // ZASSERT(recursiveMutex != NULL); ZASSERT_INFO(recursiveMutex != NULL, "zmutex (%s) not init, init it", name); xSemaphoreTakeRecursive(recursiveMutex, portMAX_DELAY); } void zmutex::unlock() { xSemaphoreGiveRecursive(recursiveMutex); } /******************************************************************************* * zlock_guard * *******************************************************************************/ zlock_guard::zlock_guard(zmutex& mutex) : m_mutex(mutex) { m_mutex.lock(); } zlock_guard::~zlock_guard() { m_mutex.unlock(); }