DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

sys_threading.h (5781B)


      1 /*
      2 ===========================================================================
      3 
      4 Doom 3 BFG Edition GPL Source Code
      5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 
      6 
      7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").  
      8 
      9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
     10 it under the terms of the GNU General Public License as published by
     11 the Free Software Foundation, either version 3 of the License, or
     12 (at your option) any later version.
     13 
     14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
     15 but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 GNU General Public License for more details.
     18 
     19 You should have received a copy of the GNU General Public License
     20 along with Doom 3 BFG Edition Source Code.  If not, see <http://www.gnu.org/licenses/>.
     21 
     22 In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code.  If not, please request a copy in writing from id Software at the address below.
     23 
     24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
     25 
     26 ===========================================================================
     27 */
     28 #ifndef __SYS_THREADING_H__
     29 #define __SYS_THREADING_H__
     30 
     31 #ifndef __TYPEINFOGEN__
     32 
     33 /*
     34 ================================================================================================
     35 
     36 	Platform specific mutex, signal, atomic integer and memory barrier.
     37 
     38 ================================================================================================
     39 */
     40 
     41 	typedef CRITICAL_SECTION		mutexHandle_t;
     42 	typedef HANDLE					signalHandle_t;
     43 	typedef LONG					interlockedInt_t;
     44 
     45 	// _ReadWriteBarrier() does not translate to any instructions but keeps the compiler
     46 	// from reordering read and write instructions across the barrier.
     47 	// MemoryBarrier() inserts and CPU instruction that keeps the CPU from reordering reads and writes.
     48 	#pragma intrinsic(_ReadWriteBarrier)
     49 	#define SYS_MEMORYBARRIER		_ReadWriteBarrier(); MemoryBarrier()
     50 
     51 
     52 
     53 
     54 
     55 /*
     56 ================================================================================================
     57 
     58 	Platform specific thread local storage.
     59 	Can be used to store either a pointer or an integer.
     60 
     61 ================================================================================================
     62 */
     63 
     64 
     65 	class idSysThreadLocalStorage {
     66 	public:
     67 		idSysThreadLocalStorage() { 
     68 			tlsIndex = TlsAlloc();
     69 		}
     70 		idSysThreadLocalStorage( const ptrdiff_t &val ) {
     71 			tlsIndex = TlsAlloc();
     72 			TlsSetValue( tlsIndex, (LPVOID)val );
     73 		}
     74 		~idSysThreadLocalStorage() {
     75 			TlsFree( tlsIndex );
     76 		}
     77 		operator ptrdiff_t() {
     78 			return (ptrdiff_t)TlsGetValue( tlsIndex );
     79 		}
     80 		const ptrdiff_t & operator = ( const ptrdiff_t &val ) {
     81 			TlsSetValue( tlsIndex, (LPVOID)val );
     82 			return val;
     83 		}	
     84 		DWORD	tlsIndex;
     85 	};
     86 
     87 #define ID_TLS idSysThreadLocalStorage
     88 
     89 
     90 #endif // __TYPEINFOGEN__
     91 
     92 /*
     93 ================================================================================================
     94 
     95 	Platform independent threading functions.
     96 
     97 ================================================================================================
     98 */
     99 
    100 enum core_t {
    101 	CORE_ANY = -1,
    102 	CORE_0A,
    103 	CORE_0B,
    104 	CORE_1A,
    105 	CORE_1B,
    106 	CORE_2A,
    107 	CORE_2B
    108 };
    109 
    110 typedef unsigned int (*xthread_t)( void * );
    111 
    112 enum xthreadPriority {
    113 	THREAD_LOWEST,
    114 	THREAD_BELOW_NORMAL,
    115 	THREAD_NORMAL,
    116 	THREAD_ABOVE_NORMAL,
    117 	THREAD_HIGHEST
    118 };
    119 
    120 #define DEFAULT_THREAD_STACK_SIZE		( 256 * 1024 )
    121 
    122 // on win32, the threadID is NOT the same as the threadHandle
    123 uintptr_t			Sys_GetCurrentThreadID();
    124 
    125 // returns a threadHandle
    126 uintptr_t			Sys_CreateThread( xthread_t function, void *parms, xthreadPriority priority, 
    127 									  const char *name, core_t core, int stackSize = DEFAULT_THREAD_STACK_SIZE, 
    128 									  bool suspended = false );
    129 
    130 void				Sys_WaitForThread( uintptr_t threadHandle );
    131 void				Sys_DestroyThread( uintptr_t threadHandle );
    132 void				Sys_SetCurrentThreadName( const char *name );
    133 
    134 void				Sys_SignalCreate( signalHandle_t & handle, bool manualReset );
    135 void				Sys_SignalDestroy( signalHandle_t & handle );
    136 void				Sys_SignalRaise( signalHandle_t & handle );
    137 void				Sys_SignalClear( signalHandle_t & handle );
    138 bool				Sys_SignalWait( signalHandle_t & handle, int timeout );
    139 
    140 void				Sys_MutexCreate( mutexHandle_t & handle );
    141 void				Sys_MutexDestroy( mutexHandle_t & handle );
    142 bool				Sys_MutexLock( mutexHandle_t & handle, bool blocking );
    143 void				Sys_MutexUnlock( mutexHandle_t & handle );
    144 
    145 interlockedInt_t	Sys_InterlockedIncrement( interlockedInt_t & value );
    146 interlockedInt_t	Sys_InterlockedDecrement( interlockedInt_t & value );
    147 
    148 interlockedInt_t	Sys_InterlockedAdd( interlockedInt_t & value, interlockedInt_t i );
    149 interlockedInt_t	Sys_InterlockedSub( interlockedInt_t & value, interlockedInt_t i );
    150 
    151 interlockedInt_t	Sys_InterlockedExchange( interlockedInt_t & value, interlockedInt_t exchange );
    152 interlockedInt_t	Sys_InterlockedCompareExchange( interlockedInt_t & value, interlockedInt_t comparand, interlockedInt_t exchange );
    153 
    154 void *				Sys_InterlockedExchangePointer( void * & ptr, void * exchange );
    155 void *				Sys_InterlockedCompareExchangePointer( void * & ptr, void * comparand, void * exchange );
    156 
    157 void				Sys_Yield();
    158 
    159 const int MAX_CRITICAL_SECTIONS		= 4;
    160 
    161 enum {
    162 	CRITICAL_SECTION_ZERO = 0,
    163 	CRITICAL_SECTION_ONE,
    164 	CRITICAL_SECTION_TWO,
    165 	CRITICAL_SECTION_THREE
    166 };
    167 
    168 #endif	// !__SYS_THREADING_H__