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.

35 lines
1.0 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. vSemaphoreDelete(recursiveMutex);
  10. }
  11. void zmutex::init() {
  12. recursiveMutex = xSemaphoreCreateRecursiveMutex();
  13. }
  14. bool zmutex::isInit() {
  15. return recursiveMutex != NULL;
  16. }
  17. void zmutex::lock() {
  18. xSemaphoreTakeRecursive(recursiveMutex, portMAX_DELAY);
  19. }
  20. void zmutex::unlock() {
  21. xSemaphoreGiveRecursive(recursiveMutex);
  22. }
  23. /*******************************************************************************
  24. * zlock_guard *
  25. *******************************************************************************/
  26. zlock_guard::zlock_guard(zmutex& mutex) : m_mutex(mutex) {
  27. m_mutex.lock();
  28. }
  29. zlock_guard::~zlock_guard() { m_mutex.unlock(); }