Kernel: Add LockFreeGuard to LockGuard.h
This commit is contained in:
parent
d452cf4170
commit
534b3e6a9a
|
@ -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;
|
||||
|
|
|
@ -227,28 +227,6 @@ namespace Kernel
|
|||
}
|
||||
}
|
||||
|
||||
// This to feels too hacky to expose out of here
|
||||
struct LockFreeGuard
|
||||
{
|
||||
LockFreeGuard(RecursivePrioritySpinLock& 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:
|
||||
RecursivePrioritySpinLock& m_lock;
|
||||
const uint32_t m_depth;
|
||||
};
|
||||
|
||||
BAN::ErrorOr<void> UnixDomainSocket::add_packet(BAN::ConstByteSpan packet)
|
||||
{
|
||||
LockGuard _(m_lock);
|
||||
|
|
Loading…
Reference in New Issue