2020-02-11 13:36:39 -08:00
|
|
|
// Copyright 2020 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include "core/core.h"
|
|
|
|
#include "core/hle/kernel/errors.h"
|
2020-02-13 13:01:44 -08:00
|
|
|
#include "core/hle/kernel/handle_table.h"
|
2020-02-11 13:36:39 -08:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
|
|
|
#include "core/hle/kernel/scheduler.h"
|
|
|
|
#include "core/hle/kernel/synchronization.h"
|
|
|
|
#include "core/hle/kernel/synchronization_object.h"
|
|
|
|
#include "core/hle/kernel/thread.h"
|
2020-02-25 12:38:33 -08:00
|
|
|
#include "core/hle/kernel/time_manager.h"
|
2020-02-11 13:36:39 -08:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
Synchronization::Synchronization(Core::System& system) : system{system} {}
|
|
|
|
|
|
|
|
void Synchronization::SignalObject(SynchronizationObject& obj) const {
|
2020-02-26 18:26:53 -08:00
|
|
|
auto& kernel = system.Kernel();
|
|
|
|
SchedulerLock lock(kernel);
|
2020-02-11 13:36:39 -08:00
|
|
|
if (obj.IsSignaled()) {
|
2020-02-25 12:38:33 -08:00
|
|
|
for (auto thread : obj.GetWaitingThreads()) {
|
|
|
|
if (thread->GetSchedulingStatus() == ThreadSchedStatus::Paused) {
|
2020-03-09 07:51:05 -07:00
|
|
|
if (thread->GetStatus() != ThreadStatus::WaitHLEEvent) {
|
|
|
|
ASSERT(thread->GetStatus() == ThreadStatus::WaitSynch);
|
|
|
|
ASSERT(thread->IsWaitingSync());
|
|
|
|
}
|
2020-02-25 12:38:33 -08:00
|
|
|
thread->SetSynchronizationResults(&obj, RESULT_SUCCESS);
|
|
|
|
thread->ResumeFromWait();
|
|
|
|
}
|
|
|
|
}
|
2020-03-03 09:02:50 -08:00
|
|
|
obj.ClearWaitingThreads();
|
2020-02-13 13:01:44 -08:00
|
|
|
}
|
2020-02-11 13:36:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<ResultCode, Handle> Synchronization::WaitFor(
|
|
|
|
std::vector<std::shared_ptr<SynchronizationObject>>& sync_objects, s64 nano_seconds) {
|
2020-02-25 12:38:33 -08:00
|
|
|
auto& kernel = system.Kernel();
|
2020-02-11 13:36:39 -08:00
|
|
|
auto* const thread = system.CurrentScheduler().GetCurrentThread();
|
2020-02-25 12:38:33 -08:00
|
|
|
Handle event_handle = InvalidHandle;
|
|
|
|
{
|
|
|
|
SchedulerLockAndSleep lock(kernel, event_handle, thread, nano_seconds);
|
|
|
|
const auto itr =
|
|
|
|
std::find_if(sync_objects.begin(), sync_objects.end(),
|
|
|
|
[thread](const std::shared_ptr<SynchronizationObject>& object) {
|
|
|
|
return object->IsSignaled();
|
|
|
|
});
|
|
|
|
|
|
|
|
if (itr != sync_objects.end()) {
|
|
|
|
// We found a ready object, acquire it and set the result value
|
|
|
|
SynchronizationObject* object = itr->get();
|
|
|
|
object->Acquire(thread);
|
2020-10-15 11:49:45 -07:00
|
|
|
const auto index = static_cast<u32>(std::distance(sync_objects.begin(), itr));
|
2020-02-25 12:38:33 -08:00
|
|
|
lock.CancelSleep();
|
|
|
|
return {RESULT_SUCCESS, index};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nano_seconds == 0) {
|
|
|
|
lock.CancelSleep();
|
|
|
|
return {RESULT_TIMEOUT, InvalidHandle};
|
|
|
|
}
|
|
|
|
|
2020-03-07 09:37:47 -08:00
|
|
|
if (thread->IsPendingTermination()) {
|
|
|
|
lock.CancelSleep();
|
|
|
|
return {ERR_THREAD_TERMINATING, InvalidHandle};
|
|
|
|
}
|
2020-02-25 12:38:33 -08:00
|
|
|
|
|
|
|
if (thread->IsSyncCancelled()) {
|
|
|
|
thread->SetSyncCancelled(false);
|
|
|
|
lock.CancelSleep();
|
|
|
|
return {ERR_SYNCHRONIZATION_CANCELED, InvalidHandle};
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& object : sync_objects) {
|
|
|
|
object->AddWaitingThread(SharedFrom(thread));
|
|
|
|
}
|
2020-03-03 09:37:11 -08:00
|
|
|
|
|
|
|
thread->SetSynchronizationObjects(&sync_objects);
|
2020-02-25 12:38:33 -08:00
|
|
|
thread->SetSynchronizationResults(nullptr, RESULT_TIMEOUT);
|
|
|
|
thread->SetStatus(ThreadStatus::WaitSynch);
|
2020-03-07 06:24:46 -08:00
|
|
|
thread->SetWaitingSync(true);
|
2020-02-11 13:36:39 -08:00
|
|
|
}
|
2020-03-07 06:24:46 -08:00
|
|
|
thread->SetWaitingSync(false);
|
2020-02-11 13:36:39 -08:00
|
|
|
|
2020-02-25 12:38:33 -08:00
|
|
|
if (event_handle != InvalidHandle) {
|
|
|
|
auto& time_manager = kernel.TimeManager();
|
|
|
|
time_manager.UnscheduleTimeEvent(event_handle);
|
2020-02-11 13:36:39 -08:00
|
|
|
}
|
|
|
|
|
2020-02-25 12:38:33 -08:00
|
|
|
{
|
|
|
|
SchedulerLock lock(kernel);
|
|
|
|
ResultCode signaling_result = thread->GetSignalingResult();
|
|
|
|
SynchronizationObject* signaling_object = thread->GetSignalingObject();
|
2020-03-03 09:37:11 -08:00
|
|
|
thread->SetSynchronizationObjects(nullptr);
|
2020-03-08 09:51:24 -07:00
|
|
|
auto shared_thread = SharedFrom(thread);
|
2020-02-26 18:26:53 -08:00
|
|
|
for (auto& obj : sync_objects) {
|
2020-03-08 09:51:24 -07:00
|
|
|
obj->RemoveWaitingThread(shared_thread);
|
2020-02-26 18:26:53 -08:00
|
|
|
}
|
2020-03-08 09:51:24 -07:00
|
|
|
if (signaling_object != nullptr) {
|
2020-02-25 12:38:33 -08:00
|
|
|
const auto itr = std::find_if(
|
|
|
|
sync_objects.begin(), sync_objects.end(),
|
|
|
|
[signaling_object](const std::shared_ptr<SynchronizationObject>& object) {
|
|
|
|
return object.get() == signaling_object;
|
|
|
|
});
|
|
|
|
ASSERT(itr != sync_objects.end());
|
|
|
|
signaling_object->Acquire(thread);
|
2020-10-15 11:49:45 -07:00
|
|
|
const auto index = static_cast<u32>(std::distance(sync_objects.begin(), itr));
|
2020-03-08 09:51:24 -07:00
|
|
|
return {signaling_result, index};
|
2020-02-25 12:38:33 -08:00
|
|
|
}
|
|
|
|
return {signaling_result, -1};
|
2020-02-11 13:36:39 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Kernel
|