mirror of
https://github.com/Lime3DS/Lime3DS.git
synced 2024-11-14 05:55:13 +01:00
Kernel: Get rid of WaitTypes and simplify lots of code, removing hacks.
This commit is contained in:
parent
6deb1a0119
commit
e5a9f1c644
@ -28,13 +28,8 @@ public:
|
|||||||
bool signaled; ///< Whether the event has already been signaled
|
bool signaled; ///< Whether the event has already been signaled
|
||||||
std::string name; ///< Name of event (optional)
|
std::string name; ///< Name of event (optional)
|
||||||
|
|
||||||
ResultVal<bool> Wait(bool wait_thread) override {
|
ResultVal<bool> Wait() override {
|
||||||
bool wait = !signaled;
|
return MakeResult<bool>(!signaled);
|
||||||
if (wait && wait_thread) {
|
|
||||||
AddWaitingThread(GetCurrentThread());
|
|
||||||
Kernel::WaitCurrentThread_WaitSynchronization(WAITTYPE_EVENT, this);
|
|
||||||
}
|
|
||||||
return MakeResult<bool>(wait);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultVal<bool> Acquire() override {
|
ResultVal<bool> Acquire() override {
|
||||||
|
@ -42,13 +42,15 @@ Thread* WaitObject::ReleaseNextThread() {
|
|||||||
return next_thread.get();
|
return next_thread.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WaitObject::ReleaseAllWaitingThreads() {
|
void WaitObject::WakeupAllWaitingThreads() {
|
||||||
auto waiting_threads_copy = waiting_threads;
|
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)
|
for (auto thread : waiting_threads_copy)
|
||||||
thread->ReleaseWaitObject(this);
|
thread->ReleaseWaitObject(this);
|
||||||
|
|
||||||
waiting_threads.clear();
|
_assert_msg_(Kernel, waiting_threads.empty(), "failed to awaken all waiting threads!");
|
||||||
}
|
}
|
||||||
|
|
||||||
HandleTable::HandleTable() {
|
HandleTable::HandleTable() {
|
||||||
|
@ -65,11 +65,10 @@ public:
|
|||||||
virtual Kernel::HandleType GetHandleType() const = 0;
|
virtual Kernel::HandleType GetHandleType() const = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if this object is available, (optionally) wait the current thread if not
|
* Check if this object is available
|
||||||
* @param wait_thread If true, wait the current thread if this object is unavailable
|
|
||||||
* @return True if the current thread should wait due to this object being unavailable
|
* @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)");
|
LOG_ERROR(Kernel, "(UNIMPLEMENTED)");
|
||||||
return UnimplementedFunction(ErrorModule::Kernel);
|
return UnimplementedFunction(ErrorModule::Kernel);
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ public:
|
|||||||
Handle lock_thread; ///< Handle to thread that currently has mutex
|
Handle lock_thread; ///< Handle to thread that currently has mutex
|
||||||
std::string name; ///< Name of mutex (optional)
|
std::string name; ///< Name of mutex (optional)
|
||||||
|
|
||||||
ResultVal<bool> Wait(bool wait_thread) override;
|
ResultVal<bool> Wait() override;
|
||||||
ResultVal<bool> Acquire() override;
|
ResultVal<bool> Acquire() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -156,12 +156,7 @@ Handle CreateMutex(bool initial_locked, const std::string& name) {
|
|||||||
return handle;
|
return handle;
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultVal<bool> Mutex::Wait(bool wait_thread) {
|
ResultVal<bool> Mutex::Wait() {
|
||||||
if (locked && wait_thread) {
|
|
||||||
AddWaitingThread(GetCurrentThread());
|
|
||||||
Kernel::WaitCurrentThread_WaitSynchronization(WAITTYPE_MUTEX, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
return MakeResult<bool>(locked);
|
return MakeResult<bool>(locked);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,15 +32,8 @@ public:
|
|||||||
return available_count > 0;
|
return available_count > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultVal<bool> Wait(bool wait_thread) override {
|
ResultVal<bool> Wait() override {
|
||||||
bool wait = !IsAvailable();
|
return MakeResult<bool>(!IsAvailable());
|
||||||
|
|
||||||
if (wait && wait_thread) {
|
|
||||||
Kernel::WaitCurrentThread_WaitSynchronization(WAITTYPE_SEMA, this);
|
|
||||||
AddWaitingThread(GetCurrentThread());
|
|
||||||
}
|
|
||||||
|
|
||||||
return MakeResult<bool>(wait);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultVal<bool> Acquire() override {
|
ResultVal<bool> Acquire() override {
|
||||||
|
@ -22,14 +22,8 @@
|
|||||||
|
|
||||||
namespace Kernel {
|
namespace Kernel {
|
||||||
|
|
||||||
ResultVal<bool> Thread::Wait(bool wait_thread) {
|
ResultVal<bool> Thread::Wait() {
|
||||||
const bool wait = status != THREADSTATUS_DORMANT;
|
return MakeResult<bool>(status != THREADSTATUS_DORMANT);
|
||||||
if (wait && wait_thread) {
|
|
||||||
AddWaitingThread(GetCurrentThread());
|
|
||||||
WaitCurrentThread_WaitSynchronization(WAITTYPE_THREADEND, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
return MakeResult<bool>(wait);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultVal<bool> Thread::Acquire() {
|
ResultVal<bool> Thread::Acquire() {
|
||||||
@ -68,7 +62,7 @@ static void ResetThread(Thread* t, u32 arg, s32 lowest_priority) {
|
|||||||
if (t->current_priority < lowest_priority) {
|
if (t->current_priority < lowest_priority) {
|
||||||
t->current_priority = t->initial_priority;
|
t->current_priority = t->initial_priority;
|
||||||
}
|
}
|
||||||
t->wait_type = WAITTYPE_NONE;
|
|
||||||
t->wait_objects.clear();
|
t->wait_objects.clear();
|
||||||
t->wait_address = 0;
|
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
|
/// Check if a thread is blocking on a the specified object
|
||||||
static bool CheckWaitType(const Thread* thread, WaitType type) {
|
static bool CheckWaitType(const Thread* thread, Object* wait_object) {
|
||||||
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) {
|
|
||||||
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 CheckWaitType(thread, type);
|
return (thread->IsWaiting());
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if a thread is blocking on a specified wait type with a specified handle and address
|
/// Check if a thread is blocking on a the specified object and an address
|
||||||
static bool CheckWaitType(const Thread* thread, WaitType type, Object* wait_object, VAddr wait_address) {
|
static bool CheckWaitType(const Thread* thread, Object* wait_object, VAddr wait_address) {
|
||||||
return CheckWaitType(thread, type, wait_object) && (wait_address == thread->wait_address);
|
return CheckWaitType(thread, wait_object) && (wait_address == thread->wait_address);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Stops the current thread
|
/// Stops the current thread
|
||||||
@ -118,7 +107,6 @@ void Thread::Stop(const char* reason) {
|
|||||||
ReleaseAllWaitingThreads();
|
ReleaseAllWaitingThreads();
|
||||||
|
|
||||||
// Stopped threads are never waiting.
|
// Stopped threads are never waiting.
|
||||||
wait_type = WAITTYPE_NONE;
|
|
||||||
wait_objects.clear();
|
wait_objects.clear();
|
||||||
wait_address = 0;
|
wait_address = 0;
|
||||||
}
|
}
|
||||||
@ -130,12 +118,6 @@ static void ChangeThreadState(Thread* t, ThreadStatus new_status) {
|
|||||||
}
|
}
|
||||||
ChangeReadyState(t, (new_status & THREADSTATUS_READY) != 0);
|
ChangeReadyState(t, (new_status & THREADSTATUS_READY) != 0);
|
||||||
t->status = new_status;
|
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
|
/// 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...
|
// 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(), WAITTYPE_ARB, arbiter, address))
|
if (!CheckWaitType(thread.get(), arbiter, address))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (thread == nullptr)
|
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...
|
// 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(), WAITTYPE_ARB, arbiter, address))
|
if (CheckWaitType(thread.get(), arbiter, address))
|
||||||
thread->ReleaseFromWait(arbiter);
|
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)
|
/// Calls a thread by marking it as "ready" (note: will not actually execute until current thread yields)
|
||||||
static void CallThread(Thread* t) {
|
static void CallThread(Thread* t) {
|
||||||
// Stop waiting
|
// Stop waiting
|
||||||
if (t->wait_type != WAITTYPE_NONE) {
|
|
||||||
t->wait_type = WAITTYPE_NONE;
|
|
||||||
}
|
|
||||||
ChangeThreadState(t, THREADSTATUS_READY);
|
ChangeThreadState(t, THREADSTATUS_READY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -201,7 +180,6 @@ static void SwitchContext(Thread* t) {
|
|||||||
current_thread = t;
|
current_thread = t;
|
||||||
ChangeReadyState(t, false);
|
ChangeReadyState(t, false);
|
||||||
t->status = (t->status | THREADSTATUS_RUNNING) & ~THREADSTATUS_READY;
|
t->status = (t->status | THREADSTATUS_RUNNING) & ~THREADSTATUS_READY;
|
||||||
t->wait_type = WAITTYPE_NONE;
|
|
||||||
Core::g_app_core->LoadContext(t->context);
|
Core::g_app_core->LoadContext(t->context);
|
||||||
} else {
|
} else {
|
||||||
current_thread = nullptr;
|
current_thread = nullptr;
|
||||||
@ -224,23 +202,20 @@ static Thread* NextThread() {
|
|||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WaitCurrentThread(WaitType wait_type) {
|
void WaitCurrentThread() {
|
||||||
Thread* thread = GetCurrentThread();
|
Thread* thread = GetCurrentThread();
|
||||||
thread->wait_type = wait_type;
|
|
||||||
ChangeThreadState(thread, ThreadStatus(THREADSTATUS_WAIT | (thread->status & THREADSTATUS_SUSPEND)));
|
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* thread = GetCurrentThread();
|
||||||
thread->wait_type = wait_type;
|
thread->wait_all = wait_all;
|
||||||
|
|
||||||
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(WaitObject* wait_object, VAddr wait_address) {
|
||||||
WaitCurrentThread_WaitSynchronization(WaitType::WAITTYPE_ARB, wait_object, 0);
|
WaitCurrentThread_WaitSynchronization(wait_object);
|
||||||
GetCurrentThread()->wait_address = wait_address;
|
GetCurrentThread()->wait_address = wait_address;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -287,7 +262,7 @@ void Thread::ReleaseFromWait(WaitObject* wait_object) {
|
|||||||
|
|
||||||
// Iterate through all waiting objects to check availability...
|
// Iterate through all waiting objects to check availability...
|
||||||
for (auto itr = wait_objects.begin(); itr != wait_objects.end(); ++itr) {
|
for (auto itr = wait_objects.begin(); itr != wait_objects.end(); ++itr) {
|
||||||
auto res = (*itr)->Wait(false);
|
auto res = (*itr)->Wait();
|
||||||
|
|
||||||
if (*res && res.Succeeded())
|
if (*res && res.Succeeded())
|
||||||
wait_all_failed = true;
|
wait_all_failed = true;
|
||||||
@ -322,9 +297,8 @@ void Thread::ResumeFromWait() {
|
|||||||
wait_object->RemoveWaitingThread(this);
|
wait_object->RemoveWaitingThread(this);
|
||||||
|
|
||||||
wait_objects.clear();
|
wait_objects.clear();
|
||||||
|
|
||||||
wait_type = WAITTYPE_NONE;
|
|
||||||
wait_all = false;
|
wait_all = false;
|
||||||
|
|
||||||
if (!(status & (THREADSTATUS_WAITSUSPEND | THREADSTATUS_DORMANT | THREADSTATUS_DEAD))) {
|
if (!(status & (THREADSTATUS_WAITSUSPEND | THREADSTATUS_DORMANT | THREADSTATUS_DEAD))) {
|
||||||
ChangeReadyState(this, true);
|
ChangeReadyState(this, true);
|
||||||
}
|
}
|
||||||
@ -390,7 +364,6 @@ ResultVal<SharedPtr<Thread>> Thread::Create(std::string name, VAddr entry_point,
|
|||||||
thread->stack_size = stack_size;
|
thread->stack_size = stack_size;
|
||||||
thread->initial_priority = thread->current_priority = priority;
|
thread->initial_priority = thread->current_priority = priority;
|
||||||
thread->processor_id = processor_id;
|
thread->processor_id = processor_id;
|
||||||
thread->wait_type = WAITTYPE_NONE;
|
|
||||||
thread->wait_all = false;
|
thread->wait_all = false;
|
||||||
thread->wait_objects.clear();
|
thread->wait_objects.clear();
|
||||||
thread->wait_address = 0;
|
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());
|
LOG_TRACE(Kernel, "cannot context switch from 0x%08X, no higher priority thread!", prev->GetHandle());
|
||||||
|
|
||||||
for (auto& thread : thread_list) {
|
for (auto& thread : thread_list) {
|
||||||
LOG_TRACE(Kernel, "\thandle=0x%08X prio=0x%02X, status=0x%08X wait_type=0x%08X",
|
LOG_TRACE(Kernel, "\thandle=0x%08X prio=0x%02X, status=0x%08X", thread->GetHandle(),
|
||||||
thread->GetHandle(), thread->current_priority, thread->status, thread->wait_type);
|
thread->current_priority, thread->status);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,18 +38,6 @@ enum ThreadStatus {
|
|||||||
THREADSTATUS_WAITSUSPEND = THREADSTATUS_WAIT | THREADSTATUS_SUSPEND
|
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 {
|
namespace Kernel {
|
||||||
|
|
||||||
class Thread : public WaitObject {
|
class Thread : public WaitObject {
|
||||||
@ -70,7 +58,7 @@ public:
|
|||||||
inline bool IsSuspended() const { return (status & THREADSTATUS_SUSPEND) != 0; }
|
inline bool IsSuspended() const { return (status & THREADSTATUS_SUSPEND) != 0; }
|
||||||
inline bool IsIdle() const { return idle; }
|
inline bool IsIdle() const { return idle; }
|
||||||
|
|
||||||
ResultVal<bool> Wait(bool wait_thread) override;
|
ResultVal<bool> Wait() override;
|
||||||
ResultVal<bool> Acquire() override;
|
ResultVal<bool> Acquire() override;
|
||||||
|
|
||||||
s32 GetPriority() const { return current_priority; }
|
s32 GetPriority() const { return current_priority; }
|
||||||
@ -89,12 +77,6 @@ public:
|
|||||||
/// Resumes a thread from waiting by marking it as "ready"
|
/// Resumes a thread from waiting by marking it as "ready"
|
||||||
void ResumeFromWait();
|
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
|
* Sets the output values after the thread awakens from WaitSynchronization
|
||||||
* @param return_val Value returned
|
* @param return_val Value returned
|
||||||
@ -116,9 +98,10 @@ public:
|
|||||||
|
|
||||||
s32 processor_id;
|
s32 processor_id;
|
||||||
|
|
||||||
WaitType wait_type;
|
std::vector<SharedPtr<WaitObject>> wait_objects; ///< Objects that the thread is waiting on
|
||||||
std::vector<SharedPtr<WaitObject>> wait_objects;
|
|
||||||
VAddr wait_address;
|
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;
|
std::string name;
|
||||||
|
|
||||||
@ -126,7 +109,6 @@ public:
|
|||||||
bool idle = false;
|
bool idle = false;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool wait_all = false;
|
|
||||||
|
|
||||||
Thread() = default;
|
Thread() = default;
|
||||||
};
|
};
|
||||||
@ -146,19 +128,15 @@ void ArbitrateAllThreads(WaitObject* arbiter, u32 address);
|
|||||||
/// Gets the current thread
|
/// Gets the current thread
|
||||||
Thread* GetCurrentThread();
|
Thread* GetCurrentThread();
|
||||||
|
|
||||||
/**
|
/// Waits the current thread
|
||||||
* Waits the current thread for the given type
|
void WaitCurrentThread();
|
||||||
* @param wait_type Type of wait
|
|
||||||
*/
|
|
||||||
void WaitCurrentThread(WaitType wait_type);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Waits the current thread from a WaitSynchronization call
|
* 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 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
|
* 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
|
* @returns The handle of the idle thread
|
||||||
*/
|
*/
|
||||||
Handle SetupIdleThread();
|
Handle SetupIdleThread();
|
||||||
|
|
||||||
/// Initialize threading
|
/// Initialize threading
|
||||||
void ThreadingInit();
|
void ThreadingInit();
|
||||||
|
|
||||||
|
@ -29,13 +29,8 @@ public:
|
|||||||
u64 initial_delay; ///< The delay until the timer fires for the first time
|
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
|
u64 interval_delay; ///< The delay until the timer fires after the first time
|
||||||
|
|
||||||
ResultVal<bool> Wait(bool wait_thread) override {
|
ResultVal<bool> Wait() override {
|
||||||
bool wait = !signaled;
|
return MakeResult<bool>(!signaled);
|
||||||
if (wait && wait_thread) {
|
|
||||||
AddWaitingThread(GetCurrentThread());
|
|
||||||
Kernel::WaitCurrentThread_WaitSynchronization(WAITTYPE_TIMER, this);
|
|
||||||
}
|
|
||||||
return MakeResult<bool>(wait);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultVal<bool> Acquire() override {
|
ResultVal<bool> Acquire() override {
|
||||||
|
@ -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(WAITTYPE_SYNCH); // TODO(bunnei): Is this correct?
|
Kernel::WaitCurrentThread(); // TODO(bunnei): Is this correct?
|
||||||
}
|
}
|
||||||
|
|
||||||
return wait.Code().raw;
|
return wait.Code().raw;
|
||||||
@ -120,22 +120,24 @@ static Result CloseHandle(Handle handle) {
|
|||||||
|
|
||||||
/// Wait for a handle to synchronize, timeout after the specified nanoseconds
|
/// Wait for a handle to synchronize, timeout after the specified nanoseconds
|
||||||
static Result WaitSynchronization1(Handle handle, s64 nano_seconds) {
|
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)
|
if (object == nullptr)
|
||||||
return InvalidHandle(ErrorModule::Kernel).raw;
|
return InvalidHandle(ErrorModule::Kernel).raw;
|
||||||
|
|
||||||
LOG_TRACE(Kernel_SVC, "called handle=0x%08X(%s:%s), nanoseconds=%lld", handle,
|
LOG_TRACE(Kernel_SVC, "called handle=0x%08X(%s:%s), nanoseconds=%lld", handle,
|
||||||
object->GetTypeName().c_str(), object->GetName().c_str(), nano_seconds);
|
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
|
// Check for next thread to schedule
|
||||||
if (wait.Succeeded() && *wait) {
|
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
|
// Create an event to wake the thread up after the specified nanosecond delay has passed
|
||||||
Kernel::WakeThreadAfterDelay(Kernel::GetCurrentThread(), nano_seconds);
|
Kernel::WakeThreadAfterDelay(Kernel::GetCurrentThread(), nano_seconds);
|
||||||
|
|
||||||
Kernel::GetCurrentThread()->SetWaitAll(false);
|
|
||||||
|
|
||||||
HLE::Reschedule(__func__);
|
HLE::Reschedule(__func__);
|
||||||
} else {
|
} else {
|
||||||
object->Acquire();
|
object->Acquire();
|
||||||
@ -166,14 +168,15 @@ static Result WaitSynchronizationN(s32* out, Handle* handles, s32 handle_count,
|
|||||||
if (handle_count != 0) {
|
if (handle_count != 0) {
|
||||||
bool selected = false; // True once an object has been selected
|
bool selected = false; // True once an object has been selected
|
||||||
for (int i = 0; i < handle_count; ++i) {
|
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)
|
if (object == nullptr)
|
||||||
return InvalidHandle(ErrorModule::Kernel).raw;
|
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...
|
// Check if the current thread should wait on this object...
|
||||||
if (wait.Succeeded() && *wait) {
|
if (wait.Succeeded() && *wait) {
|
||||||
|
|
||||||
// Check we are waiting on all objects...
|
// Check we are waiting on all objects...
|
||||||
if (wait_all)
|
if (wait_all)
|
||||||
// Wait the thread
|
// 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
|
// 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(WAITTYPE_SLEEP);
|
Kernel::WaitCurrentThread();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If thread should wait, then set its state to waiting and then reschedule...
|
// If thread should wait, then set its state to waiting and then reschedule...
|
||||||
if (wait_thread) {
|
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
|
// Create an event to wake the thread up after the specified nanosecond delay has passed
|
||||||
Kernel::WakeThreadAfterDelay(Kernel::GetCurrentThread(), nano_seconds);
|
Kernel::WakeThreadAfterDelay(Kernel::GetCurrentThread(), nano_seconds);
|
||||||
Kernel::GetCurrentThread()->SetWaitAll(wait_all);
|
|
||||||
|
|
||||||
HLE::Reschedule(__func__);
|
HLE::Reschedule(__func__);
|
||||||
|
|
||||||
@ -440,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(WAITTYPE_SLEEP);
|
Kernel::WaitCurrentThread();
|
||||||
|
|
||||||
// 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);
|
||||||
|
Loading…
Reference in New Issue
Block a user