From fefaa31cd823eeb9afedb6b6ea6a6efd87b0b8e8 Mon Sep 17 00:00:00 2001 From: Ben Vanik Date: Wed, 15 Jul 2015 22:09:19 -0700 Subject: [PATCH] Switching a few types to their platform-agnostic versions. --- src/xenia/apu/audio_system.cc | 4 +- src/xenia/base/threading.h | 4 ++ src/xenia/base/threading_win.cc | 9 +++ .../cpu/frontend/test/xe-cpu-ppc-test.cc | 70 +++++-------------- src/xenia/debug/debugger.cc | 3 + src/xenia/emulator.cc | 7 +- src/xenia/gpu/gl4/command_processor.cc | 11 +-- src/xenia/gpu/gl4/command_processor.h | 3 +- src/xenia/gpu/xe-gpu-trace-viewer.cc | 1 + src/xenia/kernel/objects/xnotify_listener.h | 2 - src/xenia/kernel/xam_ui.cc | 1 + src/xenia/kernel/xboxkrnl_module.cc | 1 + src/xenia/kernel/xboxkrnl_module.h | 3 + src/xenia/kernel/xboxkrnl_threading.cc | 5 +- 14 files changed, 53 insertions(+), 71 deletions(-) diff --git a/src/xenia/apu/audio_system.cc b/src/xenia/apu/audio_system.cc index eca7f0939..fb6f7daa2 100644 --- a/src/xenia/apu/audio_system.cc +++ b/src/xenia/apu/audio_system.cc @@ -106,8 +106,8 @@ void AudioSystem::WorkerThreadMain() { // Main run loop. while (worker_running_) { - auto result = xe::threading::WaitAny( - wait_handles_, DWORD(xe::countof(wait_handles_)), true); + auto result = + xe::threading::WaitAny(wait_handles_, xe::countof(wait_handles_), true); if (result.first == xe::threading::WaitResult::kFailed || (result.first == xe::threading::WaitResult::kSuccess && result.second == kMaximumClientCount)) { diff --git a/src/xenia/base/threading.h b/src/xenia/base/threading.h index 539fc228a..70a58699c 100644 --- a/src/xenia/base/threading.h +++ b/src/xenia/base/threading.h @@ -48,6 +48,10 @@ class Fence { // Returns the total number of logical processors in the host system. uint32_t logical_processor_count(); +// Enables the current process to set thread affinity. +// Must be called at startup before attempting to set thread affinity. +void EnableAffinityConfiguration(); + // Gets a stable thread-specific ID, but may not be. Use for informative // purposes only. uint32_t current_thread_id(); diff --git a/src/xenia/base/threading_win.cc b/src/xenia/base/threading_win.cc index 6f59b49b0..e89dfd35d 100644 --- a/src/xenia/base/threading_win.cc +++ b/src/xenia/base/threading_win.cc @@ -26,6 +26,15 @@ uint32_t logical_processor_count() { return value; } +void EnableAffinityConfiguration() { + HANDLE process_handle = GetCurrentProcess(); + DWORD_PTR process_affinity_mask; + DWORD_PTR system_affinity_mask; + GetProcessAffinityMask(process_handle, &process_affinity_mask, + &system_affinity_mask); + SetProcessAffinityMask(process_handle, system_affinity_mask); +} + uint32_t current_thread_id() { return static_cast(GetCurrentThreadId()); } diff --git a/src/xenia/cpu/frontend/test/xe-cpu-ppc-test.cc b/src/xenia/cpu/frontend/test/xe-cpu-ppc-test.cc index ef44a46c7..d710f579b 100644 --- a/src/xenia/cpu/frontend/test/xe-cpu-ppc-test.cc +++ b/src/xenia/cpu/frontend/test/xe-cpu-ppc-test.cc @@ -7,19 +7,22 @@ ****************************************************************************** */ +#include + +#include "xenia/base/filesystem.h" #include "xenia/base/logging.h" #include "xenia/base/main.h" #include "xenia/base/math.h" +#include "xenia/base/platform.h" #include "xenia/cpu/backend/x64/x64_backend.h" #include "xenia/cpu/frontend/ppc_context.h" #include "xenia/cpu/frontend/ppc_frontend.h" #include "xenia/cpu/processor.h" #include "xenia/cpu/raw_module.h" -#if !XE_PLATFORM_WIN32 -#include -#endif // !WIN32 -#include +#if XE_COMPILER_MSVC +#include "xenia/base/platform_win.h" +#endif // XE_COMPILER_MSVC DEFINE_string(test_path, "src/xenia/cpu/frontend/test/", "Directory scanned for test files."); @@ -331,69 +334,30 @@ class TestRunner { bool DiscoverTests(std::wstring& test_path, std::vector& test_files) { -// TODO(benvanik): use PAL instead of this. -#if XE_PLATFORM_WIN32 - std::wstring search_path = test_path; - search_path.append(L"\\*.s"); - WIN32_FIND_DATA ffd; - HANDLE hFind = FindFirstFile(search_path.c_str(), &ffd); - if (hFind == INVALID_HANDLE_VALUE) { - XELOGE("Unable to find test path %ls", test_path.c_str()); - return false; - } - do { - if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { - std::wstring file_name(ffd.cFileName); - std::wstring file_path = test_path; - if (*(test_path.end() - 1) != '\\') { - file_path += '\\'; - } - file_path += file_name; - test_files.push_back(file_path); - } - } while (FindNextFile(hFind, &ffd)); - FindClose(hFind); -#else - DIR* d = opendir(test_path.c_str()); - if (!d) { - XELOGE("Unable to find test path %ls", test_path.c_str()); - return false; - } - struct dirent* dir; - while ((dir = readdir(d))) { - if (dir->d_type == DT_REG) { - // Only return .s files. - string file_name = string(dir->d_name); - if (file_name.rfind(".s") != string::npos) { - string file_path = test_path; - if (*(test_path.end() - 1) != '/') { - file_path += "/"; - } - file_path += file_name; - test_files.push_back(file_path); - } + auto file_infos = xe::filesystem::ListFiles(test_path); + for (auto& file_info : file_infos) { + if (file_info.name.rfind(L".s") == file_info.name.size() - 2) { + test_files.push_back(xe::join_paths(test_path, file_info.name)); } } - closedir(d); -#endif // WIN32 return true; } -#ifdef _MSC_VER +#if XE_COMPILER_MSVC int filter(unsigned int code) { if (code == EXCEPTION_ILLEGAL_INSTRUCTION) { return EXCEPTION_EXECUTE_HANDLER; } return EXCEPTION_CONTINUE_SEARCH; } -#endif +#endif // XE_COMPILER_MSVC void ProtectedRunTest(TestSuite& test_suite, TestRunner& runner, TestCase& test_case, int& failed_count, int& passed_count) { -#ifdef _MSC_VER +#if XE_COMPILER_MSVC __try { -#endif +#endif // XE_COMPILER_MSVC if (!runner.Setup(test_suite)) { XELOGE(" TEST FAILED SETUP"); @@ -406,13 +370,13 @@ void ProtectedRunTest(TestSuite& test_suite, TestRunner& runner, ++failed_count; } -#ifdef _MSC_VER +#if XE_COMPILER_MSVC } __except(filter(GetExceptionCode())) { XELOGE(" TEST FAILED (UNSUPPORTED INSTRUCTION)"); ++failed_count; } -#endif +#endif // XE_COMPILER_MSVC } bool RunTests(const std::wstring& test_name) { diff --git a/src/xenia/debug/debugger.cc b/src/xenia/debug/debugger.cc index 44c033337..ce14e6426 100644 --- a/src/xenia/debug/debugger.cc +++ b/src/xenia/debug/debugger.cc @@ -10,6 +10,9 @@ #include "xenia/debug/debugger.h" #include + +// TODO(benvanik): generic socket implementation in base/. +#include "xenia/base/platform_win.h" #include #include #include diff --git a/src/xenia/emulator.cc b/src/xenia/emulator.cc index b643c98a4..9b75cef35 100644 --- a/src/xenia/emulator.cc +++ b/src/xenia/emulator.cc @@ -84,12 +84,7 @@ X_STATUS Emulator::Setup(ui::Window* display_window) { // Before we can set thread affinity we must enable the process to use all // logical processors. - HANDLE process_handle = GetCurrentProcess(); - DWORD_PTR process_affinity_mask; - DWORD_PTR system_affinity_mask; - GetProcessAffinityMask(process_handle, &process_affinity_mask, - &system_affinity_mask); - SetProcessAffinityMask(process_handle, system_affinity_mask); + xe::threading::EnableAffinityConfiguration(); // Create memory system first, as it is required for other systems. memory_ = std::make_unique(); diff --git a/src/xenia/gpu/gl4/command_processor.cc b/src/xenia/gpu/gl4/command_processor.cc index 494f9bad9..c7eff988b 100644 --- a/src/xenia/gpu/gl4/command_processor.cc +++ b/src/xenia/gpu/gl4/command_processor.cc @@ -63,7 +63,7 @@ CommandProcessor::CommandProcessor(GL4GraphicsSystem* graphics_system) read_ptr_index_(0), read_ptr_update_freq_(0), read_ptr_writeback_ptr_(0), - write_ptr_index_event_(CreateEvent(NULL, FALSE, FALSE, NULL)), + write_ptr_index_event_(xe::threading::Event::CreateAutoResetEvent(false)), write_ptr_index_(0), bin_select_(0xFFFFFFFFull), bin_mask_(0xFFFFFFFFull), @@ -78,7 +78,7 @@ CommandProcessor::CommandProcessor(GL4GraphicsSystem* graphics_system) draw_batcher_(graphics_system_->register_file()), scratch_buffer_(kScratchBufferCapacity, kScratchBufferAlignment) {} -CommandProcessor::~CommandProcessor() { CloseHandle(write_ptr_index_event_); } +CommandProcessor::~CommandProcessor() = default; bool CommandProcessor::Initialize( std::unique_ptr context) { @@ -101,7 +101,7 @@ void CommandProcessor::Shutdown() { EndTracing(); worker_running_ = false; - SetEvent(write_ptr_index_event_); + write_ptr_index_event_->Set(); worker_thread_->Wait(0, 0, 0, nullptr); worker_thread_.reset(); @@ -200,7 +200,8 @@ void CommandProcessor::WorkerThreadMain() { // TODO(benvanik): if we go longer than Nms, switch to waiting? // It'll keep us from burning power. // const int wait_time_ms = 5; - // WaitForSingleObject(write_ptr_index_event_, wait_time_ms); + // xe::threading::Wait(write_ptr_index_event_.get(), true, + // std::chrono::milliseconds(wait_time_ms)); xe::threading::MaybeYield(); write_ptr_index = write_ptr_index_.load(); } while (worker_running_ && pending_fns_.empty() && @@ -488,7 +489,7 @@ void CommandProcessor::EnableReadPointerWriteBack(uint32_t ptr, void CommandProcessor::UpdateWritePointer(uint32_t value) { write_ptr_index_ = value; - SetEvent(write_ptr_index_event_); + write_ptr_index_event_->Set(); } void CommandProcessor::WriteRegister(uint32_t index, uint32_t value) { diff --git a/src/xenia/gpu/gl4/command_processor.h b/src/xenia/gpu/gl4/command_processor.h index f096ed142..80d534c8a 100644 --- a/src/xenia/gpu/gl4/command_processor.h +++ b/src/xenia/gpu/gl4/command_processor.h @@ -18,6 +18,7 @@ #include #include +#include "xenia/base/threading.h" #include "xenia/gpu/gl4/draw_batcher.h" #include "xenia/gpu/gl4/gl4_shader.h" #include "xenia/gpu/gl4/gl4_shader_translator.h" @@ -259,7 +260,7 @@ class CommandProcessor { uint32_t read_ptr_update_freq_; uint32_t read_ptr_writeback_ptr_; - HANDLE write_ptr_index_event_; + std::unique_ptr write_ptr_index_event_; std::atomic write_ptr_index_; uint64_t bin_select_; diff --git a/src/xenia/gpu/xe-gpu-trace-viewer.cc b/src/xenia/gpu/xe-gpu-trace-viewer.cc index 2fdb35773..271814c07 100644 --- a/src/xenia/gpu/xe-gpu-trace-viewer.cc +++ b/src/xenia/gpu/xe-gpu-trace-viewer.cc @@ -17,6 +17,7 @@ #include "xenia/base/main.h" #include "xenia/base/mapped_memory.h" #include "xenia/base/math.h" +#include "xenia/base/platform_win.h" #include "xenia/emulator.h" #include "xenia/gpu/graphics_system.h" #include "xenia/gpu/register_file.h" diff --git a/src/xenia/kernel/objects/xnotify_listener.h b/src/xenia/kernel/objects/xnotify_listener.h index 2516fff4c..4703ad46f 100644 --- a/src/xenia/kernel/objects/xnotify_listener.h +++ b/src/xenia/kernel/objects/xnotify_listener.h @@ -18,8 +18,6 @@ #include "xenia/kernel/xobject.h" #include "xenia/xbox.h" -typedef void* HANDLE; - namespace xe { namespace kernel { diff --git a/src/xenia/kernel/xam_ui.cc b/src/xenia/kernel/xam_ui.cc index 46e049a5f..bd40355b4 100644 --- a/src/xenia/kernel/xam_ui.cc +++ b/src/xenia/kernel/xam_ui.cc @@ -8,6 +8,7 @@ */ #include "xenia/base/logging.h" +#include "xenia/base/platform_win.h" #include "xenia/emulator.h" #include "xenia/kernel/kernel_state.h" #include "xenia/kernel/util/shim_utils.h" diff --git a/src/xenia/kernel/xboxkrnl_module.cc b/src/xenia/kernel/xboxkrnl_module.cc index 6af08be9e..f0214ba9a 100644 --- a/src/xenia/kernel/xboxkrnl_module.cc +++ b/src/xenia/kernel/xboxkrnl_module.cc @@ -14,6 +14,7 @@ #include "xenia/base/clock.h" #include "xenia/base/logging.h" #include "xenia/base/math.h" +#include "xenia/base/platform_win.h" #include "xenia/emulator.h" #include "xenia/kernel/kernel_state.h" #include "xenia/kernel/xboxkrnl_private.h" diff --git a/src/xenia/kernel/xboxkrnl_module.h b/src/xenia/kernel/xboxkrnl_module.h index 5e34e6c70..f8cbb0c4e 100644 --- a/src/xenia/kernel/xboxkrnl_module.h +++ b/src/xenia/kernel/xboxkrnl_module.h @@ -17,6 +17,9 @@ // All of the exported functions: #include "xenia/kernel/xboxkrnl_rtl.h" +// TODO(benvanik): switch timer. +typedef void* HANDLE; + namespace xe { namespace kernel { diff --git a/src/xenia/kernel/xboxkrnl_threading.cc b/src/xenia/kernel/xboxkrnl_threading.cc index 2442b9483..26f2b8a81 100644 --- a/src/xenia/kernel/xboxkrnl_threading.cc +++ b/src/xenia/kernel/xboxkrnl_threading.cc @@ -11,6 +11,7 @@ #include "xenia/base/clock.h" #include "xenia/base/logging.h" #include "xenia/base/mutex.h" +#include "xenia/base/platform_win.h" #include "xenia/cpu/processor.h" #include "xenia/kernel/dispatcher.h" #include "xenia/kernel/kernel_state.h" @@ -446,7 +447,7 @@ SHIM_CALL KeTlsSetValue_shim(PPCContext* ppc_context, #if XE_PLATFORM_WIN32 result = TlsSetValue( - tls_index, reinterpret_cast(static_cast(tls_value))); + tls_index, reinterpret_cast(static_cast(tls_value))); #else result = pthread_setspecific(tls_index, (void*)tls_value) == 0; #endif // WIN32 @@ -1031,7 +1032,7 @@ SHIM_CALL KfAcquireSpinLock_shim(PPCContext* ppc_context, while (!xe::atomic_cas(0, 1, lock)) { // Spin! // TODO(benvanik): error on deadlock? - YieldProcessor(); + xe::threading::MaybeYield(); } // Raise IRQL to DISPATCH.