WrappedMutex.h 631 B

123456789101112131415161718192021222324252627282930313233343536
  1. #ifndef UNIXLIKE_WRAPPED_MUTEX_H
  2. #define UNIXLIKE_WRAPPED_MUTEX_H
  3. #include <pthread.h>
  4. /**
  5. * Wraps a mutex on unixlike patforms (linux, macOS, esp32 etc.).
  6. * Header only since all the methods call one function, we want them to be inlined
  7. **/
  8. class WrappedMutex
  9. {
  10. public:
  11. WrappedMutex()
  12. {
  13. pthread_mutex_init(&this->_mutex, NULL);
  14. }
  15. ~WrappedMutex()
  16. {
  17. pthread_mutex_destroy(&this->_mutex);
  18. }
  19. void lock()
  20. {
  21. pthread_mutex_lock(&this->_mutex);
  22. }
  23. void unlock()
  24. {
  25. pthread_mutex_unlock(&this->_mutex);
  26. }
  27. private:
  28. pthread_mutex_t _mutex;
  29. };
  30. #endif