kernel: convert GlobalSchedulerContext, KAddressArbiter, KScopedSchedulerLockAndSleep, KThreadQueue to new style
This commit is contained in:
parent
54c359d1e3
commit
d24ab14126
|
@ -12,20 +12,19 @@
|
||||||
|
|
||||||
namespace Kernel {
|
namespace Kernel {
|
||||||
|
|
||||||
GlobalSchedulerContext::GlobalSchedulerContext(KernelCore& kernel_)
|
GlobalSchedulerContext::GlobalSchedulerContext(KernelCore& kernel)
|
||||||
: kernel{kernel_}, scheduler_lock{kernel_} {}
|
: m_kernel{kernel}, m_scheduler_lock{kernel} {}
|
||||||
|
|
||||||
GlobalSchedulerContext::~GlobalSchedulerContext() = default;
|
GlobalSchedulerContext::~GlobalSchedulerContext() = default;
|
||||||
|
|
||||||
void GlobalSchedulerContext::AddThread(KThread* thread) {
|
void GlobalSchedulerContext::AddThread(KThread* thread) {
|
||||||
std::scoped_lock lock{global_list_guard};
|
std::scoped_lock lock{m_global_list_guard};
|
||||||
thread_list.push_back(thread);
|
m_thread_list.push_back(thread);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlobalSchedulerContext::RemoveThread(KThread* thread) {
|
void GlobalSchedulerContext::RemoveThread(KThread* thread) {
|
||||||
std::scoped_lock lock{global_list_guard};
|
std::scoped_lock lock{m_global_list_guard};
|
||||||
thread_list.erase(std::remove(thread_list.begin(), thread_list.end(), thread),
|
std::erase(m_thread_list, thread);
|
||||||
thread_list.end());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlobalSchedulerContext::PreemptThreads() {
|
void GlobalSchedulerContext::PreemptThreads() {
|
||||||
|
@ -38,37 +37,37 @@ void GlobalSchedulerContext::PreemptThreads() {
|
||||||
63,
|
63,
|
||||||
};
|
};
|
||||||
|
|
||||||
ASSERT(IsLocked());
|
ASSERT(KScheduler::IsSchedulerLockedByCurrentThread(m_kernel));
|
||||||
for (u32 core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) {
|
for (u32 core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) {
|
||||||
const u32 priority = preemption_priorities[core_id];
|
const u32 priority = preemption_priorities[core_id];
|
||||||
KScheduler::RotateScheduledQueue(kernel, core_id, priority);
|
KScheduler::RotateScheduledQueue(m_kernel, core_id, priority);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GlobalSchedulerContext::IsLocked() const {
|
bool GlobalSchedulerContext::IsLocked() const {
|
||||||
return scheduler_lock.IsLockedByCurrentThread();
|
return m_scheduler_lock.IsLockedByCurrentThread();
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlobalSchedulerContext::RegisterDummyThreadForWakeup(KThread* thread) {
|
void GlobalSchedulerContext::RegisterDummyThreadForWakeup(KThread* thread) {
|
||||||
ASSERT(IsLocked());
|
ASSERT(this->IsLocked());
|
||||||
|
|
||||||
woken_dummy_threads.insert(thread);
|
m_woken_dummy_threads.insert(thread);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlobalSchedulerContext::UnregisterDummyThreadForWakeup(KThread* thread) {
|
void GlobalSchedulerContext::UnregisterDummyThreadForWakeup(KThread* thread) {
|
||||||
ASSERT(IsLocked());
|
ASSERT(this->IsLocked());
|
||||||
|
|
||||||
woken_dummy_threads.erase(thread);
|
m_woken_dummy_threads.erase(thread);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlobalSchedulerContext::WakeupWaitingDummyThreads() {
|
void GlobalSchedulerContext::WakeupWaitingDummyThreads() {
|
||||||
ASSERT(IsLocked());
|
ASSERT(this->IsLocked());
|
||||||
|
|
||||||
for (auto* thread : woken_dummy_threads) {
|
for (auto* thread : m_woken_dummy_threads) {
|
||||||
thread->DummyThreadEndWait();
|
thread->DummyThreadEndWait();
|
||||||
}
|
}
|
||||||
|
|
||||||
woken_dummy_threads.clear();
|
m_woken_dummy_threads.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Kernel
|
} // namespace Kernel
|
||||||
|
|
|
@ -33,7 +33,7 @@ class GlobalSchedulerContext final {
|
||||||
public:
|
public:
|
||||||
using LockType = KAbstractSchedulerLock<KScheduler>;
|
using LockType = KAbstractSchedulerLock<KScheduler>;
|
||||||
|
|
||||||
explicit GlobalSchedulerContext(KernelCore& kernel_);
|
explicit GlobalSchedulerContext(KernelCore& kernel);
|
||||||
~GlobalSchedulerContext();
|
~GlobalSchedulerContext();
|
||||||
|
|
||||||
/// Adds a new thread to the scheduler
|
/// Adds a new thread to the scheduler
|
||||||
|
@ -43,8 +43,9 @@ public:
|
||||||
void RemoveThread(KThread* thread);
|
void RemoveThread(KThread* thread);
|
||||||
|
|
||||||
/// Returns a list of all threads managed by the scheduler
|
/// Returns a list of all threads managed by the scheduler
|
||||||
|
/// This is only safe to iterate while holding the scheduler lock
|
||||||
[[nodiscard]] const std::vector<KThread*>& GetThreadList() const {
|
[[nodiscard]] const std::vector<KThread*>& GetThreadList() const {
|
||||||
return thread_list;
|
return m_thread_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -64,29 +65,25 @@ public:
|
||||||
void WakeupWaitingDummyThreads();
|
void WakeupWaitingDummyThreads();
|
||||||
|
|
||||||
[[nodiscard]] LockType& SchedulerLock() {
|
[[nodiscard]] LockType& SchedulerLock() {
|
||||||
return scheduler_lock;
|
return m_scheduler_lock;
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] const LockType& SchedulerLock() const {
|
|
||||||
return scheduler_lock;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class KScopedSchedulerLock;
|
friend class KScopedSchedulerLock;
|
||||||
friend class KScopedSchedulerLockAndSleep;
|
friend class KScopedSchedulerLockAndSleep;
|
||||||
|
|
||||||
KernelCore& kernel;
|
KernelCore& m_kernel;
|
||||||
|
|
||||||
std::atomic_bool scheduler_update_needed{};
|
std::atomic_bool m_scheduler_update_needed{};
|
||||||
KSchedulerPriorityQueue priority_queue;
|
KSchedulerPriorityQueue m_priority_queue;
|
||||||
LockType scheduler_lock;
|
LockType m_scheduler_lock;
|
||||||
|
|
||||||
/// Lists dummy threads pending wakeup on lock release
|
/// Lists dummy threads pending wakeup on lock release
|
||||||
std::set<KThread*> woken_dummy_threads;
|
std::set<KThread*> m_woken_dummy_threads;
|
||||||
|
|
||||||
/// Lists all thread ids that aren't deleted/etc.
|
/// Lists all thread ids that aren't deleted/etc.
|
||||||
std::vector<KThread*> thread_list;
|
std::vector<KThread*> m_thread_list;
|
||||||
std::mutex global_list_guard;
|
std::mutex m_global_list_guard;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Kernel
|
} // namespace Kernel
|
||||||
|
|
|
@ -14,8 +14,8 @@
|
||||||
|
|
||||||
namespace Kernel {
|
namespace Kernel {
|
||||||
|
|
||||||
KAddressArbiter::KAddressArbiter(Core::System& system_)
|
KAddressArbiter::KAddressArbiter(Core::System& system)
|
||||||
: system{system_}, kernel{system.Kernel()} {}
|
: m_system{system}, m_kernel{system.Kernel()} {}
|
||||||
KAddressArbiter::~KAddressArbiter() = default;
|
KAddressArbiter::~KAddressArbiter() = default;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
@ -90,8 +90,8 @@ bool UpdateIfEqual(Core::System& system, s32* out, VAddr address, s32 value, s32
|
||||||
|
|
||||||
class ThreadQueueImplForKAddressArbiter final : public KThreadQueue {
|
class ThreadQueueImplForKAddressArbiter final : public KThreadQueue {
|
||||||
public:
|
public:
|
||||||
explicit ThreadQueueImplForKAddressArbiter(KernelCore& kernel_, KAddressArbiter::ThreadTree* t)
|
explicit ThreadQueueImplForKAddressArbiter(KernelCore& kernel, KAddressArbiter::ThreadTree* t)
|
||||||
: KThreadQueue(kernel_), m_tree(t) {}
|
: KThreadQueue(kernel), m_tree(t) {}
|
||||||
|
|
||||||
void CancelWait(KThread* waiting_thread, Result wait_result, bool cancel_timer_task) override {
|
void CancelWait(KThread* waiting_thread, Result wait_result, bool cancel_timer_task) override {
|
||||||
// If the thread is waiting on an address arbiter, remove it from the tree.
|
// If the thread is waiting on an address arbiter, remove it from the tree.
|
||||||
|
@ -105,7 +105,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
KAddressArbiter::ThreadTree* m_tree;
|
KAddressArbiter::ThreadTree* m_tree{};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
@ -114,10 +114,10 @@ Result KAddressArbiter::Signal(VAddr addr, s32 count) {
|
||||||
// Perform signaling.
|
// Perform signaling.
|
||||||
s32 num_waiters{};
|
s32 num_waiters{};
|
||||||
{
|
{
|
||||||
KScopedSchedulerLock sl(kernel);
|
KScopedSchedulerLock sl(m_kernel);
|
||||||
|
|
||||||
auto it = thread_tree.nfind_key({addr, -1});
|
auto it = m_tree.nfind_key({addr, -1});
|
||||||
while ((it != thread_tree.end()) && (count <= 0 || num_waiters < count) &&
|
while ((it != m_tree.end()) && (count <= 0 || num_waiters < count) &&
|
||||||
(it->GetAddressArbiterKey() == addr)) {
|
(it->GetAddressArbiterKey() == addr)) {
|
||||||
// End the thread's wait.
|
// End the thread's wait.
|
||||||
KThread* target_thread = std::addressof(*it);
|
KThread* target_thread = std::addressof(*it);
|
||||||
|
@ -126,31 +126,27 @@ Result KAddressArbiter::Signal(VAddr addr, s32 count) {
|
||||||
ASSERT(target_thread->IsWaitingForAddressArbiter());
|
ASSERT(target_thread->IsWaitingForAddressArbiter());
|
||||||
target_thread->ClearAddressArbiter();
|
target_thread->ClearAddressArbiter();
|
||||||
|
|
||||||
it = thread_tree.erase(it);
|
it = m_tree.erase(it);
|
||||||
++num_waiters;
|
++num_waiters;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ResultSuccess;
|
R_SUCCEED();
|
||||||
}
|
}
|
||||||
|
|
||||||
Result KAddressArbiter::SignalAndIncrementIfEqual(VAddr addr, s32 value, s32 count) {
|
Result KAddressArbiter::SignalAndIncrementIfEqual(VAddr addr, s32 value, s32 count) {
|
||||||
// Perform signaling.
|
// Perform signaling.
|
||||||
s32 num_waiters{};
|
s32 num_waiters{};
|
||||||
{
|
{
|
||||||
KScopedSchedulerLock sl(kernel);
|
KScopedSchedulerLock sl(m_kernel);
|
||||||
|
|
||||||
// Check the userspace value.
|
// Check the userspace value.
|
||||||
s32 user_value{};
|
s32 user_value{};
|
||||||
if (!UpdateIfEqual(system, &user_value, addr, value, value + 1)) {
|
R_UNLESS(UpdateIfEqual(m_system, &user_value, addr, value, value + 1),
|
||||||
LOG_ERROR(Kernel, "Invalid current memory!");
|
ResultInvalidCurrentMemory);
|
||||||
return ResultInvalidCurrentMemory;
|
R_UNLESS(user_value == value, ResultInvalidState);
|
||||||
}
|
|
||||||
if (user_value != value) {
|
|
||||||
return ResultInvalidState;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto it = thread_tree.nfind_key({addr, -1});
|
auto it = m_tree.nfind_key({addr, -1});
|
||||||
while ((it != thread_tree.end()) && (count <= 0 || num_waiters < count) &&
|
while ((it != m_tree.end()) && (count <= 0 || num_waiters < count) &&
|
||||||
(it->GetAddressArbiterKey() == addr)) {
|
(it->GetAddressArbiterKey() == addr)) {
|
||||||
// End the thread's wait.
|
// End the thread's wait.
|
||||||
KThread* target_thread = std::addressof(*it);
|
KThread* target_thread = std::addressof(*it);
|
||||||
|
@ -159,33 +155,33 @@ Result KAddressArbiter::SignalAndIncrementIfEqual(VAddr addr, s32 value, s32 cou
|
||||||
ASSERT(target_thread->IsWaitingForAddressArbiter());
|
ASSERT(target_thread->IsWaitingForAddressArbiter());
|
||||||
target_thread->ClearAddressArbiter();
|
target_thread->ClearAddressArbiter();
|
||||||
|
|
||||||
it = thread_tree.erase(it);
|
it = m_tree.erase(it);
|
||||||
++num_waiters;
|
++num_waiters;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ResultSuccess;
|
R_SUCCEED();
|
||||||
}
|
}
|
||||||
|
|
||||||
Result KAddressArbiter::SignalAndModifyByWaitingCountIfEqual(VAddr addr, s32 value, s32 count) {
|
Result KAddressArbiter::SignalAndModifyByWaitingCountIfEqual(VAddr addr, s32 value, s32 count) {
|
||||||
// Perform signaling.
|
// Perform signaling.
|
||||||
s32 num_waiters{};
|
s32 num_waiters{};
|
||||||
{
|
{
|
||||||
[[maybe_unused]] const KScopedSchedulerLock sl(kernel);
|
KScopedSchedulerLock sl(m_kernel);
|
||||||
|
|
||||||
auto it = thread_tree.nfind_key({addr, -1});
|
auto it = m_tree.nfind_key({addr, -1});
|
||||||
// Determine the updated value.
|
// Determine the updated value.
|
||||||
s32 new_value{};
|
s32 new_value{};
|
||||||
if (count <= 0) {
|
if (count <= 0) {
|
||||||
if (it != thread_tree.end() && it->GetAddressArbiterKey() == addr) {
|
if (it != m_tree.end() && it->GetAddressArbiterKey() == addr) {
|
||||||
new_value = value - 2;
|
new_value = value - 2;
|
||||||
} else {
|
} else {
|
||||||
new_value = value + 1;
|
new_value = value + 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (it != thread_tree.end() && it->GetAddressArbiterKey() == addr) {
|
if (it != m_tree.end() && it->GetAddressArbiterKey() == addr) {
|
||||||
auto tmp_it = it;
|
auto tmp_it = it;
|
||||||
s32 tmp_num_waiters{};
|
s32 tmp_num_waiters{};
|
||||||
while (++tmp_it != thread_tree.end() && tmp_it->GetAddressArbiterKey() == addr) {
|
while (++tmp_it != m_tree.end() && tmp_it->GetAddressArbiterKey() == addr) {
|
||||||
if (tmp_num_waiters++ >= count) {
|
if (tmp_num_waiters++ >= count) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -205,20 +201,15 @@ Result KAddressArbiter::SignalAndModifyByWaitingCountIfEqual(VAddr addr, s32 val
|
||||||
s32 user_value{};
|
s32 user_value{};
|
||||||
bool succeeded{};
|
bool succeeded{};
|
||||||
if (value != new_value) {
|
if (value != new_value) {
|
||||||
succeeded = UpdateIfEqual(system, &user_value, addr, value, new_value);
|
succeeded = UpdateIfEqual(m_system, &user_value, addr, value, new_value);
|
||||||
} else {
|
} else {
|
||||||
succeeded = ReadFromUser(system, &user_value, addr);
|
succeeded = ReadFromUser(m_system, &user_value, addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!succeeded) {
|
R_UNLESS(succeeded, ResultInvalidCurrentMemory);
|
||||||
LOG_ERROR(Kernel, "Invalid current memory!");
|
R_UNLESS(user_value == value, ResultInvalidState);
|
||||||
return ResultInvalidCurrentMemory;
|
|
||||||
}
|
|
||||||
if (user_value != value) {
|
|
||||||
return ResultInvalidState;
|
|
||||||
}
|
|
||||||
|
|
||||||
while ((it != thread_tree.end()) && (count <= 0 || num_waiters < count) &&
|
while ((it != m_tree.end()) && (count <= 0 || num_waiters < count) &&
|
||||||
(it->GetAddressArbiterKey() == addr)) {
|
(it->GetAddressArbiterKey() == addr)) {
|
||||||
// End the thread's wait.
|
// End the thread's wait.
|
||||||
KThread* target_thread = std::addressof(*it);
|
KThread* target_thread = std::addressof(*it);
|
||||||
|
@ -227,57 +218,57 @@ Result KAddressArbiter::SignalAndModifyByWaitingCountIfEqual(VAddr addr, s32 val
|
||||||
ASSERT(target_thread->IsWaitingForAddressArbiter());
|
ASSERT(target_thread->IsWaitingForAddressArbiter());
|
||||||
target_thread->ClearAddressArbiter();
|
target_thread->ClearAddressArbiter();
|
||||||
|
|
||||||
it = thread_tree.erase(it);
|
it = m_tree.erase(it);
|
||||||
++num_waiters;
|
++num_waiters;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ResultSuccess;
|
R_SUCCEED();
|
||||||
}
|
}
|
||||||
|
|
||||||
Result KAddressArbiter::WaitIfLessThan(VAddr addr, s32 value, bool decrement, s64 timeout) {
|
Result KAddressArbiter::WaitIfLessThan(VAddr addr, s32 value, bool decrement, s64 timeout) {
|
||||||
// Prepare to wait.
|
// Prepare to wait.
|
||||||
KThread* cur_thread = GetCurrentThreadPointer(kernel);
|
KThread* cur_thread = GetCurrentThreadPointer(m_kernel);
|
||||||
KHardwareTimer* timer{};
|
KHardwareTimer* timer{};
|
||||||
ThreadQueueImplForKAddressArbiter wait_queue(kernel, std::addressof(thread_tree));
|
ThreadQueueImplForKAddressArbiter wait_queue(m_kernel, std::addressof(m_tree));
|
||||||
|
|
||||||
{
|
{
|
||||||
KScopedSchedulerLockAndSleep slp{kernel, std::addressof(timer), cur_thread, timeout};
|
KScopedSchedulerLockAndSleep slp{m_kernel, std::addressof(timer), cur_thread, timeout};
|
||||||
|
|
||||||
// Check that the thread isn't terminating.
|
// Check that the thread isn't terminating.
|
||||||
if (cur_thread->IsTerminationRequested()) {
|
if (cur_thread->IsTerminationRequested()) {
|
||||||
slp.CancelSleep();
|
slp.CancelSleep();
|
||||||
return ResultTerminationRequested;
|
R_THROW(ResultTerminationRequested);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the value from userspace.
|
// Read the value from userspace.
|
||||||
s32 user_value{};
|
s32 user_value{};
|
||||||
bool succeeded{};
|
bool succeeded{};
|
||||||
if (decrement) {
|
if (decrement) {
|
||||||
succeeded = DecrementIfLessThan(system, &user_value, addr, value);
|
succeeded = DecrementIfLessThan(m_system, &user_value, addr, value);
|
||||||
} else {
|
} else {
|
||||||
succeeded = ReadFromUser(system, &user_value, addr);
|
succeeded = ReadFromUser(m_system, &user_value, addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!succeeded) {
|
if (!succeeded) {
|
||||||
slp.CancelSleep();
|
slp.CancelSleep();
|
||||||
return ResultInvalidCurrentMemory;
|
R_THROW(ResultInvalidCurrentMemory);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that the value is less than the specified one.
|
// Check that the value is less than the specified one.
|
||||||
if (user_value >= value) {
|
if (user_value >= value) {
|
||||||
slp.CancelSleep();
|
slp.CancelSleep();
|
||||||
return ResultInvalidState;
|
R_THROW(ResultInvalidState);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that the timeout is non-zero.
|
// Check that the timeout is non-zero.
|
||||||
if (timeout == 0) {
|
if (timeout == 0) {
|
||||||
slp.CancelSleep();
|
slp.CancelSleep();
|
||||||
return ResultTimedOut;
|
R_THROW(ResultTimedOut);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the arbiter.
|
// Set the arbiter.
|
||||||
cur_thread->SetAddressArbiter(&thread_tree, addr);
|
cur_thread->SetAddressArbiter(std::addressof(m_tree), addr);
|
||||||
thread_tree.insert(*cur_thread);
|
m_tree.insert(*cur_thread);
|
||||||
|
|
||||||
// Wait for the thread to finish.
|
// Wait for the thread to finish.
|
||||||
wait_queue.SetHardwareTimer(timer);
|
wait_queue.SetHardwareTimer(timer);
|
||||||
|
@ -291,41 +282,41 @@ Result KAddressArbiter::WaitIfLessThan(VAddr addr, s32 value, bool decrement, s6
|
||||||
|
|
||||||
Result KAddressArbiter::WaitIfEqual(VAddr addr, s32 value, s64 timeout) {
|
Result KAddressArbiter::WaitIfEqual(VAddr addr, s32 value, s64 timeout) {
|
||||||
// Prepare to wait.
|
// Prepare to wait.
|
||||||
KThread* cur_thread = GetCurrentThreadPointer(kernel);
|
KThread* cur_thread = GetCurrentThreadPointer(m_kernel);
|
||||||
KHardwareTimer* timer{};
|
KHardwareTimer* timer{};
|
||||||
ThreadQueueImplForKAddressArbiter wait_queue(kernel, std::addressof(thread_tree));
|
ThreadQueueImplForKAddressArbiter wait_queue(m_kernel, std::addressof(m_tree));
|
||||||
|
|
||||||
{
|
{
|
||||||
KScopedSchedulerLockAndSleep slp{kernel, std::addressof(timer), cur_thread, timeout};
|
KScopedSchedulerLockAndSleep slp{m_kernel, std::addressof(timer), cur_thread, timeout};
|
||||||
|
|
||||||
// Check that the thread isn't terminating.
|
// Check that the thread isn't terminating.
|
||||||
if (cur_thread->IsTerminationRequested()) {
|
if (cur_thread->IsTerminationRequested()) {
|
||||||
slp.CancelSleep();
|
slp.CancelSleep();
|
||||||
return ResultTerminationRequested;
|
R_THROW(ResultTerminationRequested);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the value from userspace.
|
// Read the value from userspace.
|
||||||
s32 user_value{};
|
s32 user_value{};
|
||||||
if (!ReadFromUser(system, &user_value, addr)) {
|
if (!ReadFromUser(m_system, &user_value, addr)) {
|
||||||
slp.CancelSleep();
|
slp.CancelSleep();
|
||||||
return ResultInvalidCurrentMemory;
|
R_THROW(ResultInvalidCurrentMemory);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that the value is equal.
|
// Check that the value is equal.
|
||||||
if (value != user_value) {
|
if (value != user_value) {
|
||||||
slp.CancelSleep();
|
slp.CancelSleep();
|
||||||
return ResultInvalidState;
|
R_THROW(ResultInvalidState);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that the timeout is non-zero.
|
// Check that the timeout is non-zero.
|
||||||
if (timeout == 0) {
|
if (timeout == 0) {
|
||||||
slp.CancelSleep();
|
slp.CancelSleep();
|
||||||
return ResultTimedOut;
|
R_THROW(ResultTimedOut);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the arbiter.
|
// Set the arbiter.
|
||||||
cur_thread->SetAddressArbiter(&thread_tree, addr);
|
cur_thread->SetAddressArbiter(std::addressof(m_tree), addr);
|
||||||
thread_tree.insert(*cur_thread);
|
m_tree.insert(*cur_thread);
|
||||||
|
|
||||||
// Wait for the thread to finish.
|
// Wait for the thread to finish.
|
||||||
wait_queue.SetHardwareTimer(timer);
|
wait_queue.SetHardwareTimer(timer);
|
||||||
|
|
|
@ -22,47 +22,46 @@ class KAddressArbiter {
|
||||||
public:
|
public:
|
||||||
using ThreadTree = KConditionVariable::ThreadTree;
|
using ThreadTree = KConditionVariable::ThreadTree;
|
||||||
|
|
||||||
explicit KAddressArbiter(Core::System& system_);
|
explicit KAddressArbiter(Core::System& system);
|
||||||
~KAddressArbiter();
|
~KAddressArbiter();
|
||||||
|
|
||||||
[[nodiscard]] Result SignalToAddress(VAddr addr, Svc::SignalType type, s32 value, s32 count) {
|
Result SignalToAddress(VAddr addr, Svc::SignalType type, s32 value, s32 count) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Svc::SignalType::Signal:
|
case Svc::SignalType::Signal:
|
||||||
return Signal(addr, count);
|
R_RETURN(this->Signal(addr, count));
|
||||||
case Svc::SignalType::SignalAndIncrementIfEqual:
|
case Svc::SignalType::SignalAndIncrementIfEqual:
|
||||||
return SignalAndIncrementIfEqual(addr, value, count);
|
R_RETURN(this->SignalAndIncrementIfEqual(addr, value, count));
|
||||||
case Svc::SignalType::SignalAndModifyByWaitingCountIfEqual:
|
case Svc::SignalType::SignalAndModifyByWaitingCountIfEqual:
|
||||||
return SignalAndModifyByWaitingCountIfEqual(addr, value, count);
|
R_RETURN(this->SignalAndModifyByWaitingCountIfEqual(addr, value, count));
|
||||||
|
default:
|
||||||
|
UNREACHABLE();
|
||||||
}
|
}
|
||||||
ASSERT(false);
|
|
||||||
return ResultUnknown;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] Result WaitForAddress(VAddr addr, Svc::ArbitrationType type, s32 value,
|
Result WaitForAddress(VAddr addr, Svc::ArbitrationType type, s32 value, s64 timeout) {
|
||||||
s64 timeout) {
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Svc::ArbitrationType::WaitIfLessThan:
|
case Svc::ArbitrationType::WaitIfLessThan:
|
||||||
return WaitIfLessThan(addr, value, false, timeout);
|
R_RETURN(WaitIfLessThan(addr, value, false, timeout));
|
||||||
case Svc::ArbitrationType::DecrementAndWaitIfLessThan:
|
case Svc::ArbitrationType::DecrementAndWaitIfLessThan:
|
||||||
return WaitIfLessThan(addr, value, true, timeout);
|
R_RETURN(WaitIfLessThan(addr, value, true, timeout));
|
||||||
case Svc::ArbitrationType::WaitIfEqual:
|
case Svc::ArbitrationType::WaitIfEqual:
|
||||||
return WaitIfEqual(addr, value, timeout);
|
R_RETURN(WaitIfEqual(addr, value, timeout));
|
||||||
|
default:
|
||||||
|
UNREACHABLE();
|
||||||
}
|
}
|
||||||
ASSERT(false);
|
|
||||||
return ResultUnknown;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
[[nodiscard]] Result Signal(VAddr addr, s32 count);
|
Result Signal(VAddr addr, s32 count);
|
||||||
[[nodiscard]] Result SignalAndIncrementIfEqual(VAddr addr, s32 value, s32 count);
|
Result SignalAndIncrementIfEqual(VAddr addr, s32 value, s32 count);
|
||||||
[[nodiscard]] Result SignalAndModifyByWaitingCountIfEqual(VAddr addr, s32 value, s32 count);
|
Result SignalAndModifyByWaitingCountIfEqual(VAddr addr, s32 value, s32 count);
|
||||||
[[nodiscard]] Result WaitIfLessThan(VAddr addr, s32 value, bool decrement, s64 timeout);
|
Result WaitIfLessThan(VAddr addr, s32 value, bool decrement, s64 timeout);
|
||||||
[[nodiscard]] Result WaitIfEqual(VAddr addr, s32 value, s64 timeout);
|
Result WaitIfEqual(VAddr addr, s32 value, s64 timeout);
|
||||||
|
|
||||||
ThreadTree thread_tree;
|
private:
|
||||||
|
ThreadTree m_tree;
|
||||||
Core::System& system;
|
Core::System& m_system;
|
||||||
KernelCore& kernel;
|
KernelCore& m_kernel;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Kernel
|
} // namespace Kernel
|
||||||
|
|
|
@ -80,17 +80,17 @@ public:
|
||||||
return GetCurrentThread(kernel).GetDisableDispatchCount() == 0;
|
return GetCurrentThread(kernel).GetDisableDispatchCount() == 0;
|
||||||
}
|
}
|
||||||
static bool IsSchedulerLockedByCurrentThread(KernelCore& kernel) {
|
static bool IsSchedulerLockedByCurrentThread(KernelCore& kernel) {
|
||||||
return kernel.GlobalSchedulerContext().scheduler_lock.IsLockedByCurrentThread();
|
return kernel.GlobalSchedulerContext().m_scheduler_lock.IsLockedByCurrentThread();
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool IsSchedulerUpdateNeeded(KernelCore& kernel) {
|
static bool IsSchedulerUpdateNeeded(KernelCore& kernel) {
|
||||||
return kernel.GlobalSchedulerContext().scheduler_update_needed;
|
return kernel.GlobalSchedulerContext().m_scheduler_update_needed;
|
||||||
}
|
}
|
||||||
static void SetSchedulerUpdateNeeded(KernelCore& kernel) {
|
static void SetSchedulerUpdateNeeded(KernelCore& kernel) {
|
||||||
kernel.GlobalSchedulerContext().scheduler_update_needed = true;
|
kernel.GlobalSchedulerContext().m_scheduler_update_needed = true;
|
||||||
}
|
}
|
||||||
static void ClearSchedulerUpdateNeeded(KernelCore& kernel) {
|
static void ClearSchedulerUpdateNeeded(KernelCore& kernel) {
|
||||||
kernel.GlobalSchedulerContext().scheduler_update_needed = false;
|
kernel.GlobalSchedulerContext().m_scheduler_update_needed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void DisableScheduling(KernelCore& kernel);
|
static void DisableScheduling(KernelCore& kernel);
|
||||||
|
@ -115,7 +115,7 @@ public:
|
||||||
private:
|
private:
|
||||||
// Static private API.
|
// Static private API.
|
||||||
static KSchedulerPriorityQueue& GetPriorityQueue(KernelCore& kernel) {
|
static KSchedulerPriorityQueue& GetPriorityQueue(KernelCore& kernel) {
|
||||||
return kernel.GlobalSchedulerContext().priority_queue;
|
return kernel.GlobalSchedulerContext().m_priority_queue;
|
||||||
}
|
}
|
||||||
static u64 UpdateHighestPriorityThreadsImpl(KernelCore& kernel);
|
static u64 UpdateHighestPriorityThreadsImpl(KernelCore& kernel);
|
||||||
|
|
||||||
|
@ -166,7 +166,7 @@ private:
|
||||||
class KScopedSchedulerLock : public KScopedLock<KScheduler::LockType> {
|
class KScopedSchedulerLock : public KScopedLock<KScheduler::LockType> {
|
||||||
public:
|
public:
|
||||||
explicit KScopedSchedulerLock(KernelCore& kernel)
|
explicit KScopedSchedulerLock(KernelCore& kernel)
|
||||||
: KScopedLock(kernel.GlobalSchedulerContext().scheduler_lock) {}
|
: KScopedLock(kernel.GlobalSchedulerContext().m_scheduler_lock) {}
|
||||||
~KScopedSchedulerLock() = default;
|
~KScopedSchedulerLock() = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -11,39 +11,39 @@
|
||||||
|
|
||||||
namespace Kernel {
|
namespace Kernel {
|
||||||
|
|
||||||
class [[nodiscard]] KScopedSchedulerLockAndSleep {
|
class KScopedSchedulerLockAndSleep {
|
||||||
public:
|
public:
|
||||||
explicit KScopedSchedulerLockAndSleep(KernelCore& kernel_, KHardwareTimer** out_timer,
|
explicit KScopedSchedulerLockAndSleep(KernelCore& kernel, KHardwareTimer** out_timer,
|
||||||
KThread* t, s64 timeout)
|
KThread* thread, s64 timeout_tick)
|
||||||
: kernel(kernel_), timeout_tick(timeout), thread(t), timer() {
|
: m_kernel(kernel), m_timeout_tick(timeout_tick), m_thread(thread), m_timer() {
|
||||||
// Lock the scheduler.
|
// Lock the scheduler.
|
||||||
kernel.GlobalSchedulerContext().scheduler_lock.Lock();
|
kernel.GlobalSchedulerContext().m_scheduler_lock.Lock();
|
||||||
|
|
||||||
// Set our timer only if the time is positive.
|
// Set our timer only if the time is positive.
|
||||||
timer = (timeout_tick > 0) ? std::addressof(kernel.HardwareTimer()) : nullptr;
|
m_timer = (timeout_tick > 0) ? std::addressof(kernel.HardwareTimer()) : nullptr;
|
||||||
|
|
||||||
*out_timer = timer;
|
*out_timer = m_timer;
|
||||||
}
|
}
|
||||||
|
|
||||||
~KScopedSchedulerLockAndSleep() {
|
~KScopedSchedulerLockAndSleep() {
|
||||||
// Register the sleep.
|
// Register the sleep.
|
||||||
if (timeout_tick > 0) {
|
if (m_timeout_tick > 0) {
|
||||||
timer->RegisterTask(thread, timeout_tick);
|
m_timer->RegisterTask(m_thread, m_timeout_tick);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unlock the scheduler.
|
// Unlock the scheduler.
|
||||||
kernel.GlobalSchedulerContext().scheduler_lock.Unlock();
|
m_kernel.GlobalSchedulerContext().m_scheduler_lock.Unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CancelSleep() {
|
void CancelSleep() {
|
||||||
timeout_tick = 0;
|
m_timeout_tick = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
KernelCore& kernel;
|
KernelCore& m_kernel;
|
||||||
s64 timeout_tick{};
|
s64 m_timeout_tick{};
|
||||||
KThread* thread{};
|
KThread* m_thread{};
|
||||||
KHardwareTimer* timer{};
|
KHardwareTimer* m_timer{};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Kernel
|
} // namespace Kernel
|
||||||
|
|
|
@ -7,9 +7,10 @@
|
||||||
|
|
||||||
namespace Kernel {
|
namespace Kernel {
|
||||||
|
|
||||||
void KThreadQueue::NotifyAvailable([[maybe_unused]] KThread* waiting_thread,
|
void KThreadQueue::NotifyAvailable(KThread* waiting_thread, KSynchronizationObject* signaled_object,
|
||||||
[[maybe_unused]] KSynchronizationObject* signaled_object,
|
Result wait_result) {
|
||||||
[[maybe_unused]] Result wait_result) {}
|
UNREACHABLE();
|
||||||
|
}
|
||||||
|
|
||||||
void KThreadQueue::EndWait(KThread* waiting_thread, Result wait_result) {
|
void KThreadQueue::EndWait(KThread* waiting_thread, Result wait_result) {
|
||||||
// Set the thread's wait result.
|
// Set the thread's wait result.
|
||||||
|
@ -43,7 +44,8 @@ void KThreadQueue::CancelWait(KThread* waiting_thread, Result wait_result, bool
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void KThreadQueueWithoutEndWait::EndWait([[maybe_unused]] KThread* waiting_thread,
|
void KThreadQueueWithoutEndWait::EndWait(KThread* waiting_thread, Result wait_result) {
|
||||||
[[maybe_unused]] Result wait_result) {}
|
UNREACHABLE();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Kernel
|
} // namespace Kernel
|
||||||
|
|
|
@ -12,7 +12,7 @@ class KHardwareTimer;
|
||||||
|
|
||||||
class KThreadQueue {
|
class KThreadQueue {
|
||||||
public:
|
public:
|
||||||
explicit KThreadQueue(KernelCore& kernel_) : kernel{kernel_}, m_hardware_timer{} {}
|
explicit KThreadQueue(KernelCore& kernel) : m_kernel{kernel}, m_hardware_timer{} {}
|
||||||
virtual ~KThreadQueue() = default;
|
virtual ~KThreadQueue() = default;
|
||||||
|
|
||||||
void SetHardwareTimer(KHardwareTimer* timer) {
|
void SetHardwareTimer(KHardwareTimer* timer) {
|
||||||
|
@ -25,7 +25,7 @@ public:
|
||||||
virtual void CancelWait(KThread* waiting_thread, Result wait_result, bool cancel_timer_task);
|
virtual void CancelWait(KThread* waiting_thread, Result wait_result, bool cancel_timer_task);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
KernelCore& kernel;
|
KernelCore& m_kernel;
|
||||||
KHardwareTimer* m_hardware_timer{};
|
KHardwareTimer* m_hardware_timer{};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue