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