| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | // Copyright (C) 2003-2009 Dolphin Project.
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +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-03-28 08:57:34 +00:00
										 |  |  | #ifndef _THREAD_H_
 | 
					
						
							|  |  |  | #define _THREAD_H_
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2009-01-16 02:58:34 +00:00
										 |  |  | #include <windows.h>
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | #define THREAD_RETURN DWORD WINAPI
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | #define THREAD_RETURN void*
 | 
					
						
							| 
									
										
										
										
											2009-01-09 12:10:02 +00:00
										 |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #ifdef _POSIX_THREADS
 | 
					
						
							|  |  |  | #include <pthread.h>
 | 
					
						
							| 
									
										
										
										
											2009-05-01 15:17:03 +00:00
										 |  |  | #elif GEKKO
 | 
					
						
							|  |  |  | #include <ogc/lwp_threads.h>
 | 
					
						
							| 
									
										
										
										
											2009-01-09 12:10:02 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  | #error unsupported platform (no pthreads?)
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-18 17:17:58 +00:00
										 |  |  | // Don't include common.h here as it will break LogManager
 | 
					
						
							|  |  |  | #include "CommonTypes.h"
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-24 04:54:00 +00:00
										 |  |  | // This may not be defined outside _WIN32
 | 
					
						
							|  |  |  | #ifndef _WIN32
 | 
					
						
							|  |  |  | 	#ifndef INFINITE
 | 
					
						
							|  |  |  | 	#define INFINITE 0xffffffff
 | 
					
						
							|  |  |  | 	#endif
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-22 21:16:12 +00:00
										 |  |  | // -----------------------------------------
 | 
					
						
							|  |  |  | #ifdef SETUP_TIMER_WAITING
 | 
					
						
							|  |  |  | // -----------------
 | 
					
						
							|  |  |  | 	typedef void (*EventCallBack)(void); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | // ----------------------
 | 
					
						
							| 
									
										
										
										
											2009-02-24 04:54:00 +00:00
										 |  |  | ///////////////////////////////////
 | 
					
						
							| 
									
										
										
										
											2009-02-22 21:16:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | namespace Common | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | class CriticalSection | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | 	CRITICAL_SECTION section; | 
					
						
							| 
									
										
										
										
											2009-01-09 12:10:02 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2009-05-01 15:17:03 +00:00
										 |  |  | #ifdef _POSIX_THREADS
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | 	pthread_mutex_t mutex; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-05-01 15:17:03 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	CriticalSection(int spincount = 1000); | 
					
						
							|  |  |  | 	~CriticalSection(); | 
					
						
							|  |  |  | 	void Enter(); | 
					
						
							|  |  |  | 	bool TryEnter(); | 
					
						
							|  |  |  | 	void Leave(); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | typedef DWORD (WINAPI * ThreadFunc)(void* arg); | 
					
						
							| 
									
										
										
										
											2009-01-09 12:10:02 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | typedef void* (*ThreadFunc)(void* arg); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Thread | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 	Thread(ThreadFunc entry, void* arg); | 
					
						
							|  |  |  | 	~Thread(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void SetAffinity(int mask); | 
					
						
							|  |  |  | 	static void SetCurrentThreadAffinity(int mask); | 
					
						
							| 
									
										
										
										
											2009-02-24 05:20:52 +00:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2009-02-24 03:03:11 +00:00
										 |  |  | 	void WaitForDeath(const int _Wait = INFINITE); | 
					
						
							| 
									
										
										
										
											2009-02-24 05:20:52 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  | 	void WaitForDeath(); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | 	HANDLE m_hThread; | 
					
						
							|  |  |  | 	DWORD m_threadId; | 
					
						
							| 
									
										
										
										
											2009-01-09 12:10:02 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2009-05-01 15:17:03 +00:00
										 |  |  | #ifdef _POSIX_THREADS
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | 	pthread_t thread_id; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-05-01 15:17:03 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-22 21:16:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | class Event | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-02-20 22:04:52 +00:00
										 |  |  | public: | 
					
						
							|  |  |  | 	Event(); | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-20 22:04:52 +00:00
										 |  |  | 	void Init(); | 
					
						
							|  |  |  | 	void Shutdown(); | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-20 22:04:52 +00:00
										 |  |  | 	void Set(); | 
					
						
							|  |  |  | 	void Wait(); | 
					
						
							| 
									
										
										
										
											2009-02-22 22:49:42 +00:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | 	void MsgWait(); | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | 	void MsgWait() {Wait();} | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-22 21:16:12 +00:00
										 |  |  | 	#ifdef SETUP_TIMER_WAITING
 | 
					
						
							|  |  |  | 		bool TimerWait(EventCallBack WaitCB, int Id = 0, bool OptCondition = true); | 
					
						
							|  |  |  | 		bool DoneWait(); | 
					
						
							|  |  |  | 		void SetTimer(); | 
					
						
							|  |  |  | 		bool DoneWaiting; | 
					
						
							|  |  |  | 		bool StartWait; | 
					
						
							|  |  |  | 		int Id; | 
					
						
							|  |  |  | 		HANDLE hTimer; | 
					
						
							|  |  |  | 		HANDLE hTimerQueue;		 | 
					
						
							|  |  |  | 	#endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-20 22:04:52 +00:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2009-02-20 22:04:52 +00:00
										 |  |  | 	HANDLE m_hEvent; | 
					
						
							| 
									
										
										
										
											2009-02-23 00:15:19 +00:00
										 |  |  | 	/* If we have waited more than five seconds we can be pretty sure that the thread is deadlocked.
 | 
					
						
							|  |  |  | 	   So then we can just as well continue and hope for the best. I could try several times that | 
					
						
							|  |  |  | 	   this works after a five second timeout (with works meaning that the game stopped and I could | 
					
						
							|  |  |  | 	   start another game without any noticable problems). But several times it failed to, and ended | 
					
						
							|  |  |  | 	   with a crash. But it's better than an infinite deadlock. */ | 
					
						
							|  |  |  | 	static const int THREAD_WAIT_TIMEOUT = 5000; // INFINITE or 5000 for example
 | 
					
						
							| 
									
										
										
										
											2009-01-09 12:10:02 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2009-02-20 22:04:52 +00:00
										 |  |  | 	bool is_set_; | 
					
						
							| 
									
										
										
										
											2009-05-01 15:17:03 +00:00
										 |  |  | #ifdef _POSIX_THREADS
 | 
					
						
							| 
									
										
										
										
											2009-02-20 22:04:52 +00:00
										 |  |  | 	pthread_cond_t event_; | 
					
						
							|  |  |  | 	pthread_mutex_t mutex_; | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-05-01 15:17:03 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-20 22:04:52 +00:00
										 |  |  | void InitThreading(); | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | void SleepCurrentThread(int ms); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SetCurrentThreadName(const char *name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | LONG SyncInterlockedExchangeAdd(LONG *Dest, LONG Val); | 
					
						
							|  |  |  | LONG SyncInterlockedExchange(LONG *Dest, LONG Val); | 
					
						
							|  |  |  | LONG SyncInterlockedIncrement(LONG *Dest); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | } // namespace Common
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-07 21:16:52 +00:00
										 |  |  | #endif // _THREAD_H_
 |