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; using namespace Empty;
EmptySemaphore::EmptySemaphore() : bool EmptySemaphore::give() {
_owner(NULL), if (_taken) {
_k(NULL) _taken = false;
{}
bool EmptySemaphore::get(void* owner) {
if (_owner == NULL) {
_owner = owner;
return true; return true;
} else { } else {
return false; return false;
} }
} }
bool EmptySemaphore::release(void* owner) { bool EmptySemaphore::take(uint32_t timeout_ms) {
if (_owner == NULL || _owner != owner) { return take_nonblocking();
return false; }
} else {
_owner = NULL; bool EmptySemaphore::take_nonblocking() {
if (_k){ /* No syncronisation primitives to garuntee this is correct */
_k(); if (!_taken) {
_k = NULL; _taken = true;
}
return 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 { class Empty::EmptySemaphore : public AP_HAL::Semaphore {
public: public:
EmptySemaphore(); EmptySemaphore() : _taken(false) {}
// get - to claim ownership of the semaphore bool give();
bool get(void* owner); bool take(uint32_t timeout_ms);
// release - to give up ownership of the semaphore bool take_nonblocking();
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);
private: private:
void* _owner; bool _taken;
AP_HAL::Proc _k;
}; };
#endif // __AP_HAL_EMPTY_SEMAPHORE_H__ #endif // __AP_HAL_EMPTY_SEMAPHORE_H__