| 
									
										
										
										
											2009-07-28 21:32:10 +00:00
										 |  |  | // Copyright (C) 2003 Dolphin Project.
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // 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.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 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/
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-22 21:16:12 +00:00
										 |  |  | #include "Setup.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #include "Thread.h"
 | 
					
						
							| 
									
										
										
										
											2009-08-06 06:18:22 +00:00
										 |  |  | #include "Common.h"
 | 
					
						
							| 
									
										
										
										
											2009-07-10 20:22:25 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-02 21:52:43 +00:00
										 |  |  | #ifdef __APPLE__
 | 
					
						
							|  |  |  | #include <mach/mach.h>
 | 
					
						
							|  |  |  | #elif defined BSD4_4
 | 
					
						
							|  |  |  | #include <pthread_np.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-10 20:22:25 +00:00
										 |  |  | #ifdef USE_BEGINTHREADEX
 | 
					
						
							|  |  |  | #include <process.h>
 | 
					
						
							|  |  |  | #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
 | 
					
						
							| 
									
										
										
										
											2011-01-27 20:47:58 +00:00
										 |  |  | 	return GetCurrentThreadId(); | 
					
						
							| 
									
										
										
										
											2011-02-02 21:52:43 +00:00
										 |  |  | #elif defined __APPLE__
 | 
					
						
							|  |  |  | 	return mach_thread_self(); | 
					
						
							| 
									
										
										
										
											2010-04-29 13:46:20 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2011-01-27 20:47:58 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2010-04-29 13:46:20 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2011-01-27 20:47:58 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-04-29 13:46:20 +00: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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	SetThreadAffinityMask(thread, mask); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SetCurrentThreadAffinity(u32 mask) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	SetThreadAffinityMask(GetCurrentThread(), mask); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | // Supporting functions
 | 
					
						
							|  |  |  | void SleepCurrentThread(int ms) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Sleep(ms); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-04-29 13:46:20 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | void SwitchCurrentThread() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	SwitchToThread(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2010-04-29 13:46:20 +00: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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	static const DWORD MS_VC_EXCEPTION = 0x406D1388; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	#pragma pack(push,8)
 | 
					
						
							|  |  |  | 	struct THREADNAME_INFO | 
					
						
							| 
									
										
										
										
											2010-04-29 13:46:20 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		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
 | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | 	} info; | 
					
						
							|  |  |  | 	#pragma pack(pop)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	info.dwType = 0x1000; | 
					
						
							|  |  |  | 	info.szName = szThreadName; | 
					
						
							|  |  |  | 	info.dwThreadID = -1; //dwThreadID;
 | 
					
						
							|  |  |  | 	info.dwFlags = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	__try | 
					
						
							| 
									
										
										
										
											2010-04-29 13:46:20 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | 		RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | 	__except(EXCEPTION_CONTINUE_EXECUTION) | 
					
						
							|  |  |  | 	{} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-04-29 13:46:20 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-01-09 12:10:02 +00: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) | 
					
						
							|  |  |  | {                 | 
					
						
							|  |  |  | #ifdef __APPLE__
 | 
					
						
							|  |  |  | 	thread_policy_set(pthread_mach_thread_np(thread), | 
					
						
							|  |  |  | 		THREAD_AFFINITY_POLICY, (integer_t *)&mask, 1); | 
					
						
							|  |  |  | #elif defined __linux__ || defined BSD4_4
 | 
					
						
							| 
									
										
										
										
											2011-01-27 20:47:58 +00:00
										 |  |  | 	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); | 
					
						
							|  |  |  |                  | 
					
						
							|  |  |  | 	pthread_setaffinity_np(thread, sizeof(cpu_set), &cpu_set); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SetCurrentThreadAffinity(u32 mask) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-02-02 21:52:43 +00:00
										 |  |  | 	SetThreadAffinity(pthread_self(), mask); | 
					
						
							| 
									
										
										
										
											2011-01-27 20:47:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | static pthread_key_t threadname_key; | 
					
						
							|  |  |  | static pthread_once_t threadname_key_once = PTHREAD_ONCE_INIT; | 
					
						
							| 
									
										
										
										
											2010-06-16 14:22:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | void SleepCurrentThread(int ms) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	usleep(1000 * ms); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-04-29 13:46:20 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | void SwitchCurrentThread() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	usleep(1000 * 1); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-06-16 14:22:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | static void FreeThreadName(void* threadname) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	free(threadname); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-07-06 16:16:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | static void ThreadnameKeyAlloc() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	pthread_key_create(&threadname_key, FreeThreadName); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-07-06 16:16:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | void SetCurrentThreadName(const char* szThreadName) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	pthread_once(&threadname_key_once, ThreadnameKeyAlloc); | 
					
						
							| 
									
										
										
										
											2010-07-06 16:16:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | 	void* threadname; | 
					
						
							|  |  |  | 	if ((threadname = pthread_getspecific(threadname_key)) != NULL) | 
					
						
							|  |  |  | 		free(threadname); | 
					
						
							| 
									
										
										
										
											2010-07-06 16:16:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | 	pthread_setspecific(threadname_key, strdup(szThreadName)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	INFO_LOG(COMMON, "%s(%s)\n", __FUNCTION__, szThreadName); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-07-06 16:16:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2010-04-29 13:46:20 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | } // namespace Common
 |