sys_threading.h 5.6 KB

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