1
0
Fork 0

Kernel: Get rid of WaitTypes and simplify lots of code, removing hacks.

This commit is contained in:
bunnei 2015-01-18 13:25:51 -05:00
parent 6deb1a0119
commit e5a9f1c644
9 changed files with 63 additions and 122 deletions

View File

@ -28,13 +28,8 @@ public:
bool signaled; ///< Whether the event has already been signaled
std::string name; ///< Name of event (optional)
ResultVal<bool> Wait(bool wait_thread) override {
bool wait = !signaled;
if (wait && wait_thread) {
AddWaitingThread(GetCurrentThread());
Kernel::WaitCurrentThread_WaitSynchronization(WAITTYPE_EVENT, this);
}
return MakeResult<bool>(wait);
ResultVal<bool> Wait() override {
return MakeResult<bool>(!signaled);
}
ResultVal<bool> Acquire() override {

View File

@ -42,13 +42,15 @@ Thread* WaitObject::ReleaseNextThread() {
return next_thread.get();
}
void WaitObject::ReleaseAllWaitingThreads() {
void WaitObject::WakeupAllWaitingThreads() {
auto waiting_threads_copy = waiting_threads;
// We use a copy because ReleaseWaitObject will remove the thread from this object's
// waiting_threads list
for (auto thread : waiting_threads_copy)
thread->ReleaseWaitObject(this);
waiting_threads.clear();
_assert_msg_(Kernel, waiting_threads.empty(), "failed to awaken all waiting threads!");
}
HandleTable::HandleTable() {

View File

@ -65,11 +65,10 @@ public:
virtual Kernel::HandleType GetHandleType() const = 0;
/**
* Check if this object is available, (optionally) wait the current thread if not
* @param wait_thread If true, wait the current thread if this object is unavailable
* Check if this object is available
* @return True if the current thread should wait due to this object being unavailable
*/
virtual ResultVal<bool> Wait(bool wait_thread) {
virtual ResultVal<bool> Wait() {
LOG_ERROR(Kernel, "(UNIMPLEMENTED)");
return UnimplementedFunction(ErrorModule::Kernel);
}

View File

@ -26,7 +26,7 @@ public:
Handle lock_thread; ///< Handle to thread that currently has mutex
std::string name; ///< Name of mutex (optional)
ResultVal<bool> Wait(bool wait_thread) override;
ResultVal<bool> Wait() override;
ResultVal<bool> Acquire() override;
};
@ -156,12 +156,7 @@ Handle CreateMutex(bool initial_locked, const std::string& name) {
return handle;
}
ResultVal<bool> Mutex::Wait(bool wait_thread) {
if (locked && wait_thread) {
AddWaitingThread(GetCurrentThread());
Kernel::WaitCurrentThread_WaitSynchronization(WAITTYPE_MUTEX, this);
}
ResultVal<bool> Mutex::Wait() {
return MakeResult<bool>(locked);
}

View File

@ -32,15 +32,8 @@ public:
return available_count > 0;
}
ResultVal<bool> Wait(bool wait_thread) override {
bool wait = !IsAvailable();
if (wait && wait_thread) {
Kernel::WaitCurrentThread_WaitSynchronization(WAITTYPE_SEMA, this);
AddWaitingThread(GetCurrentThread());
}
return MakeResult<bool>(wait);
ResultVal<bool> Wait() override {
return MakeResult<bool>(!IsAvailable());
}
ResultVal<bool> Acquire() override {

View File

@ -22,14 +22,8 @@
namespace Kernel {
ResultVal<bool> Thread::Wait(bool wait_thread) {
const bool wait = status != THREADSTATUS_DORMANT;
if (wait && wait_thread) {
AddWaitingThread(GetCurrentThread());
WaitCurrentThread_WaitSynchronization(WAITTYPE_THREADEND, this);
}
return MakeResult<bool>(wait);
ResultVal<bool> Thread::Wait() {
return MakeResult<bool>(status != THREADSTATUS_DORMANT);
}
ResultVal<bool> Thread::Acquire() {
@ -68,7 +62,7 @@ static void ResetThread(Thread* t, u32 arg, s32 lowest_priority) {
if (t->current_priority < lowest_priority) {
t->current_priority = t->initial_priority;
}
t->wait_type = WAITTYPE_NONE;
t->wait_objects.clear();
t->wait_address = 0;
}
@ -89,23 +83,18 @@ static void ChangeReadyState(Thread* t, bool ready) {
}
}
/// Check if a thread is blocking on a specified wait type
static bool CheckWaitType(const Thread* thread, WaitType type) {
return (type == thread->wait_type) && (thread->IsWaiting());
}
/// Check if a thread is blocking on a specified wait type with a specified handle
static bool CheckWaitType(const Thread* thread, WaitType type, Object* wait_object) {
/// Check if a thread is blocking on a the specified object
static bool CheckWaitType(const Thread* thread, Object* wait_object) {
for (auto itr = thread->wait_objects.begin(); itr != thread->wait_objects.end(); ++itr) {
if (*itr == wait_object)
return CheckWaitType(thread, type);
return (thread->IsWaiting());
}
return false;
}
/// Check if a thread is blocking on a specified wait type with a specified handle and address
static bool CheckWaitType(const Thread* thread, WaitType type, Object* wait_object, VAddr wait_address) {
return CheckWaitType(thread, type, wait_object) && (wait_address == thread->wait_address);
/// Check if a thread is blocking on a the specified object and an address
static bool CheckWaitType(const Thread* thread, Object* wait_object, VAddr wait_address) {
return CheckWaitType(thread, wait_object) && (wait_address == thread->wait_address);
}
/// Stops the current thread
@ -118,7 +107,6 @@ void Thread::Stop(const char* reason) {
ReleaseAllWaitingThreads();
// Stopped threads are never waiting.
wait_type = WAITTYPE_NONE;
wait_objects.clear();
wait_address = 0;
}
@ -130,12 +118,6 @@ static void ChangeThreadState(Thread* t, ThreadStatus new_status) {
}
ChangeReadyState(t, (new_status & THREADSTATUS_READY) != 0);
t->status = new_status;
if (new_status == THREADSTATUS_WAIT) {
if (t->wait_type == WAITTYPE_NONE) {
LOG_ERROR(Kernel, "Waittype none not allowed");
}
}
}
/// Arbitrate the highest priority thread that is waiting
@ -145,7 +127,7 @@ Thread* ArbitrateHighestPriorityThread(WaitObject* arbiter, u32 address) {
// Iterate through threads, find highest priority thread that is waiting to be arbitrated...
for (auto& thread : thread_list) {
if (!CheckWaitType(thread.get(), WAITTYPE_ARB, arbiter, address))
if (!CheckWaitType(thread.get(), arbiter, address))
continue;
if (thread == nullptr)
@ -170,7 +152,7 @@ void ArbitrateAllThreads(WaitObject* arbiter, u32 address) {
// Iterate through threads, find highest priority thread that is waiting to be arbitrated...
for (auto& thread : thread_list) {
if (CheckWaitType(thread.get(), WAITTYPE_ARB, arbiter, address))
if (CheckWaitType(thread.get(), arbiter, address))
thread->ReleaseFromWait(arbiter);
}
}
@ -178,9 +160,6 @@ void ArbitrateAllThreads(WaitObject* arbiter, u32 address) {
/// Calls a thread by marking it as "ready" (note: will not actually execute until current thread yields)
static void CallThread(Thread* t) {
// Stop waiting
if (t->wait_type != WAITTYPE_NONE) {
t->wait_type = WAITTYPE_NONE;
}
ChangeThreadState(t, THREADSTATUS_READY);
}
@ -201,7 +180,6 @@ static void SwitchContext(Thread* t) {
current_thread = t;
ChangeReadyState(t, false);
t->status = (t->status | THREADSTATUS_RUNNING) & ~THREADSTATUS_READY;
t->wait_type = WAITTYPE_NONE;
Core::g_app_core->LoadContext(t->context);
} else {
current_thread = nullptr;
@ -224,23 +202,20 @@ static Thread* NextThread() {
return next;
}
void WaitCurrentThread(WaitType wait_type) {
void WaitCurrentThread() {
Thread* thread = GetCurrentThread();
thread->wait_type = wait_type;
ChangeThreadState(thread, ThreadStatus(THREADSTATUS_WAIT | (thread->status & THREADSTATUS_SUSPEND)));
}
void WaitCurrentThread_WaitSynchronization(WaitType wait_type, WaitObject* wait_object, unsigned index) {
void WaitCurrentThread_WaitSynchronization(WaitObject* wait_object, bool wait_all) {
Thread* thread = GetCurrentThread();
thread->wait_type = wait_type;
thread->wait_all = wait_all;
thread->wait_objects.push_back(wait_object);
ChangeThreadState(thread, ThreadStatus(THREADSTATUS_WAIT | (thread->status & THREADSTATUS_SUSPEND)));
}
void WaitCurrentThread_ArbitrateAddress(WaitObject* wait_object, VAddr wait_address) {
WaitCurrentThread_WaitSynchronization(WaitType::WAITTYPE_ARB, wait_object, 0);
WaitCurrentThread_WaitSynchronization(wait_object);
GetCurrentThread()->wait_address = wait_address;
}
@ -287,7 +262,7 @@ void Thread::ReleaseFromWait(WaitObject* wait_object) {
// Iterate through all waiting objects to check availability...
for (auto itr = wait_objects.begin(); itr != wait_objects.end(); ++itr) {
auto res = (*itr)->Wait(false);
auto res = (*itr)->Wait();
if (*res && res.Succeeded())
wait_all_failed = true;
@ -322,9 +297,8 @@ void Thread::ResumeFromWait() {
wait_object->RemoveWaitingThread(this);
wait_objects.clear();
wait_type = WAITTYPE_NONE;
wait_all = false;
if (!(status & (THREADSTATUS_WAITSUSPEND | THREADSTATUS_DORMANT | THREADSTATUS_DEAD))) {
ChangeReadyState(this, true);
}
@ -390,7 +364,6 @@ ResultVal<SharedPtr<Thread>> Thread::Create(std::string name, VAddr entry_point,
thread->stack_size = stack_size;
thread->initial_priority = thread->current_priority = priority;
thread->processor_id = processor_id;
thread->wait_type = WAITTYPE_NONE;
thread->wait_all = false;
thread->wait_objects.clear();
thread->wait_address = 0;
@ -476,8 +449,8 @@ void Reschedule() {
LOG_TRACE(Kernel, "cannot context switch from 0x%08X, no higher priority thread!", prev->GetHandle());
for (auto& thread : thread_list) {
LOG_TRACE(Kernel, "\thandle=0x%08X prio=0x%02X, status=0x%08X wait_type=0x%08X",
thread->GetHandle(), thread->current_priority, thread->status, thread->wait_type);
LOG_TRACE(Kernel, "\thandle=0x%08X prio=0x%02X, status=0x%08X", thread->GetHandle(),
thread->current_priority, thread->status);
}
}
}

View File

@ -38,18 +38,6 @@ enum ThreadStatus {
THREADSTATUS_WAITSUSPEND = THREADSTATUS_WAIT | THREADSTATUS_SUSPEND
};
enum WaitType {
WAITTYPE_NONE,
WAITTYPE_SLEEP,
WAITTYPE_SEMA,
WAITTYPE_EVENT,
WAITTYPE_THREADEND,
WAITTYPE_MUTEX,
WAITTYPE_SYNCH,
WAITTYPE_ARB,
WAITTYPE_TIMER,
};
namespace Kernel {
class Thread : public WaitObject {
@ -70,7 +58,7 @@ public:
inline bool IsSuspended() const { return (status & THREADSTATUS_SUSPEND) != 0; }
inline bool IsIdle() const { return idle; }
ResultVal<bool> Wait(bool wait_thread) override;
ResultVal<bool> Wait() override;
ResultVal<bool> Acquire() override;
s32 GetPriority() const { return current_priority; }
@ -89,12 +77,6 @@ public:
/// Resumes a thread from waiting by marking it as "ready"
void ResumeFromWait();
/**
* Sets the waiting mode of the thread
* @param wait_all If true, wait for all objects, otherwise just wait for the first one
*/
void SetWaitAll(bool wait_all) { this->wait_all = wait_all; }
/**
* Sets the output values after the thread awakens from WaitSynchronization
* @param return_val Value returned
@ -116,9 +98,10 @@ public:
s32 processor_id;
WaitType wait_type;
std::vector<SharedPtr<WaitObject>> wait_objects;
VAddr wait_address;
std::vector<SharedPtr<WaitObject>> wait_objects; ///< Objects that the thread is waiting on
VAddr wait_address; ///< If waiting on an AddressArbiter, this is the arbitration address
bool wait_all; ///< True if the thread is waiting on all objects before resuming
std::string name;
@ -126,7 +109,6 @@ public:
bool idle = false;
private:
bool wait_all = false;
Thread() = default;
};
@ -146,19 +128,15 @@ void ArbitrateAllThreads(WaitObject* arbiter, u32 address);
/// Gets the current thread
Thread* GetCurrentThread();
/**
* Waits the current thread for the given type
* @param wait_type Type of wait
*/
void WaitCurrentThread(WaitType wait_type);
/// Waits the current thread
void WaitCurrentThread();
/**
* Waits the current thread from a WaitSynchronization call
* @param wait_type Type of wait
* @param wait_object Kernel object that we are waiting on
* @param index Index of calling object (for WaitSynchronizationN only)
* @param wait_all If true, wait on all objects before resuming (for WaitSynchronizationN only)
*/
void WaitCurrentThread_WaitSynchronization(WaitType wait_type, WaitObject* wait_object, unsigned index=0);
void WaitCurrentThread_WaitSynchronization(WaitObject* wait_object, bool wait_all=false);
/**
* Waits the current thread from an ArbitrateAddress call
@ -181,6 +159,7 @@ void WakeThreadAfterDelay(Thread* thread, s64 nanoseconds);
* @returns The handle of the idle thread
*/
Handle SetupIdleThread();
/// Initialize threading
void ThreadingInit();

View File

@ -29,13 +29,8 @@ public:
u64 initial_delay; ///< The delay until the timer fires for the first time
u64 interval_delay; ///< The delay until the timer fires after the first time
ResultVal<bool> Wait(bool wait_thread) override {
bool wait = !signaled;
if (wait && wait_thread) {
AddWaitingThread(GetCurrentThread());
Kernel::WaitCurrentThread_WaitSynchronization(WAITTYPE_TIMER, this);
}
return MakeResult<bool>(wait);
ResultVal<bool> Wait() override {
return MakeResult<bool>(!signaled);
}
ResultVal<bool> Acquire() override {

View File

@ -105,7 +105,7 @@ static Result SendSyncRequest(Handle handle) {
ResultVal<bool> wait = session->SyncRequest();
if (wait.Succeeded() && *wait) {
Kernel::WaitCurrentThread(WAITTYPE_SYNCH); // TODO(bunnei): Is this correct?
Kernel::WaitCurrentThread(); // TODO(bunnei): Is this correct?
}
return wait.Code().raw;
@ -120,22 +120,24 @@ static Result CloseHandle(Handle handle) {
/// Wait for a handle to synchronize, timeout after the specified nanoseconds
static Result WaitSynchronization1(Handle handle, s64 nano_seconds) {
SharedPtr<Kernel::Object> object = Kernel::g_handle_table.GetGeneric(handle);
Kernel::WaitObject* object = static_cast<Kernel::WaitObject*>(Kernel::g_handle_table.GetGeneric(handle).get());
if (object == nullptr)
return InvalidHandle(ErrorModule::Kernel).raw;
LOG_TRACE(Kernel_SVC, "called handle=0x%08X(%s:%s), nanoseconds=%lld", handle,
object->GetTypeName().c_str(), object->GetName().c_str(), nano_seconds);
ResultVal<bool> wait = object->Wait(true);
ResultVal<bool> wait = object->Wait();
// Check for next thread to schedule
if (wait.Succeeded() && *wait) {
object->AddWaitingThread(Kernel::GetCurrentThread());
Kernel::WaitCurrentThread_WaitSynchronization(object);
// Create an event to wake the thread up after the specified nanosecond delay has passed
Kernel::WakeThreadAfterDelay(Kernel::GetCurrentThread(), nano_seconds);
Kernel::GetCurrentThread()->SetWaitAll(false);
HLE::Reschedule(__func__);
} else {
object->Acquire();
@ -166,14 +168,15 @@ static Result WaitSynchronizationN(s32* out, Handle* handles, s32 handle_count,
if (handle_count != 0) {
bool selected = false; // True once an object has been selected
for (int i = 0; i < handle_count; ++i) {
SharedPtr<Kernel::Object> object = Kernel::g_handle_table.GetGeneric(handles[i]);
Kernel::WaitObject* object = static_cast<Kernel::WaitObject*>(Kernel::g_handle_table.GetGeneric(handles[i]).get());
if (object == nullptr)
return InvalidHandle(ErrorModule::Kernel).raw;
ResultVal<bool> wait = object->Wait(true);
ResultVal<bool> wait = object->Wait();
// Check if the current thread should wait on this object...
if (wait.Succeeded() && *wait) {
// Check we are waiting on all objects...
if (wait_all)
// Wait the thread
@ -193,15 +196,22 @@ static Result WaitSynchronizationN(s32* out, Handle* handles, s32 handle_count,
// NOTE: This should deadlock the current thread if no timeout was specified
if (!wait_all) {
wait_thread = true;
Kernel::WaitCurrentThread(WAITTYPE_SLEEP);
Kernel::WaitCurrentThread();
}
}
// If thread should wait, then set its state to waiting and then reschedule...
if (wait_thread) {
// Actually wait the current thread on each object if we decided to wait...
for (int i = 0; i < handle_count; ++i) {
auto object = Kernel::g_handle_table.GetWaitObject(handles[i]);
object->AddWaitingThread(Kernel::GetCurrentThread());
Kernel::WaitCurrentThread_WaitSynchronization(object, wait_all);
}
// Create an event to wake the thread up after the specified nanosecond delay has passed
Kernel::WakeThreadAfterDelay(Kernel::GetCurrentThread(), nano_seconds);
Kernel::GetCurrentThread()->SetWaitAll(wait_all);
HLE::Reschedule(__func__);
@ -440,7 +450,7 @@ static void SleepThread(s64 nanoseconds) {
LOG_TRACE(Kernel_SVC, "called nanoseconds=%lld", nanoseconds);
// Sleep current thread and check for next thread to schedule
Kernel::WaitCurrentThread(WAITTYPE_SLEEP);
Kernel::WaitCurrentThread();
// Create an event to wake the thread up after the specified nanosecond delay has passed
Kernel::WakeThreadAfterDelay(Kernel::GetCurrentThread(), nanoseconds);