Example code displaying how to use and implement the mbed RTOS along with a simple state machine used to capture button presses.
Fork of mbed-rtos by
rtos/Mutex.cpp@6:350b53afb889, 2012-11-23 (annotated)
- Committer:
- emilmont
- Date:
- Fri Nov 23 09:57:31 2012 +0000
- Revision:
- 6:350b53afb889
- Child:
- 8:88a1a9c26ae3
Merge RTOS C++ API and RTX under the same library; Update RTX to version 4.60; Add proper Thread destructor;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 6:350b53afb889 | 1 | #include "Mutex.h" |
emilmont | 6:350b53afb889 | 2 | |
emilmont | 6:350b53afb889 | 3 | #include <string.h> |
emilmont | 6:350b53afb889 | 4 | #include "error.h" |
emilmont | 6:350b53afb889 | 5 | |
emilmont | 6:350b53afb889 | 6 | namespace rtos { |
emilmont | 6:350b53afb889 | 7 | |
emilmont | 6:350b53afb889 | 8 | Mutex::Mutex() { |
emilmont | 6:350b53afb889 | 9 | #ifdef CMSIS_OS_RTX |
emilmont | 6:350b53afb889 | 10 | memset(_mutex_data, 0, sizeof(_mutex_data)); |
emilmont | 6:350b53afb889 | 11 | _osMutexDef.mutex = _mutex_data; |
emilmont | 6:350b53afb889 | 12 | #endif |
emilmont | 6:350b53afb889 | 13 | _osMutexId = osMutexCreate(&_osMutexDef); |
emilmont | 6:350b53afb889 | 14 | if (_osMutexId == NULL) { |
emilmont | 6:350b53afb889 | 15 | error("Error initializing the mutex object\n"); |
emilmont | 6:350b53afb889 | 16 | } |
emilmont | 6:350b53afb889 | 17 | } |
emilmont | 6:350b53afb889 | 18 | |
emilmont | 6:350b53afb889 | 19 | osStatus Mutex::lock(uint32_t millisec) { |
emilmont | 6:350b53afb889 | 20 | return osMutexWait(_osMutexId, millisec); |
emilmont | 6:350b53afb889 | 21 | } |
emilmont | 6:350b53afb889 | 22 | |
emilmont | 6:350b53afb889 | 23 | bool Mutex::trylock() { |
emilmont | 6:350b53afb889 | 24 | return (osMutexWait(_osMutexId, 0) == osOK); |
emilmont | 6:350b53afb889 | 25 | } |
emilmont | 6:350b53afb889 | 26 | |
emilmont | 6:350b53afb889 | 27 | osStatus Mutex::unlock() { |
emilmont | 6:350b53afb889 | 28 | return osMutexRelease(_osMutexId); |
emilmont | 6:350b53afb889 | 29 | } |
emilmont | 6:350b53afb889 | 30 | |
emilmont | 6:350b53afb889 | 31 | Mutex::~Mutex() { |
emilmont | 6:350b53afb889 | 32 | osMutexDelete(_osMutexId); |
emilmont | 6:350b53afb889 | 33 | } |
emilmont | 6:350b53afb889 | 34 | |
emilmont | 6:350b53afb889 | 35 | } |