2 changed files with 22 additions and 17 deletions
@ -1,26 +1,28 @@
@@ -1,26 +1,28 @@
|
||||
|
||||
#include "Semaphores.h" |
||||
|
||||
extern const AP_HAL::HAL& hal; |
||||
|
||||
using namespace Linux; |
||||
|
||||
bool LinuxSemaphore::give() { |
||||
if (_taken) { |
||||
_taken = false; |
||||
return true; |
||||
} else { |
||||
return false; |
||||
} |
||||
bool LinuxSemaphore::give()
|
||||
{ |
||||
return pthread_mutex_unlock(&_lock) == 0; |
||||
} |
||||
|
||||
bool LinuxSemaphore::take(uint32_t timeout_ms) { |
||||
bool LinuxSemaphore::take(uint32_t timeout_ms)
|
||||
{ |
||||
if (timeout_ms == 0) { |
||||
return pthread_mutex_lock(&_lock) == 0; |
||||
} |
||||
while (timeout_ms-- > 0) { |
||||
if (take_nonblocking()) return true; |
||||
hal.scheduler->delay(1); |
||||
} |
||||
return take_nonblocking(); |
||||
} |
||||
|
||||
bool LinuxSemaphore::take_nonblocking() { |
||||
/* we need pthread semaphores here */ |
||||
if (!_taken) { |
||||
_taken = true; |
||||
return true; |
||||
} |
||||
return false; |
||||
bool LinuxSemaphore::take_nonblocking()
|
||||
{ |
||||
return pthread_mutex_trylock(&_lock) == 0; |
||||
} |
||||
|
Loading…
Reference in new issue