AP_HAL_Empty: implement new Semaphore interface
This commit is contained in:
parent
2d6b649aa4
commit
f178d1bd02
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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__
|
||||
|
Loading…
Reference in New Issue
Block a user