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.cpp133
1 files changed, 133 insertions, 0 deletions
diff --git a/src/common/thread.cpp b/src/common/thread.cpp
new file mode 100644
index 000000000..27dbf3f93
--- /dev/null
+++ b/src/common/thread.cpp
@@ -0,0 +1,133 @@
1// Copyright 2013 Dolphin Emulator Project
2// Licensed under GPLv2
3// Refer to the license.txt file included.
4
5#include "thread.h"
6#include "common.h"
7
8#ifdef __APPLE__
9#include <mach/mach.h>
10#elif defined BSD4_4
11#include <pthread_np.h>
12#endif
13
14#ifdef USE_BEGINTHREADEX
15#include <process.h>
16#endif
17
18namespace Common
19{
20
21int CurrentThreadId()
22{
23#ifdef _WIN32
24 return GetCurrentThreadId();
25#elif defined __APPLE__
26 return mach_thread_self();
27#else
28 return 0;
29#endif
30}
31
32#ifdef _WIN32
33
34void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask)
35{
36 SetThreadAffinityMask(thread, mask);
37}
38
39void SetCurrentThreadAffinity(u32 mask)
40{
41 SetThreadAffinityMask(GetCurrentThread(), mask);
42}
43
44// Supporting functions
45void SleepCurrentThread(int ms)
46{
47 Sleep(ms);
48}
49
50void SwitchCurrentThread()
51{
52 SwitchToThread();
53}
54
55// Sets the debugger-visible name of the current thread.
56// Uses undocumented (actually, it is now documented) trick.
57// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/vxtsksettingthreadname.asp
58
59// This is implemented much nicer in upcoming msvc++, see:
60// http://msdn.microsoft.com/en-us/library/xcb2z8hs(VS.100).aspx
61void SetCurrentThreadName(const char* szThreadName)
62{
63 static const DWORD MS_VC_EXCEPTION = 0x406D1388;
64
65 #pragma pack(push,8)
66 struct THREADNAME_INFO
67 {
68 DWORD dwType; // must be 0x1000
69 LPCSTR szName; // pointer to name (in user addr space)
70 DWORD dwThreadID; // thread ID (-1=caller thread)
71 DWORD dwFlags; // reserved for future use, must be zero
72 } info;
73 #pragma pack(pop)
74
75 info.dwType = 0x1000;
76 info.szName = szThreadName;
77 info.dwThreadID = -1; //dwThreadID;
78 info.dwFlags = 0;
79
80 __try
81 {
82 RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info);
83 }
84 __except(EXCEPTION_CONTINUE_EXECUTION)
85 {}
86}
87
88#else // !WIN32, so must be POSIX threads
89
90void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask)
91{
92#ifdef __APPLE__
93 thread_policy_set(pthread_mach_thread_np(thread),
94 THREAD_AFFINITY_POLICY, (integer_t *)&mask, 1);
95#elif (defined __linux__ || defined BSD4_4) && !(defined ANDROID)
96 cpu_set_t cpu_set;
97 CPU_ZERO(&cpu_set);
98
99 for (int i = 0; i != sizeof(mask) * 8; ++i)
100 if ((mask >> i) & 1)
101 CPU_SET(i, &cpu_set);
102
103 pthread_setaffinity_np(thread, sizeof(cpu_set), &cpu_set);
104#endif
105}
106
107void SetCurrentThreadAffinity(u32 mask)
108{
109 SetThreadAffinity(pthread_self(), mask);
110}
111
112void SleepCurrentThread(int ms)
113{
114 usleep(1000 * ms);
115}
116
117void SwitchCurrentThread()
118{
119 usleep(1000 * 1);
120}
121
122void SetCurrentThreadName(const char* szThreadName)
123{
124#ifdef __APPLE__
125 pthread_setname_np(szThreadName);
126#else
127 pthread_setname_np(pthread_self(), szThreadName);
128#endif
129}
130
131#endif
132
133} // namespace Common