From 0135b328edb1b58d1ff97cfdc35e05f02cec1c2a Mon Sep 17 00:00:00 2001
From: Subv <subv2112@gmail.com>
Date: Sun, 12 Aug 2018 17:47:15 -0500
Subject: [PATCH 1/4] Kernel/Thread: Always use the threadsafe option when
 scheduling wakeups.

WakeAfterDelay might be called from any host thread, so err on the side of caution and use the thread-safe CoreTiming::ScheduleEventThreadsafe.

Note that CoreTiming is still far from thread-safe, there may be more things we have to work on for it to be up to par with what we want.
---
 src/core/core_timing.cpp       | 2 --
 src/core/hle/kernel/thread.cpp | 6 ++++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp
index d3bb6f8188..f977d1b325 100644
--- a/src/core/core_timing.cpp
+++ b/src/core/core_timing.cpp
@@ -135,11 +135,9 @@ void ClearPendingEvents() {
 void ScheduleEvent(s64 cycles_into_future, const EventType* event_type, u64 userdata) {
     ASSERT(event_type != nullptr);
     s64 timeout = GetTicks() + cycles_into_future;
-
     // If this event needs to be scheduled before the next advance(), force one early
     if (!is_global_timer_sane)
         ForceExceptionCheck(cycles_into_future);
-
     event_queue.emplace_back(Event{timeout, event_fifo_id++, userdata, event_type});
     std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
 }
diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp
index b9022feae2..e308b9c7bd 100644
--- a/src/core/hle/kernel/thread.cpp
+++ b/src/core/hle/kernel/thread.cpp
@@ -155,8 +155,10 @@ void Thread::WakeAfterDelay(s64 nanoseconds) {
     if (nanoseconds == -1)
         return;
 
-    CoreTiming::ScheduleEvent(CoreTiming::nsToCycles(nanoseconds), ThreadWakeupEventType,
-                              callback_handle);
+    // This function might be called from any thread so we have to be cautious and use the
+    // thread-safe version of ScheduleEvent.
+    CoreTiming::ScheduleEventThreadsafe(CoreTiming::nsToCycles(nanoseconds), ThreadWakeupEventType,
+                                        callback_handle);
 }
 
 void Thread::CancelWakeupTimer() {

From 84b542c386063339c03069b8f4db8221200a27cd Mon Sep 17 00:00:00 2001
From: Subv <subv2112@gmail.com>
Date: Sun, 12 Aug 2018 17:49:55 -0500
Subject: [PATCH 2/4] Kernel/Threads: Lock the HLE mutex when executing the
 wakeup callback.

Another thread might be in the middle of a reschedule, thus altering the state of the schedulers.
---
 src/core/hle/kernel/thread.cpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp
index e308b9c7bd..5f87ff8593 100644
--- a/src/core/hle/kernel/thread.cpp
+++ b/src/core/hle/kernel/thread.cpp
@@ -23,6 +23,7 @@
 #include "core/hle/kernel/object.h"
 #include "core/hle/kernel/process.h"
 #include "core/hle/kernel/thread.h"
+#include "core/hle/lock.h"
 #include "core/hle/result.h"
 #include "core/memory.h"
 
@@ -104,6 +105,10 @@ void ExitCurrentThread() {
  */
 static void ThreadWakeupCallback(u64 thread_handle, int cycles_late) {
     const auto proper_handle = static_cast<Handle>(thread_handle);
+
+    // Lock the global kernel mutex when we enter the kernel HLE.
+    std::lock_guard<std::recursive_mutex> lock(HLE::g_hle_lock);
+
     SharedPtr<Thread> thread = wakeup_callback_handle_table.Get<Thread>(proper_handle);
     if (thread == nullptr) {
         LOG_CRITICAL(Kernel, "Callback fired for invalid thread {:08X}", proper_handle);

From 3a338d9286bb7c8a4e169914965e6241c466bc6a Mon Sep 17 00:00:00 2001
From: Subv <subv2112@gmail.com>
Date: Sun, 12 Aug 2018 17:50:44 -0500
Subject: [PATCH 3/4] CPU/HLE: Lock the HLE mutex before performing a
 reschedule.

Another thread might be in the middle of an SVC, thus altering the state of the schedulers.
---
 src/core/core_cpu.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/core/core_cpu.cpp b/src/core/core_cpu.cpp
index 46a522fcd4..9b306faf6a 100644
--- a/src/core/core_cpu.cpp
+++ b/src/core/core_cpu.cpp
@@ -14,6 +14,7 @@
 #include "core/core_timing.h"
 #include "core/hle/kernel/scheduler.h"
 #include "core/hle/kernel/thread.h"
+#include "core/hle/lock.h"
 #include "core/settings.h"
 
 namespace Core {
@@ -125,6 +126,8 @@ void Cpu::Reschedule() {
     }
 
     reschedule_pending = false;
+    // Lock the global kernel mutex when we manipulate the HLE state
+    std::lock_guard<std::recursive_mutex> lock(HLE::g_hle_lock);
     scheduler->Reschedule();
 }
 

From 2e7802ad7d4ab80d9547c3ee44ad3a1341dea625 Mon Sep 17 00:00:00 2001
From: Subv <subv2112@gmail.com>
Date: Sun, 12 Aug 2018 17:51:47 -0500
Subject: [PATCH 4/4] Core/HLE: Make  the 'reschedule_pending' flag atomic.

Another thread may write to this variable while the core in question is in the middle of checking for a reschedule request.
---
 src/core/core_cpu.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/core/core_cpu.h b/src/core/core_cpu.h
index 9769529033..56cdae1947 100644
--- a/src/core/core_cpu.h
+++ b/src/core/core_cpu.h
@@ -79,7 +79,7 @@ private:
     std::shared_ptr<CpuBarrier> cpu_barrier;
     std::shared_ptr<Kernel::Scheduler> scheduler;
 
-    bool reschedule_pending{};
+    std::atomic<bool> reschedule_pending = false;
     size_t core_index;
 };