vdev: add a SmartLock class that automatically unlocks when going out of scope

This commit is contained in:
Beat Küng 2016-04-22 18:37:39 +02:00 committed by Lorenz Meier
parent aacfd8d553
commit 9a0cff2a00
1 changed files with 20 additions and 0 deletions

View File

@ -196,6 +196,26 @@ protected:
px4_sem_post(&_lock);
}
/**
* @class Smart locking object that uses the same lock as lock(), but automatically
* takes the lock when created and releases the lock when the object goes out of
* scope. Use like this:
* {
* SmartLock smart_lock(*this);
* //critical section start
* ...
* //critical section end
* }
*/
class SmartLock
{
public:
SmartLock(Device &device) : _device(device) { _device.lock(); }
~SmartLock() { _device.unlock(); }
private:
Device &_device;
};
private:
px4_sem_t _lock; /**< lock to protect access to all class members (also for derived classes) */