| 
									
										
										
										
											2015-05-24 06:55:12 +02:00
										 |  |  | // Copyright 2008 Dolphin Emulator Project
 | 
					
						
							| 
									
										
										
										
											2015-05-18 01:08:10 +02:00
										 |  |  | // Licensed under GPLv2+
 | 
					
						
							| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | #include "Common/Thread.h"
 | 
					
						
							| 
									
										
										
										
											2014-09-18 23:17:41 -05:00
										 |  |  | #include "Common/CommonFuncs.h"
 | 
					
						
							| 
									
										
										
										
											2014-09-07 20:06:58 -05:00
										 |  |  | #include "Common/CommonTypes.h"
 | 
					
						
							| 
									
										
										
										
											2009-07-10 20:22:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-22 23:08:45 -08:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | #include <windows.h>
 | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2015-09-04 10:15:16 -05:00
										 |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-02 21:52:43 +00:00
										 |  |  | #ifdef __APPLE__
 | 
					
						
							|  |  |  | #include <mach/mach.h>
 | 
					
						
							| 
									
										
										
										
											2016-07-17 04:09:46 -06:00
										 |  |  | #elif defined BSD4_4 || defined __FreeBSD__ || defined __OpenBSD__
 | 
					
						
							| 
									
										
										
										
											2011-02-02 21:52:43 +00:00
										 |  |  | #include <pthread_np.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-12 01:36:29 +01:00
										 |  |  | #ifdef USE_VTUNE
 | 
					
						
							|  |  |  | #include <ittnotify.h>
 | 
					
						
							|  |  |  | #pragma comment(lib, "libittnotify.lib")
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | namespace Common | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-01-27 20:47:58 +00:00
										 |  |  | int CurrentThreadId() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-04-29 13:46:20 +00:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return GetCurrentThreadId(); | 
					
						
							| 
									
										
										
										
											2011-02-02 21:52:43 +00:00
										 |  |  | #elif defined __APPLE__
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return mach_thread_self(); | 
					
						
							| 
									
										
										
										
											2010-04-29 13:46:20 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											2010-04-29 13:46:20 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2011-01-27 20:47:58 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2011-01-27 20:47:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   SetThreadAffinityMask(thread, mask); | 
					
						
							| 
									
										
										
										
											2011-01-27 20:47:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SetCurrentThreadAffinity(u32 mask) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   SetThreadAffinityMask(GetCurrentThread(), mask); | 
					
						
							| 
									
										
										
										
											2011-01-27 20:47:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | // Supporting functions
 | 
					
						
							|  |  |  | void SleepCurrentThread(int ms) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   Sleep(ms); | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-08-29 16:14:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | void SwitchCurrentThread() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   SwitchToThread(); | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-06-16 14:22:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | // 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
 | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | // 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) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   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) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | #else  // !WIN32, so must be POSIX threads
 | 
					
						
							| 
									
										
										
										
											2011-01-27 20:47:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-02 21:52:43 +00:00
										 |  |  | void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask) | 
					
						
							| 
									
										
										
										
											2013-04-15 16:28:55 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-02-02 21:52:43 +00:00
										 |  |  | #ifdef __APPLE__
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   thread_policy_set(pthread_mach_thread_np(thread), THREAD_AFFINITY_POLICY, (integer_t*)&mask, 1); | 
					
						
							| 
									
										
										
										
											2015-06-10 22:15:11 -07:00
										 |  |  | #elif (defined __linux__ || defined BSD4_4 || defined __FreeBSD__) && !(defined ANDROID)
 | 
					
						
							|  |  |  | #ifdef __FreeBSD__
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   cpuset_t cpu_set; | 
					
						
							| 
									
										
										
										
											2015-06-10 22:15:11 -07:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   cpu_set_t cpu_set; | 
					
						
							| 
									
										
										
										
											2015-06-10 22:15:11 -07:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   CPU_ZERO(&cpu_set); | 
					
						
							| 
									
										
										
										
											2013-04-15 16:28:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   for (int i = 0; i != sizeof(mask) * 8; ++i) | 
					
						
							|  |  |  |     if ((mask >> i) & 1) | 
					
						
							|  |  |  |       CPU_SET(i, &cpu_set); | 
					
						
							| 
									
										
										
										
											2013-04-15 16:28:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   pthread_setaffinity_np(thread, sizeof(cpu_set), &cpu_set); | 
					
						
							| 
									
										
										
										
											2011-01-27 20:47:58 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SetCurrentThreadAffinity(u32 mask) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   SetThreadAffinity(pthread_self(), mask); | 
					
						
							| 
									
										
										
										
											2011-01-27 20:47:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | void SleepCurrentThread(int ms) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   usleep(1000 * ms); | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-08-29 16:14:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | void SwitchCurrentThread() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   usleep(1000 * 1); | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-06-16 14:22:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | void SetCurrentThreadName(const char* szThreadName) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-08-29 16:14:08 -04:00
										 |  |  | #ifdef __APPLE__
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   pthread_setname_np(szThreadName); | 
					
						
							| 
									
										
										
										
											2016-07-17 04:09:46 -06:00
										 |  |  | #elif defined __FreeBSD__ || defined __OpenBSD__
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   pthread_set_name_np(pthread_self(), szThreadName); | 
					
						
							| 
									
										
										
										
											2013-08-29 16:14:08 -04:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   // linux doesn't allow to set more than 16 bytes, including \0.
 | 
					
						
							|  |  |  |   pthread_setname_np(pthread_self(), std::string(szThreadName).substr(0, 15).c_str()); | 
					
						
							| 
									
										
										
										
											2013-08-29 16:14:08 -04:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-02-12 01:36:29 +01:00
										 |  |  | #ifdef USE_VTUNE
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   // VTune uses OS thread names by default but probably supports longer names when set via its own
 | 
					
						
							|  |  |  |   // API.
 | 
					
						
							|  |  |  |   __itt_thread_set_name(szThreadName); | 
					
						
							| 
									
										
										
										
											2015-02-12 01:36:29 +01:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-07-06 16:16:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2013-08-29 16:14:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | }  // namespace Common
 |