Kernel: Add LockFreeGuard to LockGuard.h
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
#include <BAN/NoCopyMove.h>
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
@@ -27,4 +29,30 @@ namespace Kernel
|
||||
Lock& m_lock;
|
||||
};
|
||||
|
||||
template<typename Lock>
|
||||
class LockFreeGuard
|
||||
{
|
||||
BAN_NON_COPYABLE(LockFreeGuard);
|
||||
BAN_NON_MOVABLE(LockFreeGuard);
|
||||
|
||||
public:
|
||||
LockFreeGuard(Lock& lock)
|
||||
: m_lock(lock)
|
||||
, m_depth(lock.lock_depth())
|
||||
{
|
||||
for (uint32_t i = 0; i < m_depth; i++)
|
||||
m_lock.unlock();
|
||||
}
|
||||
|
||||
~LockFreeGuard()
|
||||
{
|
||||
for (uint32_t i = 0; i < m_depth; i++)
|
||||
m_lock.lock();
|
||||
}
|
||||
|
||||
private:
|
||||
Lock& m_lock;
|
||||
const uint32_t m_depth;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -19,6 +19,8 @@ namespace Kernel
|
||||
void unlock();
|
||||
bool is_locked() const;
|
||||
|
||||
uint32_t lock_depth() const { return m_locker != -1; }
|
||||
|
||||
private:
|
||||
BAN::Atomic<pid_t> m_locker = -1;
|
||||
};
|
||||
@@ -34,6 +36,8 @@ namespace Kernel
|
||||
void unlock();
|
||||
bool is_locked() const;
|
||||
|
||||
uint32_t lock_depth() const { return m_lock_depth; }
|
||||
|
||||
private:
|
||||
BAN::Atomic<pid_t> m_locker = -1;
|
||||
BAN::Atomic<uint32_t> m_lock_depth = 0;
|
||||
|
||||
Reference in New Issue
Block a user