blob: db9e9a3b4a952515343c298b0586b345e7e902e1 [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001#include "HAL/Semaphore.hpp"
2
3#include "Log.hpp"
4
5// set the logging level
6TLogLevel semaphoreLogLevel = logDEBUG;
7
8#define SEMAPHORE_LOG(level) \
9 if (level > semaphoreLogLevel) ; \
10 else Log().Get(level)
11
12MUTEX_ID initializeMutexNormal() { return new priority_mutex; }
13
14void deleteMutex(MUTEX_ID sem) { delete sem; }
15
16/**
17 * Lock the mutex, blocking until it's available.
18 */
19void 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 */
25bool 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 */
31void giveMutex(MUTEX_ID mutex) { mutex->unlock(); }
32
33MULTIWAIT_ID initializeMultiWait() { return new priority_condition_variable; }
34
35void deleteMultiWait(MULTIWAIT_ID cond) { delete cond; }
36
37void takeMultiWait(MULTIWAIT_ID cond, MUTEX_ID m) {
38 std::unique_lock<priority_mutex> lock(*m);
39 cond->wait(lock);
40}
41
42void giveMultiWait(MULTIWAIT_ID cond) { cond->notify_all(); }