1
0
mirror of https://github.com/doitsujin/dxvk.git synced 2025-03-13 19:29:14 +01:00

[util] Code cleanup for winegcc stuff

This commit is contained in:
Philip Rebohle 2018-07-21 12:51:50 +02:00
parent f38ee85a39
commit 64ffcbc9ef
No known key found for this signature in database
GPG Key ID: C8CC613427A31C99
5 changed files with 59 additions and 44 deletions

View File

@ -1,5 +1,6 @@
util_src = files([
'util_env.cpp',
'util_string.cpp',
'com/com_guid.cpp',
'com/com_private_data.cpp',

View File

@ -1,38 +1,34 @@
#pragma once
#include <functional>
#include "util_error.h"
#include <functional>
#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<void()> proc;
static DWORD WINAPI nativeProc(void *arg) {
auto* proc = reinterpret_cast<thread*>(arg);
proc->proc();
return 0;
}
public:
explicit thread(std::function<void()> 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<void()> 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<void()> m_proc;
HANDLE m_handle;
static DWORD WINAPI nativeProc(void *arg) {
auto* proc = reinterpret_cast<thread*>(arg);
proc->m_proc();
return 0;
}
};
namespace this_thread {

View File

@ -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;
}
}

25
src/util/util_string.cpp Normal file
View File

@ -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;
}
}

View File

@ -2,6 +2,7 @@
#include <string>
#include <sstream>
#include "./com/com_include.h"
namespace dxvk::str {