Compare commits
2 Commits
ca8e7b40bc
...
c2053ed3dd
Author | SHA1 | Date |
---|---|---|
|
c2053ed3dd | |
|
f3408bb0df |
|
@ -35,6 +35,8 @@ namespace BAN
|
||||||
inline T operator--(int) volatile { return __atomic_fetch_sub(&m_value, 1, MEM_ORDER); }
|
inline T operator--(int) volatile { return __atomic_fetch_sub(&m_value, 1, MEM_ORDER); }
|
||||||
inline T operator++(int) volatile { return __atomic_fetch_add(&m_value, 1, MEM_ORDER); }
|
inline T operator++(int) volatile { return __atomic_fetch_add(&m_value, 1, MEM_ORDER); }
|
||||||
|
|
||||||
|
bool compare_exchange(T expected, T desired) volatile { return __atomic_compare_exchange_n(&m_value, &expected, desired, false, MEM_ORDER, MEM_ORDER); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
T m_value;
|
T m_value;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <BAN/Atomic.h>
|
||||||
#include <BAN/NoCopyMove.h>
|
#include <BAN/NoCopyMove.h>
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
@ -19,7 +20,7 @@ namespace Kernel
|
||||||
bool is_locked() const;
|
bool is_locked() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
volatile pid_t m_locker = -1;
|
BAN::Atomic<pid_t> m_locker = -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
class RecursiveSpinLock
|
class RecursiveSpinLock
|
||||||
|
@ -34,8 +35,8 @@ namespace Kernel
|
||||||
bool is_locked() const;
|
bool is_locked() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
pid_t m_locker = -1;
|
BAN::Atomic<pid_t> m_locker = -1;
|
||||||
uint32_t m_lock_depth = 0;
|
BAN::Atomic<uint32_t> m_lock_depth = 0;
|
||||||
SpinLock m_lock;
|
SpinLock m_lock;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
#include <kernel/Scheduler.h>
|
#include <kernel/Scheduler.h>
|
||||||
#include <kernel/SpinLock.h>
|
#include <kernel/SpinLock.h>
|
||||||
#include <kernel/CriticalScope.h>
|
|
||||||
|
|
||||||
namespace Kernel
|
namespace Kernel
|
||||||
{
|
{
|
||||||
|
@ -8,31 +7,18 @@ namespace Kernel
|
||||||
void SpinLock::lock()
|
void SpinLock::lock()
|
||||||
{
|
{
|
||||||
pid_t tid = Scheduler::current_tid();
|
pid_t tid = Scheduler::current_tid();
|
||||||
while (true)
|
while (!m_locker.compare_exchange(-1, tid))
|
||||||
{
|
|
||||||
{
|
|
||||||
CriticalScope _;
|
|
||||||
ASSERT(m_locker != tid);
|
|
||||||
if (m_locker == -1)
|
|
||||||
{
|
|
||||||
m_locker = tid;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Scheduler::get().reschedule();
|
Scheduler::get().reschedule();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpinLock::unlock()
|
void SpinLock::unlock()
|
||||||
{
|
{
|
||||||
CriticalScope _;
|
|
||||||
ASSERT(m_locker == Scheduler::current_tid());
|
ASSERT(m_locker == Scheduler::current_tid());
|
||||||
m_locker = -1;
|
m_locker = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SpinLock::is_locked() const
|
bool SpinLock::is_locked() const
|
||||||
{
|
{
|
||||||
CriticalScope _;
|
|
||||||
return m_locker != -1;
|
return m_locker != -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue