2014-05-02 17:54:01 -04:00
|
|
|
#include "HAL/Semaphore.hpp"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2014-05-02 17:54:01 -04:00
|
|
|
#include "Log.hpp"
|
2014-01-06 10:12:21 -05:00
|
|
|
|
|
|
|
|
// set the logging level
|
|
|
|
|
TLogLevel semaphoreLogLevel = logDEBUG;
|
|
|
|
|
|
|
|
|
|
#define SEMAPHORE_LOG(level) \
|
|
|
|
|
if (level > semaphoreLogLevel) ; \
|
|
|
|
|
else Log().Get(level)
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-08-19 11:12:54 -04:00
|
|
|
MUTEX_ID initializeMutexNormal() { return new std::mutex; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-08-19 11:12:54 -04:00
|
|
|
void deleteMutex(MUTEX_ID sem) { delete sem; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
2015-08-19 11:12:54 -04:00
|
|
|
* Lock the mutex, blocking until it's available.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-08-19 11:12:54 -04:00
|
|
|
void takeMutex(MUTEX_ID mutex) { mutex->lock(); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
2015-08-19 11:12:54 -04:00
|
|
|
* Attempt to lock the mutex.
|
|
|
|
|
* @return true if succeeded in locking the mutex, false otherwise.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-08-19 11:12:54 -04:00
|
|
|
bool tryTakeMutex(MUTEX_ID mutex) { return mutex->try_lock(); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
2015-08-19 11:12:54 -04:00
|
|
|
* Unlock the mutex.
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return 0 for success, -1 for error. If -1, the error will be in errno.
|
|
|
|
|
*/
|
2015-08-19 11:12:54 -04:00
|
|
|
void giveMutex(MUTEX_ID mutex) { mutex->unlock(); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-08-19 11:12:54 -04:00
|
|
|
MULTIWAIT_ID initializeMultiWait() { return new std::condition_variable; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-08-19 11:12:54 -04:00
|
|
|
void deleteMultiWait(MULTIWAIT_ID cond) { delete cond; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-08-19 11:12:54 -04:00
|
|
|
void takeMultiWait(MULTIWAIT_ID cond, MUTEX_ID m) {
|
|
|
|
|
std::unique_lock<std::mutex> lock(*m);
|
|
|
|
|
cond->wait(lock);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-08-19 11:12:54 -04:00
|
|
|
void giveMultiWait(MULTIWAIT_ID cond) { cond->notify_all(); }
|