AddressArbiter: Changed to Kernel::Object, big cleanup, removed code that made no sense.

This commit is contained in:
bunnei 2015-01-18 13:56:40 -05:00
parent e5a9f1c644
commit 254e4ebd58
5 changed files with 45 additions and 38 deletions

View File

@ -15,7 +15,7 @@
namespace Kernel { namespace Kernel {
class AddressArbiter : public WaitObject { class AddressArbiter : public Object {
public: public:
std::string GetTypeName() const override { return "Arbiter"; } std::string GetTypeName() const override { return "Arbiter"; }
std::string GetName() const override { return name; } std::string GetName() const override { return name; }
@ -30,7 +30,7 @@ public:
/// Arbitrate an address /// Arbitrate an address
ResultCode ArbitrateAddress(Handle handle, ArbitrationType type, u32 address, s32 value, u64 nanoseconds) { ResultCode ArbitrateAddress(Handle handle, ArbitrationType type, u32 address, s32 value, u64 nanoseconds) {
WaitObject* object = static_cast<WaitObject*>(Kernel::g_handle_table.GetGeneric(handle).get()); AddressArbiter* object = Kernel::g_handle_table.Get<AddressArbiter>(handle).get();
if (object == nullptr) if (object == nullptr)
return InvalidHandle(ErrorModule::Kernel); return InvalidHandle(ErrorModule::Kernel);
@ -41,24 +41,24 @@ ResultCode ArbitrateAddress(Handle handle, ArbitrationType type, u32 address, s3
case ArbitrationType::Signal: case ArbitrationType::Signal:
// Negative value means resume all threads // Negative value means resume all threads
if (value < 0) { if (value < 0) {
ArbitrateAllThreads(object, address); ArbitrateAllThreads(address);
} else { } else {
// Resume first N threads // Resume first N threads
for(int i = 0; i < value; i++) for(int i = 0; i < value; i++)
ArbitrateHighestPriorityThread(object, address); ArbitrateHighestPriorityThread(address);
} }
break; break;
// Wait current thread (acquire the arbiter)... // Wait current thread (acquire the arbiter)...
case ArbitrationType::WaitIfLessThan: case ArbitrationType::WaitIfLessThan:
if ((s32)Memory::Read32(address) <= value) { if ((s32)Memory::Read32(address) <= value) {
Kernel::WaitCurrentThread_ArbitrateAddress(object, address); Kernel::WaitCurrentThread_ArbitrateAddress(address);
HLE::Reschedule(__func__); HLE::Reschedule(__func__);
} }
break; break;
case ArbitrationType::WaitIfLessThanWithTimeout: case ArbitrationType::WaitIfLessThanWithTimeout:
if ((s32)Memory::Read32(address) <= value) { if ((s32)Memory::Read32(address) <= value) {
Kernel::WaitCurrentThread_ArbitrateAddress(object, address); Kernel::WaitCurrentThread_ArbitrateAddress(address);
Kernel::WakeThreadAfterDelay(GetCurrentThread(), nanoseconds); Kernel::WakeThreadAfterDelay(GetCurrentThread(), nanoseconds);
HLE::Reschedule(__func__); HLE::Reschedule(__func__);
} }
@ -68,7 +68,7 @@ ResultCode ArbitrateAddress(Handle handle, ArbitrationType type, u32 address, s3
s32 memory_value = Memory::Read32(address) - 1; s32 memory_value = Memory::Read32(address) - 1;
Memory::Write32(address, memory_value); Memory::Write32(address, memory_value);
if (memory_value <= value) { if (memory_value <= value) {
Kernel::WaitCurrentThread_ArbitrateAddress(object, address); Kernel::WaitCurrentThread_ArbitrateAddress(address);
HLE::Reschedule(__func__); HLE::Reschedule(__func__);
} }
break; break;
@ -78,7 +78,7 @@ ResultCode ArbitrateAddress(Handle handle, ArbitrationType type, u32 address, s3
s32 memory_value = Memory::Read32(address) - 1; s32 memory_value = Memory::Read32(address) - 1;
Memory::Write32(address, memory_value); Memory::Write32(address, memory_value);
if (memory_value <= value) { if (memory_value <= value) {
Kernel::WaitCurrentThread_ArbitrateAddress(object, address); Kernel::WaitCurrentThread_ArbitrateAddress(address);
Kernel::WakeThreadAfterDelay(GetCurrentThread(), nanoseconds); Kernel::WakeThreadAfterDelay(GetCurrentThread(), nanoseconds);
HLE::Reschedule(__func__); HLE::Reschedule(__func__);
} }

View File

@ -36,7 +36,7 @@ Thread* WaitObject::ReleaseNextThread() {
auto next_thread = waiting_threads.front(); auto next_thread = waiting_threads.front();
next_thread->ReleaseFromWait(this); next_thread->ReleaseWaitObject(this);
waiting_threads.erase(waiting_threads.begin()); waiting_threads.erase(waiting_threads.begin());
return next_thread.get(); return next_thread.get();

View File

@ -83,8 +83,8 @@ static void ChangeReadyState(Thread* t, bool ready) {
} }
} }
/// Check if a thread is blocking on a the specified object /// Check if a thread is waiting on a the specified wait object
static bool CheckWaitType(const Thread* thread, Object* wait_object) { static bool CheckWait_WaitObject(const Thread* thread, WaitObject* wait_object) {
for (auto itr = thread->wait_objects.begin(); itr != thread->wait_objects.end(); ++itr) { for (auto itr = thread->wait_objects.begin(); itr != thread->wait_objects.end(); ++itr) {
if (*itr == wait_object) if (*itr == wait_object)
return (thread->IsWaiting()); return (thread->IsWaiting());
@ -92,9 +92,9 @@ static bool CheckWaitType(const Thread* thread, Object* wait_object) {
return false; return false;
} }
/// Check if a thread is blocking on a the specified object and an address /// Check if the specified thread is waiting on the specified address to be arbitrated
static bool CheckWaitType(const Thread* thread, Object* wait_object, VAddr wait_address) { static bool CheckWait_AddressArbiter(const Thread* thread, VAddr wait_address) {
return CheckWaitType(thread, wait_object) && (wait_address == thread->wait_address); return thread->IsWaiting() && thread->wait_objects.empty() && wait_address == thread->wait_address;
} }
/// Stops the current thread /// Stops the current thread
@ -121,17 +121,17 @@ static void ChangeThreadState(Thread* t, ThreadStatus new_status) {
} }
/// Arbitrate the highest priority thread that is waiting /// Arbitrate the highest priority thread that is waiting
Thread* ArbitrateHighestPriorityThread(WaitObject* arbiter, u32 address) { Thread* ArbitrateHighestPriorityThread(u32 address) {
Thread* highest_priority_thread = nullptr; Thread* highest_priority_thread = nullptr;
s32 priority = THREADPRIO_LOWEST; s32 priority = THREADPRIO_LOWEST;
// Iterate through threads, find highest priority thread that is waiting to be arbitrated... // Iterate through threads, find highest priority thread that is waiting to be arbitrated...
for (auto& thread : thread_list) { for (auto& thread : thread_list) {
if (!CheckWaitType(thread.get(), arbiter, address)) if (!CheckWait_AddressArbiter(thread.get(), address))
continue; continue;
if (thread == nullptr) if (thread == nullptr)
continue; // TODO(yuriks): Thread handle will hang around forever. Should clean up. continue;
if(thread->current_priority <= priority) { if(thread->current_priority <= priority) {
highest_priority_thread = thread.get(); highest_priority_thread = thread.get();
@ -141,19 +141,19 @@ Thread* ArbitrateHighestPriorityThread(WaitObject* arbiter, u32 address) {
// If a thread was arbitrated, resume it // If a thread was arbitrated, resume it
if (nullptr != highest_priority_thread) { if (nullptr != highest_priority_thread) {
highest_priority_thread->ReleaseFromWait(arbiter); highest_priority_thread->ResumeFromWait();
} }
return highest_priority_thread; return highest_priority_thread;
} }
/// Arbitrate all threads currently waiting /// Arbitrate all threads currently waiting
void ArbitrateAllThreads(WaitObject* arbiter, u32 address) { void ArbitrateAllThreads(u32 address) {
// Iterate through threads, find highest priority thread that is waiting to be arbitrated... // Iterate through threads, find highest priority thread that is waiting to be arbitrated...
for (auto& thread : thread_list) { for (auto& thread : thread_list) {
if (CheckWaitType(thread.get(), arbiter, address)) if (CheckWait_AddressArbiter(thread.get(), address))
thread->ReleaseFromWait(arbiter); thread->ResumeFromWait();
} }
} }
@ -202,21 +202,28 @@ static Thread* NextThread() {
return next; return next;
} }
void WaitCurrentThread() { void WaitCurrentThread_Sleep() {
Thread* thread = GetCurrentThread(); Thread* thread = GetCurrentThread();
thread->wait_all = false;
thread->wait_address = 0;
thread->wait_objects.clear();
ChangeThreadState(thread, ThreadStatus(THREADSTATUS_WAIT | (thread->status & THREADSTATUS_SUSPEND))); ChangeThreadState(thread, ThreadStatus(THREADSTATUS_WAIT | (thread->status & THREADSTATUS_SUSPEND)));
} }
void WaitCurrentThread_WaitSynchronization(WaitObject* wait_object, bool wait_all) { void WaitCurrentThread_WaitSynchronization(WaitObject* wait_object, bool wait_all) {
Thread* thread = GetCurrentThread(); Thread* thread = GetCurrentThread();
thread->wait_all = wait_all; thread->wait_all = wait_all;
thread->wait_address = 0;
thread->wait_objects.push_back(wait_object); thread->wait_objects.push_back(wait_object);
ChangeThreadState(thread, ThreadStatus(THREADSTATUS_WAIT | (thread->status & THREADSTATUS_SUSPEND))); ChangeThreadState(thread, ThreadStatus(THREADSTATUS_WAIT | (thread->status & THREADSTATUS_SUSPEND)));
} }
void WaitCurrentThread_ArbitrateAddress(WaitObject* wait_object, VAddr wait_address) { void WaitCurrentThread_ArbitrateAddress(VAddr wait_address) {
WaitCurrentThread_WaitSynchronization(wait_object); Thread* thread = GetCurrentThread();
GetCurrentThread()->wait_address = wait_address; thread->wait_all = false;
thread->wait_address = wait_address;
thread->wait_objects.clear();
ChangeThreadState(thread, ThreadStatus(THREADSTATUS_WAIT | (thread->status & THREADSTATUS_SUSPEND)));
} }
/// Event type for the thread wake up event /// Event type for the thread wake up event
@ -248,7 +255,7 @@ void WakeThreadAfterDelay(Thread* thread, s64 nanoseconds) {
CoreTiming::ScheduleEvent(usToCycles(microseconds), ThreadWakeupEventType, thread->GetHandle()); CoreTiming::ScheduleEvent(usToCycles(microseconds), ThreadWakeupEventType, thread->GetHandle());
} }
void Thread::ReleaseFromWait(WaitObject* wait_object) { void Thread::ReleaseWaitObject(WaitObject* wait_object) {
if (wait_objects.empty()) { if (wait_objects.empty()) {
LOG_CRITICAL(Kernel, "thread is not waiting on any objects!"); LOG_CRITICAL(Kernel, "thread is not waiting on any objects!");
return; return;
@ -298,6 +305,7 @@ void Thread::ResumeFromWait() {
wait_objects.clear(); wait_objects.clear();
wait_all = false; wait_all = false;
wait_address = 0;
if (!(status & (THREADSTATUS_WAITSUSPEND | THREADSTATUS_DORMANT | THREADSTATUS_DEAD))) { if (!(status & (THREADSTATUS_WAITSUSPEND | THREADSTATUS_DORMANT | THREADSTATUS_DEAD))) {
ChangeReadyState(this, true); ChangeReadyState(this, true);

View File

@ -69,10 +69,10 @@ public:
void Stop(const char* reason); void Stop(const char* reason);
/** /**
* Release an object from the thread's wait list * Release an acquired wait object
* @param wait_object WaitObject to release from the thread's wait list * @param wait_object WaitObject to release
*/ */
void ReleaseFromWait(WaitObject* wait_object); void ReleaseWaitObject(WaitObject* wait_object);
/// Resumes a thread from waiting by marking it as "ready" /// Resumes a thread from waiting by marking it as "ready"
void ResumeFromWait(); void ResumeFromWait();
@ -120,16 +120,16 @@ SharedPtr<Thread> SetupMainThread(s32 priority, u32 stack_size);
void Reschedule(); void Reschedule();
/// Arbitrate the highest priority thread that is waiting /// Arbitrate the highest priority thread that is waiting
Thread* ArbitrateHighestPriorityThread(WaitObject* arbiter, u32 address); Thread* ArbitrateHighestPriorityThread(u32 address);
/// Arbitrate all threads currently waiting... /// Arbitrate all threads currently waiting...
void ArbitrateAllThreads(WaitObject* arbiter, u32 address); void ArbitrateAllThreads(u32 address);
/// Gets the current thread /// Gets the current thread
Thread* GetCurrentThread(); Thread* GetCurrentThread();
/// Waits the current thread /// Waits the current thread on a sleep
void WaitCurrentThread(); void WaitCurrentThread_Sleep();
/** /**
* Waits the current thread from a WaitSynchronization call * Waits the current thread from a WaitSynchronization call
@ -140,10 +140,9 @@ void WaitCurrentThread_WaitSynchronization(WaitObject* wait_object, bool wait_al
/** /**
* Waits the current thread from an ArbitrateAddress call * Waits the current thread from an ArbitrateAddress call
* @param wait_object Kernel object that we are waiting on
* @param wait_address Arbitration address used to resume from wait * @param wait_address Arbitration address used to resume from wait
*/ */
void WaitCurrentThread_ArbitrateAddress(WaitObject* wait_object, VAddr wait_address); void WaitCurrentThread_ArbitrateAddress(VAddr wait_address);
/** /**
* Schedules an event to wake up the specified thread after the specified delay. * Schedules an event to wake up the specified thread after the specified delay.

View File

@ -105,7 +105,7 @@ static Result SendSyncRequest(Handle handle) {
ResultVal<bool> wait = session->SyncRequest(); ResultVal<bool> wait = session->SyncRequest();
if (wait.Succeeded() && *wait) { if (wait.Succeeded() && *wait) {
Kernel::WaitCurrentThread(); // TODO(bunnei): Is this correct? Kernel::WaitCurrentThread_Sleep(); // TODO(bunnei): Is this correct?
} }
return wait.Code().raw; return wait.Code().raw;
@ -196,7 +196,7 @@ static Result WaitSynchronizationN(s32* out, Handle* handles, s32 handle_count,
// NOTE: This should deadlock the current thread if no timeout was specified // NOTE: This should deadlock the current thread if no timeout was specified
if (!wait_all) { if (!wait_all) {
wait_thread = true; wait_thread = true;
Kernel::WaitCurrentThread(); Kernel::WaitCurrentThread_Sleep();
} }
} }
@ -450,7 +450,7 @@ static void SleepThread(s64 nanoseconds) {
LOG_TRACE(Kernel_SVC, "called nanoseconds=%lld", nanoseconds); LOG_TRACE(Kernel_SVC, "called nanoseconds=%lld", nanoseconds);
// Sleep current thread and check for next thread to schedule // Sleep current thread and check for next thread to schedule
Kernel::WaitCurrentThread(); Kernel::WaitCurrentThread_Sleep();
// Create an event to wake the thread up after the specified nanosecond delay has passed // Create an event to wake the thread up after the specified nanosecond delay has passed
Kernel::WakeThreadAfterDelay(Kernel::GetCurrentThread(), nanoseconds); Kernel::WakeThreadAfterDelay(Kernel::GetCurrentThread(), nanoseconds);