diff --git a/CMakeLists.txt b/CMakeLists.txt index 95f8479533..eb3ba02f95 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -270,8 +270,6 @@ add_library(Common STATIC Common/MsgHandler.h Common/StringUtils.cpp Common/StringUtils.h - Common/Thread.cpp - Common/Thread.h Common/ThreadPools.cpp Common/ThreadPools.h Common/Timer.cpp diff --git a/Common/LogManager.h b/Common/LogManager.h index 38ea882062..8cd615033e 100644 --- a/Common/LogManager.h +++ b/Common/LogManager.h @@ -19,11 +19,11 @@ #include "Log.h" #include "StringUtils.h" -#include "Thread.h" #include "FileUtil.h" #include "file/ini_file.h" #include +#include "Common/StdMutex.h" #define MAX_MESSAGES 8000 #define MAX_MSGLEN 1024 diff --git a/Common/Thread.cpp b/Common/Thread.cpp deleted file mode 100644 index c6d94258ba..0000000000 --- a/Common/Thread.cpp +++ /dev/null @@ -1,214 +0,0 @@ -// Copyright (C) 2003 Dolphin Project. - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, version 2.0 or later versions. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License 2.0 for more details. - -// A copy of the GPL 2.0 should have been included with the program. -// If not, see http://www.gnu.org/licenses/ - -// Official SVN repository and contact information can be found at -// http://code.google.com/p/dolphin-emu/ - -#include "Thread.h" -#include "Common.h" - -#ifdef __APPLE__ -#include -#elif defined(ANDROID) -#include -#elif defined BSD4_4 -#include -#endif - -#ifdef BLACKBERRY -#include -#endif - -#ifdef USE_BEGINTHREADEX -#include -#endif - -namespace Common -{ - -int CurrentThreadId() -{ -#ifdef _WIN32 - return GetCurrentThreadId(); -#elif defined __APPLE__ - return mach_thread_self(); -#else - return pthread_self(); -#endif -} - -#ifdef _WIN32 - -bool SetThreadAffinity(std::thread::native_handle_type thread, u32 mask) -{ - return SetThreadAffinityMask(thread, mask) != 0; -} - -bool SetCurrentThreadAffinity(u32 mask) -{ - return SetThreadAffinityMask(GetCurrentThread(), mask) != 0; -} - -// Supporting functions -void SleepCurrentThread(int ms) -{ - Sleep(ms); -} - -void SwitchCurrentThread() -{ - SwitchToThread(); -} - -// Sets the debugger-visible name of the current thread. -// Uses undocumented (actually, it is now documented) trick. -// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/vxtsksettingthreadname.asp - -// This is implemented much nicer in upcoming msvc++, see: -// http://msdn.microsoft.com/en-us/library/xcb2z8hs(VS.100).aspx -void SetCurrentThreadName(const char* szThreadName) -{ - static const DWORD MS_VC_EXCEPTION = 0x406D1388; - - #pragma pack(push,8) - struct THREADNAME_INFO - { - DWORD dwType; // must be 0x1000 - LPCSTR szName; // pointer to name (in user addr space) - DWORD dwThreadID; // thread ID (-1=caller thread) - DWORD dwFlags; // reserved for future use, must be zero - } info; - #pragma pack(pop) - - info.dwType = 0x1000; - info.szName = szThreadName; - info.dwThreadID = -1; //dwThreadID; - info.dwFlags = 0; - - __try - { - RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info); - } - __except(EXCEPTION_CONTINUE_EXECUTION) - {} -} - -void EnableCrashingOnCrashes() -{ - typedef BOOL (WINAPI *tGetPolicy)(LPDWORD lpFlags); - typedef BOOL (WINAPI *tSetPolicy)(DWORD dwFlags); - const DWORD EXCEPTION_SWALLOWING = 0x1; - - HMODULE kernel32 = LoadLibraryA("kernel32.dll"); - tGetPolicy pGetPolicy = (tGetPolicy)GetProcAddress(kernel32, - "GetProcessUserModeExceptionPolicy"); - tSetPolicy pSetPolicy = (tSetPolicy)GetProcAddress(kernel32, - "SetProcessUserModeExceptionPolicy"); - if (pGetPolicy && pSetPolicy) - { - DWORD dwFlags; - if (pGetPolicy(&dwFlags)) - { - // Turn off the filter - pSetPolicy(dwFlags & ~EXCEPTION_SWALLOWING); - } - } -} - - -#else // !WIN32, so must be POSIX threads - -bool SetThreadAffinity(std::thread::native_handle_type thread, u32 mask) -{ -#ifdef __APPLE__ - return thread_policy_set(pthread_mach_thread_np(thread), - THREAD_AFFINITY_POLICY, (integer_t *)&mask, 1) == 0; -#elif defined(ANDROID) || defined(BLACKBERRY) - return false; -#elif (defined __linux__ || defined BSD4_4) - cpu_set_t cpu_set; - CPU_ZERO(&cpu_set); - - for (int i = 0; i != sizeof(mask) * 8; ++i) { - if ((mask >> i) & 1) - CPU_SET(i, &cpu_set); - } - - return pthread_setaffinity_np(thread, sizeof(cpu_set), &cpu_set) == 0; -#endif - - return false; -} - -bool SetCurrentThreadAffinity(u32 mask) -{ -#ifdef BLACKBERRY - return ThreadCtl(_NTO_TCTL_RUNMASK, &mask) != -1; -#elif defined(ANDROID) - return syscall(__NR_sched_setaffinity, gettid(), sizeof(mask), &mask) == 0; -#else - return SetThreadAffinity(pthread_self(), mask); -#endif -} - -static pthread_key_t threadname_key; -static pthread_once_t threadname_key_once = PTHREAD_ONCE_INIT; - -void SleepCurrentThread(int ms) -{ - usleep(1000 * ms); -} - -void SwitchCurrentThread() -{ - usleep(1000 * 1); -} - -static void FreeThreadName(void* threadname) -{ - free(threadname); -} - -static void ThreadnameKeyAlloc() -{ - pthread_key_create(&threadname_key, FreeThreadName); -} - -void SetCurrentThreadName(const char* szThreadName) -{ -#ifdef APPLE - pthread_setname_np(szThreadName); -#elif defined(BLACKBERRY) - pthread_setname_np(pthread_self(), szThreadName); -#else - pthread_once(&threadname_key_once, ThreadnameKeyAlloc); - - void* threadname; - if ((threadname = pthread_getspecific(threadname_key)) != NULL) - free(threadname); - - pthread_setspecific(threadname_key, strdup(szThreadName)); -#endif - - INFO_LOG(COMMON, "%s(%s)\n", __FUNCTION__, szThreadName); -} - -void EnableCrashingOnCrashes() -{ - -} - -#endif - -} // namespace Common diff --git a/Common/Thread.h b/Common/Thread.h deleted file mode 100644 index 677ae51a61..0000000000 --- a/Common/Thread.h +++ /dev/null @@ -1,144 +0,0 @@ -// Copyright (C) 2003 Dolphin Project. - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, version 2.0 or later versions. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License 2.0 for more details. - -// A copy of the GPL 2.0 should have been included with the program. -// If not, see http://www.gnu.org/licenses/ - -// Official SVN repository and contact information can be found at -// http://code.google.com/p/dolphin-emu/ - -#ifndef _THREAD_H_ -#define _THREAD_H_ - -#include "thread/thread.h" -#include "StdConditionVariable.h" - -// Don't include common.h here as it will break LogManager -#include "CommonTypes.h" -#include -#include - -// This may not be defined outside _WIN32 -#ifndef _WIN32 -#ifndef INFINITE -#define INFINITE 0xffffffff -#endif - -// Assume !ARM && !MIPS = x86 -#if !defined(ARM) && !defined(MIPS) -#include -#endif - -//for gettimeofday and struct time(spec|val) -#include -#include -#endif - -namespace Common -{ - -int CurrentThreadId(); - -// In Windows 7 SP1 or later, stops Windows from swallowing crashes in WndProcs and other callbacks. -void EnableCrashingOnCrashes(); - -bool SetThreadAffinity(std::thread::native_handle_type thread, u32 mask); -bool SetCurrentThreadAffinity(u32 mask); - -class Event -{ -public: - Event() - : is_set(false) - {}; - - void Set() - { - std::lock_guard lk(m_mutex); - if (!is_set) - { - is_set = true; - m_condvar.notify_one(); - } - } - - void Wait() - { - std::unique_lock lk(m_mutex); - m_condvar.wait(lk, [&]{ return is_set; }); - is_set = false; - } - - void Reset() - { - std::unique_lock lk(m_mutex); - // no other action required, since wait loops on the predicate and any lingering signal will get cleared on the first iteration - is_set = false; - } - -private: - volatile bool is_set; - std::condition_variable m_condvar; - std::mutex m_mutex; -}; - -// TODO: doesn't work on windows with (count > 2) -class Barrier -{ -public: - Barrier(size_t count) - : m_count(count), m_waiting(0) - {} - - // block until "count" threads call Sync() - bool Sync() - { - std::unique_lock lk(m_mutex); - - // TODO: broken when next round of Sync()s - // is entered before all waiting threads return from the notify_all - - if (m_count == ++m_waiting) - { - m_waiting = 0; - m_condvar.notify_all(); - return true; - } - else - { - m_condvar.wait(lk, [&]{ return (0 == m_waiting); }); - return false; - } - } - -private: - std::condition_variable m_condvar; - std::mutex m_mutex; - const size_t m_count; - volatile size_t m_waiting; -}; - -void SleepCurrentThread(int ms); -void SwitchCurrentThread(); // On Linux, this is equal to sleep 1ms - -// Use this function during a spin-wait to make the current thread -// relax while another thread is working. This may be more efficient -// than using events because event functions use kernel calls. -inline void YieldCPU() -{ - std::this_thread::yield(); -} - -void SetCurrentThreadName(const char *name); - -} // namespace Common - -#endif // _THREAD_H_ diff --git a/Core/HLE/__sceAudio.cpp b/Core/HLE/__sceAudio.cpp index 9396b46d9c..fe0871bc5e 100644 --- a/Core/HLE/__sceAudio.cpp +++ b/Core/HLE/__sceAudio.cpp @@ -27,7 +27,6 @@ #include "../Config.h" #include "ChunkFile.h" #include "FixedSizeQueue.h" -#include "Common/Thread.h" #include "Common/Atomics.h" #include "../../native/base/mutex.h" diff --git a/Core/HLE/sceDisplay.cpp b/Core/HLE/sceDisplay.cpp index 020fb8d671..6cf967ea9a 100644 --- a/Core/HLE/sceDisplay.cpp +++ b/Core/HLE/sceDisplay.cpp @@ -29,7 +29,6 @@ #include "gfx_es2/gl_state.h" #endif -#include "Common/Thread.h" #include "Core/CoreTiming.h" #include "Core/CoreParameter.h" #include "Core/Reporting.h" @@ -464,7 +463,7 @@ void DoFrameTiming(bool &throttle, bool &skipFrame, float timestep) { } else { // Wait until we've caught up. while (time_now_d() < nextFrameTime) { - Common::SleepCurrentThread(1); + sleep_ms(1); // Sleep for 1ms on this thread time_update(); } } diff --git a/Qt/Common.pro b/Qt/Common.pro index f817c67f33..a21543459d 100755 --- a/Qt/Common.pro +++ b/Qt/Common.pro @@ -40,7 +40,6 @@ SOURCES += ../Common/ChunkFile.cpp \ ../Common/Misc.cpp \ ../Common/MsgHandler.cpp \ ../Common/StringUtils.cpp \ - ../Common/Thread.cpp \ ../Common/ThreadPools.cpp \ ../Common/Timer.cpp \ ../Common/Crypto/*.cpp @@ -54,7 +53,6 @@ HEADERS += ../Common/ChunkFile.h \ ../Common/MemoryUtil.h \ ../Common/MsgHandler.h \ ../Common/StringUtils.h \ - ../Common/Thread.h \ ../Common/ThreadPools.h \ ../Common/Timer.h \ ../Common/Crypto/*.h diff --git a/Qt/QtHost.cpp b/Qt/QtHost.cpp index 55f270190d..b00f635fe7 100644 --- a/Qt/QtHost.cpp +++ b/Qt/QtHost.cpp @@ -249,7 +249,6 @@ void QtHost::NextGPUStep() void NativeInit(int argc, const char *argv[], const char *savegame_directory, const char *external_directory, const char *installID) { - Common::EnableCrashingOnCrashes(); isMessagePending = false; std::string user_data_path = savegame_directory; diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index e0ebf49a82..dbd658f5b9 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -37,6 +37,7 @@ #include "native/ext/stb_image_write/stb_image_writer.h" #include "native/ext/jpge/jpge.h" #include "native/util/text/utf8.h" +#include "native/thread/thread.h" #include "gfx_es2/gl_state.h" #include "gfx_es2/draw_text.h" #include "gfx_es2/draw_buffer.h" diff --git a/Windows/main.cpp b/Windows/main.cpp index 9fe3efbae1..939a035a47 100644 --- a/Windows/main.cpp +++ b/Windows/main.cpp @@ -156,9 +156,31 @@ std::string System_GetProperty(SystemProperty prop) { } } +void EnableCrashingOnCrashes() +{ + typedef BOOL (WINAPI *tGetPolicy)(LPDWORD lpFlags); + typedef BOOL (WINAPI *tSetPolicy)(DWORD dwFlags); + const DWORD EXCEPTION_SWALLOWING = 0x1; + + HMODULE kernel32 = LoadLibraryA("kernel32.dll"); + tGetPolicy pGetPolicy = (tGetPolicy)GetProcAddress(kernel32, + "GetProcessUserModeExceptionPolicy"); + tSetPolicy pSetPolicy = (tSetPolicy)GetProcAddress(kernel32, + "SetProcessUserModeExceptionPolicy"); + if (pGetPolicy && pSetPolicy) + { + DWORD dwFlags; + if (pGetPolicy(&dwFlags)) + { + // Turn off the filter + pSetPolicy(dwFlags & ~EXCEPTION_SWALLOWING); + } + } +} + int WINAPI WinMain(HINSTANCE _hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow) { - Common::EnableCrashingOnCrashes(); + EnableCrashingOnCrashes(); wchar_t modulePath[MAX_PATH]; GetModuleFileName(NULL, modulePath, MAX_PATH); diff --git a/android/jni/Android.mk b/android/jni/Android.mk index cb03ed87e9..5a8e9070bf 100644 --- a/android/jni/Android.mk +++ b/android/jni/Android.mk @@ -119,7 +119,6 @@ EXEC_AND_LIB_FILES := \ $(SRC)/Common/MsgHandler.cpp \ $(SRC)/Common/FileUtil.cpp \ $(SRC)/Common/StringUtils.cpp \ - $(SRC)/Common/Thread.cpp \ $(SRC)/Common/ThreadPools.cpp \ $(SRC)/Common/Timer.cpp \ $(SRC)/Common/Misc.cpp \