From 64ffcbc9efef9750f7302b295bbdd2551e6ee42b Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Sat, 21 Jul 2018 12:51:50 +0200 Subject: [PATCH] [util] Code cleanup for winegcc stuff --- src/util/meson.build | 1 + src/util/thread.h | 56 +++++++++++++++++++++++----------------- src/util/util_env.cpp | 20 -------------- src/util/util_string.cpp | 25 ++++++++++++++++++ src/util/util_string.h | 1 + 5 files changed, 59 insertions(+), 44 deletions(-) create mode 100644 src/util/util_string.cpp diff --git a/src/util/meson.build b/src/util/meson.build index 7bfb4c8a..265aee80 100644 --- a/src/util/meson.build +++ b/src/util/meson.build @@ -1,5 +1,6 @@ util_src = files([ 'util_env.cpp', + 'util_string.cpp', 'com/com_guid.cpp', 'com/com_private_data.cpp', diff --git a/src/util/thread.h b/src/util/thread.h index 3bb17ea2..6086e634 100644 --- a/src/util/thread.h +++ b/src/util/thread.h @@ -1,38 +1,34 @@ #pragma once +#include + #include "util_error.h" -#include #include "./com/com_include.h" -/* - * This is needed mostly for winelib builds. Wine needs to setup each thread that - * calls Windows APIs. It means that in winelib builds, we can't let standard C++ - * library create threads and need to use Wine for that instead. We use a thin wrapper - * around Windows thread functions so that the rest of code just has to use - * dxvk::thread class instead of std::thread. - */ namespace dxvk { + /** + * \brief Thread helper class + * + * This is needed mostly for winelib builds. Wine needs to setup each thread that + * calls Windows APIs. It means that in winelib builds, we can't let standard C++ + * library create threads and need to use Wine for that instead. We use a thin wrapper + * around Windows thread functions so that the rest of code just has to use + * dxvk::thread class instead of std::thread. + */ class thread { - private: - HANDLE m_handle; - std::function proc; - - static DWORD WINAPI nativeProc(void *arg) { - auto* proc = reinterpret_cast(arg); - proc->proc(); - return 0; - } public: - explicit thread(std::function func) : proc(func) { - m_handle = ::CreateThread(nullptr, 0, thread::nativeProc, this, 0, nullptr); - if (!m_handle) { - throw DxvkError("Failed to create thread"); - } - } - thread() : m_handle(nullptr) {} + thread() + : m_handle(nullptr) { } + + explicit thread(std::function func) : m_proc(func) { + m_handle = ::CreateThread(nullptr, 0, thread::nativeProc, this, 0, nullptr); + + if (!m_handle) + throw DxvkError("Failed to create thread"); + } ~thread() { if (m_handle) @@ -42,6 +38,18 @@ namespace dxvk { void join() { ::WaitForSingleObject(m_handle, INFINITE); } + + private: + + std::function m_proc; + HANDLE m_handle; + + static DWORD WINAPI nativeProc(void *arg) { + auto* proc = reinterpret_cast(arg); + proc->m_proc(); + return 0; + } + }; namespace this_thread { diff --git a/src/util/util_env.cpp b/src/util/util_env.cpp index 3eab0506..c8bd7263 100644 --- a/src/util/util_env.cpp +++ b/src/util/util_env.cpp @@ -53,23 +53,3 @@ namespace dxvk::env { } } - -#ifdef CP_UNIXCP -static constexpr int cp = CP_UNIXCP; -#else -static constexpr int cp = CP_ACP; -#endif - -namespace dxvk::str { - std::string fromws(const WCHAR *ws) { - size_t len = WideCharToMultiByte(cp, 0, ws, -1, NULL, 0, NULL, NULL); - if (len <= 1) - return ""; - - len--; - std::string result; - result.resize(len); - WideCharToMultiByte(cp, 0, ws, -1, &result.at(0), len, NULL, NULL); - return result; - } -} diff --git a/src/util/util_string.cpp b/src/util/util_string.cpp new file mode 100644 index 00000000..8e1eb0c7 --- /dev/null +++ b/src/util/util_string.cpp @@ -0,0 +1,25 @@ +#include "util_string.h" + +#ifdef CP_UNIXCP +static constexpr int cp = CP_UNIXCP; +#else +static constexpr int cp = CP_ACP; +#endif + +namespace dxvk::str { + std::string fromws(const WCHAR *ws) { + size_t len = ::WideCharToMultiByte(cp, + 0, ws, -1, nullptr, 0, nullptr, nullptr); + + if (len <= 1) + return ""; + + len -= 1; + + std::string result; + result.resize(len); + ::WideCharToMultiByte(cp, 0, ws, -1, + &result.at(0), len, nullptr, nullptr); + return result; + } +} diff --git a/src/util/util_string.h b/src/util/util_string.h index ca42114d..e7e44b60 100644 --- a/src/util/util_string.h +++ b/src/util/util_string.h @@ -2,6 +2,7 @@ #include #include + #include "./com/com_include.h" namespace dxvk::str {