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.

28 lines
1.2 KiB

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