diff options
| author | bernard <[email protected]> | 2017-01-31 13:19:18 +0800 |
|---|---|---|
| committer | bernard <[email protected]> | 2017-01-31 13:19:18 +0800 |
| commit | eb2c0981974ef5d93e6315880973c4c256c89dc3 (patch) | |
| tree | 5bbb6c36337408262240ef9021d2bd12719e346d /components/cplusplus/Mutex.cpp | |
| parent | 6b183461994ca25f4ea1a8fc78c67320b3fc9481 (diff) | |
[C++] Add C++ interface for RT-Thread kernel.
Diffstat (limited to 'components/cplusplus/Mutex.cpp')
| -rw-r--r-- | components/cplusplus/Mutex.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/components/cplusplus/Mutex.cpp b/components/cplusplus/Mutex.cpp new file mode 100644 index 0000000000..c8dbbe6598 --- /dev/null +++ b/components/cplusplus/Mutex.cpp @@ -0,0 +1,35 @@ +#include "Mutex.h" + +using namespace rtthread; + +Mutex::Mutex(const char *name) +{ + rt_mutex_init(&mID, name, RT_IPC_FLAG_FIFO); +} + +bool Mutex::lock(int32_t millisec) +{ + rt_int32_t tick; + + if (millisec < 0) + tick = -1; + else + tick = rt_tick_from_millisecond(millisec); + + return rt_mutex_take(&mID, tick) == RT_EOK; +} + +bool Mutex::trylock() +{ + return lock(0); +} + +void Mutex::unlock() +{ + rt_mutex_release(&mID); +} + +Mutex::~Mutex() +{ + rt_mutex_detach(&mID); +} |
