Mutex.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* Copyright (c) 2012 mbed.org */
  2. #ifndef MUTEX_H
  3. #define MUTEX_H
  4. #include <stdint.h>
  5. #include "cmsis_os.h"
  6. namespace rtos {
  7. /*! The Mutex class is used to synchronise the execution of threads.
  8. This is for example used to protect access to a shared resource.
  9. */
  10. class Mutex {
  11. public:
  12. /*! Create and Initialize a Mutex object */
  13. Mutex();
  14. /*! Wait until a Mutex becomes available.
  15. \param millisec timeout value or 0 in case of no time-out. (default: osWaitForever)
  16. \return status code that indicates the execution status of the function.
  17. */
  18. osStatus lock(uint32_t millisec=osWaitForever);
  19. /*! Try to lock the mutex, and return immediately
  20. \return true if the mutex was acquired, false otherwise.
  21. */
  22. bool trylock();
  23. /*! Unlock the mutex that has previously been locked by the same thread
  24. \return status code that indicates the execution status of the function.
  25. */
  26. osStatus unlock();
  27. private:
  28. osMutexId _osMutexId;
  29. osMutexDef_t _osMutexDef;
  30. #ifdef CMSIS_OS_RTX
  31. int32_t _mutex_data[3];
  32. #endif
  33. };
  34. }
  35. #endif