summaryrefslogtreecommitdiff
path: root/src/common/thread.cpp
diff options
context:
space:
mode:
authorGravatar Yuri Kunde Schlesner2016-09-21 11:29:48 -0700
committerGravatar GitHub2016-09-21 11:29:48 -0700
commitd5d2ca8058a0f1c00ab7ca9fe2c058ba47546c0a (patch)
tree8a22ca73ff838f3f0090b29a548ae81087fc90ed /src/common/thread.cpp
parentREADME: Specify master branch for Travis CI badge (diff)
parentFix Travis clang-format check (diff)
downloadyuzu-d5d2ca8058a0f1c00ab7ca9fe2c058ba47546c0a.tar.gz
yuzu-d5d2ca8058a0f1c00ab7ca9fe2c058ba47546c0a.tar.xz
yuzu-d5d2ca8058a0f1c00ab7ca9fe2c058ba47546c0a.zip
Merge pull request #2086 from linkmauve/clang-format
Add clang-format as part of our {commit,travis}-time checks
Diffstat (limited to 'src/common/thread.cpp')
-rw-r--r--src/common/thread.cpp82
1 files changed, 32 insertions, 50 deletions
diff --git a/src/common/thread.cpp b/src/common/thread.cpp
index 7bbf080bc..6e7b39b9a 100644
--- a/src/common/thread.cpp
+++ b/src/common/thread.cpp
@@ -3,29 +3,25 @@
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include "common/thread.h" 5#include "common/thread.h"
6
7#ifdef __APPLE__ 6#ifdef __APPLE__
8 #include <mach/mach.h> 7#include <mach/mach.h>
9#elif defined(_WIN32) 8#elif defined(_WIN32)
10 #include <Windows.h> 9#include <Windows.h>
10#else
11#if defined(BSD4_4) || defined(__OpenBSD__)
12#include <pthread_np.h>
11#else 13#else
12 #if defined(BSD4_4) || defined(__OpenBSD__) 14#include <pthread.h>
13 #include <pthread_np.h> 15#endif
14 #else 16#include <sched.h>
15 #include <pthread.h>
16 #endif
17 #include <sched.h>
18#endif 17#endif
19
20#ifndef _WIN32 18#ifndef _WIN32
21 #include <unistd.h> 19#include <unistd.h>
22#endif 20#endif
23 21
24namespace Common 22namespace Common {
25{
26 23
27int CurrentThreadId() 24int CurrentThreadId() {
28{
29#ifdef _MSC_VER 25#ifdef _MSC_VER
30 return GetCurrentThreadId(); 26 return GetCurrentThreadId();
31#elif defined __APPLE__ 27#elif defined __APPLE__
@@ -37,26 +33,22 @@ int CurrentThreadId()
37 33
38#ifdef _WIN32 34#ifdef _WIN32
39// Supporting functions 35// Supporting functions
40void SleepCurrentThread(int ms) 36void SleepCurrentThread(int ms) {
41{
42 Sleep(ms); 37 Sleep(ms);
43} 38}
44#endif 39#endif
45 40
46#ifdef _MSC_VER 41#ifdef _MSC_VER
47 42
48void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask) 43void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask) {
49{
50 SetThreadAffinityMask(thread, mask); 44 SetThreadAffinityMask(thread, mask);
51} 45}
52 46
53void SetCurrentThreadAffinity(u32 mask) 47void SetCurrentThreadAffinity(u32 mask) {
54{
55 SetThreadAffinityMask(GetCurrentThread(), mask); 48 SetThreadAffinityMask(GetCurrentThread(), mask);
56} 49}
57 50
58void SwitchCurrentThread() 51void SwitchCurrentThread() {
59{
60 SwitchToThread(); 52 SwitchToThread();
61} 53}
62 54
@@ -66,40 +58,34 @@ void SwitchCurrentThread()
66 58
67// This is implemented much nicer in upcoming msvc++, see: 59// This is implemented much nicer in upcoming msvc++, see:
68// http://msdn.microsoft.com/en-us/library/xcb2z8hs(VS.100).aspx 60// http://msdn.microsoft.com/en-us/library/xcb2z8hs(VS.100).aspx
69void SetCurrentThreadName(const char* szThreadName) 61void SetCurrentThreadName(const char* szThreadName) {
70{
71 static const DWORD MS_VC_EXCEPTION = 0x406D1388; 62 static const DWORD MS_VC_EXCEPTION = 0x406D1388;
72 63
73 #pragma pack(push,8) 64#pragma pack(push, 8)
74 struct THREADNAME_INFO 65 struct THREADNAME_INFO {
75 { 66 DWORD dwType; // must be 0x1000
76 DWORD dwType; // must be 0x1000 67 LPCSTR szName; // pointer to name (in user addr space)
77 LPCSTR szName; // pointer to name (in user addr space)
78 DWORD dwThreadID; // thread ID (-1=caller thread) 68 DWORD dwThreadID; // thread ID (-1=caller thread)
79 DWORD dwFlags; // reserved for future use, must be zero 69 DWORD dwFlags; // reserved for future use, must be zero
80 } info; 70 } info;
81 #pragma pack(pop) 71#pragma pack(pop)
82 72
83 info.dwType = 0x1000; 73 info.dwType = 0x1000;
84 info.szName = szThreadName; 74 info.szName = szThreadName;
85 info.dwThreadID = -1; //dwThreadID; 75 info.dwThreadID = -1; // dwThreadID;
86 info.dwFlags = 0; 76 info.dwFlags = 0;
87 77
88 __try 78 __try {
89 { 79 RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
90 RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info); 80 } __except (EXCEPTION_CONTINUE_EXECUTION) {
91 } 81 }
92 __except(EXCEPTION_CONTINUE_EXECUTION)
93 {}
94} 82}
95 83
96#else // !MSVC_VER, so must be POSIX threads 84#else // !MSVC_VER, so must be POSIX threads
97 85
98void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask) 86void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask) {
99{
100#ifdef __APPLE__ 87#ifdef __APPLE__
101 thread_policy_set(pthread_mach_thread_np(thread), 88 thread_policy_set(pthread_mach_thread_np(thread), THREAD_AFFINITY_POLICY, (integer_t*)&mask, 1);
102 THREAD_AFFINITY_POLICY, (integer_t *)&mask, 1);
103#elif (defined __linux__ || defined BSD4_4) && !(defined ANDROID) 89#elif (defined __linux__ || defined BSD4_4) && !(defined ANDROID)
104 cpu_set_t cpu_set; 90 cpu_set_t cpu_set;
105 CPU_ZERO(&cpu_set); 91 CPU_ZERO(&cpu_set);
@@ -112,27 +98,23 @@ void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask)
112#endif 98#endif
113} 99}
114 100
115void SetCurrentThreadAffinity(u32 mask) 101void SetCurrentThreadAffinity(u32 mask) {
116{
117 SetThreadAffinity(pthread_self(), mask); 102 SetThreadAffinity(pthread_self(), mask);
118} 103}
119 104
120#ifndef _WIN32 105#ifndef _WIN32
121void SleepCurrentThread(int ms) 106void SleepCurrentThread(int ms) {
122{
123 usleep(1000 * ms); 107 usleep(1000 * ms);
124} 108}
125 109
126void SwitchCurrentThread() 110void SwitchCurrentThread() {
127{
128 usleep(1000 * 1); 111 usleep(1000 * 1);
129} 112}
130#endif 113#endif
131 114
132// MinGW with the POSIX threading model does not support pthread_setname_np 115// MinGW with the POSIX threading model does not support pthread_setname_np
133#if !defined(_WIN32) || defined(_MSC_VER) 116#if !defined(_WIN32) || defined(_MSC_VER)
134void SetCurrentThreadName(const char* szThreadName) 117void SetCurrentThreadName(const char* szThreadName) {
135{
136#ifdef __APPLE__ 118#ifdef __APPLE__
137 pthread_setname_np(szThreadName); 119 pthread_setname_np(szThreadName);
138#elif defined(__OpenBSD__) 120#elif defined(__OpenBSD__)