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.

52 lines
1.3 KiB

1 year ago
  1. #include "mutex.hpp"
  2. #define TAG "zmutex"
  3. using namespace iflytop;
  4. /*******************************************************************************
  5. * zmutex *
  6. *******************************************************************************/
  7. zmutex::zmutex() {}
  8. zmutex::~zmutex() {
  9. #ifdef PC_IFLYTOP_ENABLE_OS
  10. vSemaphoreDelete(recursiveMutex);
  11. #endif
  12. }
  13. void zmutex::init() {
  14. #ifdef PC_IFLYTOP_ENABLE_OS
  15. recursiveMutex = xSemaphoreCreateRecursiveMutex();
  16. #endif
  17. }
  18. bool zmutex::isInit() {
  19. #ifdef PC_IFLYTOP_ENABLE_OS
  20. return recursiveMutex != NULL;
  21. #else
  22. return true;
  23. #endif
  24. }
  25. void zmutex::lock() {
  26. #ifdef PC_IFLYTOP_ENABLE_OS
  27. ZEARLY_ASSERT(recursiveMutex != NULL);
  28. xSemaphoreTakeRecursive(recursiveMutex, portMAX_DELAY);
  29. #else
  30. chip_critical_enter();
  31. #endif
  32. }
  33. void zmutex::unlock() {
  34. #ifdef PC_IFLYTOP_ENABLE_OS
  35. xSemaphoreGiveRecursive(recursiveMutex);
  36. #else
  37. chip_critical_exit();
  38. #endif
  39. }
  40. /*******************************************************************************
  41. * zlock_guard *
  42. *******************************************************************************/
  43. zlock_guard::zlock_guard(zmutex& mutex) : m_mutex(mutex) {
  44. m_mutex.lock();
  45. }
  46. zlock_guard::~zlock_guard() { m_mutex.unlock(); }