Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame^] | 1 | #include "HAL/Semaphore.hpp" |
| 2 | |
| 3 | #include "Log.hpp" |
| 4 | |
| 5 | // set the logging level |
| 6 | TLogLevel semaphoreLogLevel = logDEBUG; |
| 7 | |
| 8 | #define SEMAPHORE_LOG(level) \ |
| 9 | if (level > semaphoreLogLevel) ; \ |
| 10 | else Log().Get(level) |
| 11 | |
| 12 | MUTEX_ID initializeMutexNormal() { return new priority_mutex; } |
| 13 | |
| 14 | void deleteMutex(MUTEX_ID sem) { delete sem; } |
| 15 | |
| 16 | /** |
| 17 | * Lock the mutex, blocking until it's available. |
| 18 | */ |
| 19 | void takeMutex(MUTEX_ID mutex) { mutex->lock(); } |
| 20 | |
| 21 | /** |
| 22 | * Attempt to lock the mutex. |
| 23 | * @return true if succeeded in locking the mutex, false otherwise. |
| 24 | */ |
| 25 | bool tryTakeMutex(MUTEX_ID mutex) { return mutex->try_lock(); } |
| 26 | |
| 27 | /** |
| 28 | * Unlock the mutex. |
| 29 | * @return 0 for success, -1 for error. If -1, the error will be in errno. |
| 30 | */ |
| 31 | void giveMutex(MUTEX_ID mutex) { mutex->unlock(); } |
| 32 | |
| 33 | MULTIWAIT_ID initializeMultiWait() { return new priority_condition_variable; } |
| 34 | |
| 35 | void deleteMultiWait(MULTIWAIT_ID cond) { delete cond; } |
| 36 | |
| 37 | void takeMultiWait(MULTIWAIT_ID cond, MUTEX_ID m) { |
| 38 | std::unique_lock<priority_mutex> lock(*m); |
| 39 | cond->wait(lock); |
| 40 | } |
| 41 | |
| 42 | void giveMultiWait(MULTIWAIT_ID cond) { cond->notify_all(); } |