1
0
Fork 0
forked from suyu/suyu

Kernel: Separate WaitSynchronization into Wait and Acquire methods.

This commit is contained in:
bunnei 2015-01-17 22:23:49 -05:00
parent 627e96fc15
commit aa01c57ae9
8 changed files with 59 additions and 18 deletions

View file

@ -28,7 +28,7 @@ public:
bool signaled; ///< Whether the event has already been signaled
std::string name; ///< Name of event (optional)
ResultVal<bool> WaitSynchronization(unsigned index) override {
ResultVal<bool> Wait(unsigned index) override {
bool wait = !signaled;
if (wait) {
AddWaitingThread(GetCurrentThread());
@ -36,6 +36,10 @@ public:
}
return MakeResult<bool>(wait);
}
ResultVal<bool> Acquire() override {
return MakeResult<bool>(true);
}
};
ResultCode SignalEvent(const Handle handle) {

View file

@ -65,11 +65,20 @@ public:
virtual Kernel::HandleType GetHandleType() const = 0;
/**
* Wait for kernel object to synchronize.
* Wait the current thread for kernel object to synchronize.
* @param index Index of wait object (only applies to WaitSynchronizationN)
* @return True if the current thread should wait as a result of the wait
*/
virtual ResultVal<bool> WaitSynchronization(unsigned index=0) {
virtual ResultVal<bool> Wait(unsigned index = 0) {
LOG_ERROR(Kernel, "(UNIMPLEMENTED)");
return UnimplementedFunction(ErrorModule::Kernel);
}
/**
* Acquire/lock the kernel object if it is available
* @return True if we were able to acquire the kernel object, otherwise false
*/
virtual ResultVal<bool> Acquire() {
LOG_ERROR(Kernel, "(UNIMPLEMENTED)");
return UnimplementedFunction(ErrorModule::Kernel);
}

View file

@ -26,7 +26,8 @@ public:
Handle lock_thread; ///< Handle to thread that currently has mutex
std::string name; ///< Name of mutex (optional)
ResultVal<bool> WaitSynchronization(unsigned index) override;
ResultVal<bool> Wait(unsigned index) override;
ResultVal<bool> Acquire() override;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
@ -155,17 +156,25 @@ Handle CreateMutex(bool initial_locked, const std::string& name) {
return handle;
}
ResultVal<bool> Mutex::WaitSynchronization(unsigned index) {
bool wait = locked;
ResultVal<bool> Mutex::Wait(unsigned index) {
if (locked) {
AddWaitingThread(GetCurrentThread());
Kernel::WaitCurrentThread_WaitSynchronization(WAITTYPE_MUTEX, this, index);
} else {
}
return MakeResult<bool>(locked);
}
ResultVal<bool> Mutex::Acquire() {
bool res = false;
if (!locked) {
// Lock the mutex when the first thread accesses it
locked = true;
res = true;
MutexAcquireLock(this);
}
return MakeResult<bool>(wait);
return MakeResult<bool>(res);
}
} // namespace

View file

@ -32,18 +32,27 @@ public:
return available_count > 0;
}
ResultVal<bool> WaitSynchronization(unsigned index) override {
ResultVal<bool> Wait(unsigned index) override {
bool wait = !IsAvailable();
if (wait) {
Kernel::WaitCurrentThread_WaitSynchronization(WAITTYPE_SEMA, this, index);
AddWaitingThread(GetCurrentThread());
} else {
--available_count;
}
return MakeResult<bool>(wait);
}
ResultVal<bool> Acquire() override {
bool res = false;
if (IsAvailable()) {
--available_count;
res = true;
}
return MakeResult<bool>(res);
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View file

@ -22,7 +22,7 @@
namespace Kernel {
ResultVal<bool> Thread::WaitSynchronization(unsigned index) {
ResultVal<bool> Thread::Wait(unsigned index) {
const bool wait = status != THREADSTATUS_DORMANT;
if (wait) {
AddWaitingThread(GetCurrentThread());
@ -32,6 +32,10 @@ ResultVal<bool> Thread::WaitSynchronization(unsigned index) {
return MakeResult<bool>(wait);
}
ResultVal<bool> Thread::Acquire() {
return MakeResult<bool>(true);
}
// Lists all thread ids that aren't deleted/etc.
static std::vector<SharedPtr<Thread>> thread_list;

View file

@ -70,7 +70,8 @@ public:
inline bool IsSuspended() const { return (status & THREADSTATUS_SUSPEND) != 0; }
inline bool IsIdle() const { return idle; }
ResultVal<bool> WaitSynchronization(unsigned index) override;
ResultVal<bool> Wait(unsigned index) override;
ResultVal<bool> Acquire() override;
s32 GetPriority() const { return current_priority; }
void SetPriority(s32 priority);

View file

@ -29,7 +29,7 @@ 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> WaitSynchronization(unsigned index) override {
ResultVal<bool> Wait(unsigned index) override {
bool wait = !signaled;
if (wait) {
AddWaitingThread(GetCurrentThread());
@ -37,6 +37,10 @@ public:
}
return MakeResult<bool>(wait);
}
ResultVal<bool> Acquire() override {
return MakeResult<bool>(true);
}
};
/**

View file

@ -127,7 +127,7 @@ static Result WaitSynchronization1(Handle handle, s64 nano_seconds) {
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->WaitSynchronization();
ResultVal<bool> wait = object->Wait();
// Check for next thread to schedule
if (wait.Succeeded() && *wait) {
@ -137,6 +137,8 @@ static Result WaitSynchronization1(Handle handle, s64 nano_seconds) {
Kernel::GetCurrentThread()->SetWaitAll(false);
HLE::Reschedule(__func__);
} else {
object->Acquire();
}
return wait.Code().raw;
@ -163,15 +165,14 @@ static Result WaitSynchronizationN(s32* out, Handle* handles, s32 handle_count,
if (object == nullptr)
return InvalidHandle(ErrorModule::Kernel).raw;
ResultVal<bool> wait = object->WaitSynchronization(handle_index);
ResultVal<bool> wait = object->Wait(handle_index);
wait_thread = (wait.Succeeded() && *wait);
// If this object waited and we are waiting on all objects to synchronize
if (wait_thread && wait_all) {
if (wait_thread && wait_all)
// Enforce later on that this thread does not continue
wait_all_succeeded = true;
}
// If this object synchronized and we are not waiting on all objects to synchronize
if (!wait_thread && !wait_all)