diff --git a/src/xenia/apu/audio_system.cc b/src/xenia/apu/audio_system.cc index 9fb13accf..97f692c30 100644 --- a/src/xenia/apu/audio_system.cc +++ b/src/xenia/apu/audio_system.cc @@ -15,6 +15,7 @@ #include "xenia/base/math.h" #include "xenia/base/ring_buffer.h" #include "xenia/base/string_buffer.h" +#include "xenia/base/threading.h" #include "xenia/cpu/processor.h" #include "xenia/cpu/thread_state.h" #include "xenia/emulator.h" @@ -147,7 +148,7 @@ void AudioSystem::WorkerThreadMain() { if (!pumped) { SCOPE_profile_cpu_i("apu", "Sleep"); - Sleep(500); + xe::threading::Sleep(std::chrono::milliseconds::duration(500)); } } worker_running_ = false; diff --git a/src/xenia/base/threading.h b/src/xenia/base/threading.h index 345dc14ab..417b05fee 100644 --- a/src/xenia/base/threading.h +++ b/src/xenia/base/threading.h @@ -57,6 +57,9 @@ void set_name(std::thread::native_handle_type handle, const std::string& name); // Yields the current thread to the scheduler. Maybe. void MaybeYield(); +// Memory barrier (request - may be ignored). +void SyncMemory(); + // Sleeps the current thread for at least as long as the given duration. void Sleep(std::chrono::microseconds duration); template diff --git a/src/xenia/base/threading_win.cc b/src/xenia/base/threading_win.cc index 1a7019e28..61315d6bf 100644 --- a/src/xenia/base/threading_win.cc +++ b/src/xenia/base/threading_win.cc @@ -52,11 +52,16 @@ void set_name(std::thread::native_handle_type handle, const std::string& name) { set_name(GetThreadId(handle), name); } -void MaybeYield() { SwitchToThread(); } +void MaybeYield() { + SwitchToThread(); + MemoryBarrier(); +} + +void SyncMemory() { MemoryBarrier(); } void Sleep(std::chrono::microseconds duration) { if (duration.count() < 100) { - SwitchToThread(); + MaybeYield(); } else { ::Sleep(static_cast(duration.count() / 1000)); } diff --git a/src/xenia/gpu/gl4/command_processor.cc b/src/xenia/gpu/gl4/command_processor.cc index c75d2ede4..494f9bad9 100644 --- a/src/xenia/gpu/gl4/command_processor.cc +++ b/src/xenia/gpu/gl4/command_processor.cc @@ -201,8 +201,7 @@ void CommandProcessor::WorkerThreadMain() { // It'll keep us from burning power. // const int wait_time_ms = 5; // WaitForSingleObject(write_ptr_index_event_, wait_time_ms); - SwitchToThread(); - MemoryBarrier(); + xe::threading::MaybeYield(); write_ptr_index = write_ptr_index_.load(); } while (worker_running_ && pending_fns_.empty() && (write_ptr_index == 0xBAADF00D || @@ -1104,14 +1103,15 @@ bool CommandProcessor::ExecutePacketType3_WAIT_REG_MEM(RingbufferReader* reader, PrepareForWait(); if (!FLAGS_vsync) { // User wants it fast and dangerous. - SwitchToThread(); + xe::threading::MaybeYield(); } else { - Sleep(wait / 0x100); + xe::threading::Sleep( + std::chrono::milliseconds::duration(wait / 0x100)); } - MemoryBarrier(); + xe::threading::SyncMemory(); ReturnFromWait(); } else { - SwitchToThread(); + xe::threading::MaybeYield(); } } } while (!matched); diff --git a/src/xenia/gpu/gl4/gl4_graphics_system.cc b/src/xenia/gpu/gl4/gl4_graphics_system.cc index 37955a96a..b72279cd9 100644 --- a/src/xenia/gpu/gl4/gl4_graphics_system.cc +++ b/src/xenia/gpu/gl4/gl4_graphics_system.cc @@ -125,7 +125,7 @@ X_STATUS GL4GraphicsSystem::Setup(cpu::Processor* processor, MarkVblank(); last_frame_time = current_time; } - Sleep(1); + xe::threading::Sleep(std::chrono::milliseconds::duration(1)); } return 0; })); diff --git a/src/xenia/kernel/apps/xmp_app.cc b/src/xenia/kernel/apps/xmp_app.cc index e8459eb69..98c54c5d9 100644 --- a/src/xenia/kernel/apps/xmp_app.cc +++ b/src/xenia/kernel/apps/xmp_app.cc @@ -32,7 +32,7 @@ XXMPApp::XXMPApp(KernelState* kernel_state) X_RESULT XXMPApp::XMPGetStatus(uint32_t state_ptr) { // Some stupid games will hammer this on a thread - induce a delay // here to keep from starving real threads. - Sleep(1); + xe::threading::Sleep(std::chrono::milliseconds::duration(1)); XELOGD("XMPGetStatus(%.8X)", state_ptr); xe::store_and_swap(memory_->TranslateVirtual(state_ptr), diff --git a/src/xenia/kernel/xboxkrnl_audio_xma.cc b/src/xenia/kernel/xboxkrnl_audio_xma.cc index 013f95afa..0cbadf113 100644 --- a/src/xenia/kernel/xboxkrnl_audio_xma.cc +++ b/src/xenia/kernel/xboxkrnl_audio_xma.cc @@ -345,7 +345,7 @@ SHIM_CALL XMABlockWhileInUse_shim(PPCContext* ppc_context, if (!context.input_buffer_0_valid && !context.input_buffer_1_valid) { break; } - Sleep(1); + xe::threading::Sleep(std::chrono::milliseconds::duration(1)); } while (true); SHIM_SET_RETURN_32(0); diff --git a/src/xenia/kernel/xboxkrnl_rtl.cc b/src/xenia/kernel/xboxkrnl_rtl.cc index 2866baee2..16040f664 100644 --- a/src/xenia/kernel/xboxkrnl_rtl.cc +++ b/src/xenia/kernel/xboxkrnl_rtl.cc @@ -14,6 +14,7 @@ #include "xenia/base/atomic.h" #include "xenia/base/logging.h" #include "xenia/base/string.h" +#include "xenia/base/threading.h" #include "xenia/kernel/kernel_state.h" #include "xenia/kernel/xboxkrnl_private.h" #include "xenia/kernel/objects/xthread.h" @@ -398,7 +399,7 @@ spin: // TODO(benvanik): contention - do a real wait! // XELOGE("RtlEnterCriticalSection tried to really lock!"); spin_wait_remaining = 0; // HACK: spin forever - SwitchToThread(); + xe::threading::MaybeYield(); goto spin; }