AP_HAL_Empty: implement new Semaphore interface

This commit is contained in:
Pat Hickey 2013-01-02 16:44:23 -08:00
parent 2d6b649aa4
commit f178d1bd02
2 changed files with 18 additions and 36 deletions

View File

@ -3,38 +3,25 @@
using namespace Empty;
EmptySemaphore::EmptySemaphore() :
_owner(NULL),
_k(NULL)
{}
bool EmptySemaphore::get(void* owner) {
if (_owner == NULL) {
_owner = owner;
bool EmptySemaphore::give() {
if (_taken) {
_taken = false;
return true;
} else {
return false;
}
}
bool EmptySemaphore::release(void* owner) {
if (_owner == NULL || _owner != owner) {
return false;
} else {
_owner = NULL;
if (_k){
_k();
_k = NULL;
}
bool EmptySemaphore::take(uint32_t timeout_ms) {
return take_nonblocking();
}
bool EmptySemaphore::take_nonblocking() {
/* No syncronisation primitives to garuntee this is correct */
if (!_taken) {
_taken = true;
return true;
} else {
return false;
}
}
bool EmptySemaphore::call_on_release(void* caller, AP_HAL::Proc k) {
/* idk what semantics randy was looking for here, honestly.
* seems like a bad idea. */
_k = k;
return true;
}

View File

@ -6,17 +6,12 @@
class Empty::EmptySemaphore : public AP_HAL::Semaphore {
public:
EmptySemaphore();
// get - to claim ownership of the semaphore
bool get(void* owner);
// release - to give up ownership of the semaphore
bool release(void* owner);
// call_on_release - returns true if caller successfully added to the
// queue to be called back
bool call_on_release(void* caller, AP_HAL::Proc k);
EmptySemaphore() : _taken(false) {}
bool give();
bool take(uint32_t timeout_ms);
bool take_nonblocking();
private:
void* _owner;
AP_HAL::Proc _k;
bool _taken;
};
#endif // __AP_HAL_EMPTY_SEMAPHORE_H__