You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
1.2 KiB

12 months ago
11 months ago
12 months ago
8 months ago
12 months ago
11 months ago
8 months ago
12 months ago
  1. #include "mutex.hpp"
  2. #define TAG "zmutex"
  3. using namespace iflytop;
  4. /*******************************************************************************
  5. * zmutex *
  6. *******************************************************************************/
  7. zmutex::zmutex(const char* name) { this->name = name; }
  8. zmutex::~zmutex() { vSemaphoreDelete(recursiveMutex); }
  9. void zmutex::init() {
  10. recursiveMutex = xSemaphoreCreateRecursiveMutex();
  11. ZASSERT_INFO(recursiveMutex != NULL, "zmutex (%s) init failed", name);
  12. }
  13. bool zmutex::isInit() { return recursiveMutex != NULL; }
  14. void zmutex::lock() {
  15. // ZASSERT(recursiveMutex != NULL);
  16. ZASSERT_INFO(recursiveMutex != NULL, "zmutex (%s) not init, init it", name);
  17. xSemaphoreTakeRecursive(recursiveMutex, portMAX_DELAY);
  18. }
  19. void zmutex::unlock() { xSemaphoreGiveRecursive(recursiveMutex); }
  20. /*******************************************************************************
  21. * zlock_guard *
  22. *******************************************************************************/
  23. zlock_guard::zlock_guard(zmutex& mutex) : m_mutex(mutex) { m_mutex.lock(); }
  24. zlock_guard::~zlock_guard() { m_mutex.unlock(); }