Mutex.cpp 591 B

12345678910111213141516171819202122232425262728293031
  1. #include "Mutex.h"
  2. #include <string.h>
  3. //#include "error.h"
  4. namespace rtos {
  5. Mutex::Mutex() {
  6. #ifdef CMSIS_OS_RTX
  7. memset(_mutex_data, 0, sizeof(_mutex_data));
  8. _osMutexDef.mutex = _mutex_data;
  9. #endif
  10. _osMutexId = osMutexCreate(&_osMutexDef);
  11. if (_osMutexId == NULL) {
  12. // error("Error initializing the mutex object\n");
  13. }
  14. }
  15. osStatus Mutex::lock(uint32_t millisec) {
  16. return osMutexWait(_osMutexId, millisec);
  17. }
  18. bool Mutex::trylock() {
  19. return (osMutexWait(_osMutexId, 0) == osOK);
  20. }
  21. osStatus Mutex::unlock() {
  22. return osMutexRelease(_osMutexId);
  23. }
  24. }