Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // G4Threading << 26 // $Id$ 27 // 27 // 28 // Description: << 28 // --------------------------------------------------------------- >> 29 // GEANT 4 class header file 29 // 30 // 30 // This unit defines types and macros used to << 31 // Class Description: >> 32 // >> 33 // This file defines types and macros used to expose Geant4 threading model. 31 34 32 // Author: Andrea Dotti, 15 February 2013 - Fi << 35 // --------------------------------------------------------------- 33 // Revision: Jonathan R. Madsen, 21 February 2 << 36 // Author: Andrea Dotti (15 Feb 2013): First Implementation 34 // ------------------------------------------- << 37 // --------------------------------------------------------------- 35 #ifndef G4Threading_hh 38 #ifndef G4Threading_hh 36 #define G4Threading_hh 1 << 39 #define G4Threading_hh 37 40 38 #include "G4Types.hh" 41 #include "G4Types.hh" 39 #include "globals.hh" << 40 << 41 #include <chrono> << 42 #include <condition_variable> << 43 #include <future> << 44 #include <mutex> << 45 #include <thread> << 46 #include <vector> << 47 << 48 // Macro to put current thread to sleep << 49 // << 50 #define G4THREADSLEEP(tick) << 51 std::this_thread::sleep_for(std::chrono::sec << 52 << 53 // Will be used in the future when migrating t << 54 template <typename _Tp> << 55 using G4Future = std::future<_Tp>; << 56 template <typename _Tp> << 57 using G4SharedFuture = std::shared_future<_Tp> << 58 template <typename _Tp> << 59 using G4Promise = std::promise<_Tp>; << 60 << 61 // NOTE ON GEANT4 SERIAL BUILDS AND M << 62 // ================================== << 63 // << 64 // G4Mutex and G4RecursiveMutex are always C++ << 65 // however, in serial mode, using G4MUTEXLOCK << 66 // types has no effect -- i.e. the mutexes are << 67 // << 68 // Additionally, when a G4Mutex or G4Recursive << 69 // and G4RecursiveAutoLock, respectively, thes << 70 // the locking and unlocking of the mutex. Reg << 71 // G4AutoLock and G4RecursiveAutoLock inherit << 72 // and std::unique_lock<std::recursive_mutex>, << 73 // that in situations (such as is needed by th << 74 // G4AutoLock and G4RecursiveAutoLock can be p << 75 // a std::unique_lock. Within these functions, << 76 // member functions are not virtual, they will << 77 // and unlocking behavior << 78 // --> An example of this behavior can be foun << 79 << 80 // Global mutex types << 81 using G4Mutex = std::mutex; << 82 using G4RecursiveMutex = std::recursive_mutex; << 83 << 84 // Mutex macros << 85 #define G4MUTEX_INITIALIZER << 86 {} << 87 #define G4MUTEXINIT(mutex) << 88 ; << 89 ; << 90 #define G4MUTEXDESTROY(mutex) << 91 ; << 92 ; << 93 << 94 // Static functions: get_id(), sleep_for(...), << 95 namespace G4ThisThread << 96 { << 97 using namespace std::this_thread; << 98 } << 99 << 100 // Will be used in the future when migrating t << 101 // and are currently used in unit tests << 102 template <typename _Tp> << 103 using G4Promise = std::promise<_Tp>; << 104 template <typename _Tp> << 105 using G4Future = std::future<_Tp>; << 106 template <typename _Tp> << 107 using G4SharedFuture = std::shared_future<_Tp> << 108 << 109 // Some useful types << 110 using G4ThreadFunReturnType = void*; << 111 using G4ThreadFunArgType = void*; << 112 using thread_lock = << 113 G4int (*)(G4Mutex*); // typedef G4int (*thr << 114 using thread_unlock = << 115 G4int (*)(G4Mutex*); // typedef G4int (*thr << 116 << 117 // Helper function for getting a unique static << 118 // class or type << 119 // Usage example: << 120 // a template class "G4Cache<T>" that requir << 121 // mutex for specific to type T: << 122 // G4AutoLock l(G4TypeMutex<G4Cache<T>>() << 123 template <typename _Tp> << 124 G4Mutex& G4TypeMutex() << 125 { << 126 static G4Mutex _mutex; << 127 return _mutex; << 128 } << 129 << 130 // Helper function for getting a unique static << 131 // specific class or type << 132 // Usage example: << 133 // a template class "G4Cache<T> << 134 // recursive_mutex for specific << 135 // G4RecursiveAutoLock << 136 // l(G4TypeRecursiveMut << 137 template <typename _Tp> << 138 G4RecursiveMutex& G4TypeRecursiveMutex() << 139 { << 140 static G4RecursiveMutex _mutex; << 141 return _mutex; << 142 } << 143 42 144 #if defined(G4MULTITHREADED) 43 #if defined(G4MULTITHREADED) 145 //========================================== << 44 //=============================== 146 // G4MULTITHREADED is ON - threading enabled << 45 // Multi-threaded build 147 //========================================== << 46 //=============================== 148 << 47 #if ( defined(__MACH__) && defined(__clang__) && defined(__x86_64__) ) || \ 149 // global thread types << 48 ( defined(__MACH__) && defined(__GNUC__) && __GNUC__>=4 && __GNUC_MINOR__>=7 ) || \ 150 using G4Thread = std::thread; << 49 defined(__linux__) || defined(_AIX) 151 using G4NativeThread = std::thread::native_han << 50 // 152 << 51 // Multi-threaded build: for POSIX systems 153 // mutex macros << 52 // 154 # define G4MUTEXLOCK(mutex) << 53 #include <pthread.h> 155 { << 54 #if defined(__MACH__) // needed only for MacOSX for definition of pid_t 156 (mutex)->lock(); << 55 #include <sys/types.h> 157 } << 56 #endif 158 # define G4MUTEXUNLOCK(mutex) << 57 159 { << 58 typedef pthread_mutex_t G4Mutex; 160 (mutex)->unlock(); << 59 typedef pthread_t G4Thread; >> 60 >> 61 // G4Mutex initializer macro >> 62 // >> 63 #define G4MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER >> 64 >> 65 // Lock/unlock a G4Mutex function name >> 66 // >> 67 #define G4MUTEXLOCK pthread_mutex_lock >> 68 #define G4MUTEXUNLOCK pthread_mutex_unlock >> 69 >> 70 // Macro to iniaizlie a Mutex >> 71 #define G4MUTEXINIT(mutex) pthread_mutex_init( &mutex , NULL); >> 72 #define G4MUTEXDESTROY(mutex) pthread_mutex_destroy( &mutex ); >> 73 >> 74 // Macro to create a G4Thread object >> 75 // >> 76 #define G4THREADCREATE( worker , func , arg ) { \ >> 77 pthread_attr_t attr; \ >> 78 pthread_attr_init(&attr); \ >> 79 pthread_attr_setstacksize(&attr,16*1024*1024); \ >> 80 pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE); \ >> 81 pthread_create( worker, &attr, func , arg ); \ 161 } 82 } 162 83 163 // Macro to join thread << 84 // Macro to join thread 164 # define G4THREADJOIN(worker) (worker).join() << 85 #define G4THREADJOIN( worker ) pthread_join( worker , NULL) 165 86 166 // std::thread::id does not cast to integer << 87 // Macro to retrieve caller thread 167 using G4Pid_t = std::thread::id; << 88 #define G4THREADSELF pthread_self 168 89 169 // Instead of previous macro taking one argume << 90 // Some useful types 170 // unlimited arguments << 91 // 171 template <typename _Worker, typename _Func, ty << 92 typedef void* G4ThreadFunReturnType; 172 void G4THREADCREATE(_Worker*& worker, _Func fu << 93 typedef void* G4ThreadFunArgType; 173 { << 94 typedef G4int (*thread_lock)(G4Mutex*); 174 *worker = G4Thread(func, std::forward<_Args> << 95 typedef G4int (*thread_unlock)(G4Mutex*); 175 } << 96 176 << 97 typedef pid_t G4Pid_t; 177 // Conditions << 98 178 // << 99 // Conditions 179 // See G4MTRunManager for example on how to us << 100 // 180 // << 101 // See G4MTRunManager for example on how to use these 181 using G4Condition = std::condition_variable; << 102 // This complication is needed to be portable with WIN32 182 # define G4CONDITION_INITIALIZER << 103 // Note that WIN32 requires an additional initialization step. 183 {} << 104 // See example code 184 # define G4CONDITIONWAIT(cond, lock) (cond)-> << 105 // 185 # define G4CONDITIONWAITLAMBDA(cond, lock, la << 106 typedef pthread_cond_t G4Condition; 186 # define G4CONDITIONNOTIFY(cond) (cond)->noti << 107 #define G4CONDITION_INITIALIZER PTHREAD_COND_INITIALIZER 187 # define G4CONDITIONBROADCAST(cond) (cond)->n << 108 188 // << 109 #define G4CONDITIONWAIT( cond, mutex ) pthread_cond_wait( cond , mutex ); 189 // we don't define above globally so single-th << 110 #define G4CONDTIONBROADCAST( cond ) pthread_cond_broadcast( cond ); 190 // caught in condition with no other thread to << 111 191 // << 112 #elif defined(WIN32) 192 << 113 // 193 #else << 114 // Multi-threaded build: for Windows systems 194 //========================================== << 115 // 195 // G4MULTITHREADED is OFF - Sequential build << 116 #include "windefs.hh" // Include 'safe...' <windows.h> 196 //========================================== << 117 197 << 118 typedef HANDLE G4Mutex; 198 // implement a dummy thread class that acts li << 119 typedef HANDLE G4Thread; 199 class G4DummyThread << 120 200 { << 121 #define G4MUTEX_INITIALIZER CreateMutex(NULL,FALSE,NULL) 201 public: << 122 DWORD /*WINAPI*/ G4WaitForSingleObjectInf( __in G4Mutex m ); 202 using native_handle_type = G4int; << 123 #define G4MUTEXLOCK G4WaitForSingleObjectInf 203 using id = std::thread::id; << 124 204 << 125 // #define G4MUTEXINIT(mutex) InitializeCriticalSection( &mutex ); 205 public: << 126 #define G4MUTEXINIT(mutex); 206 // does nothing << 127 #define G4MUTEXDESTROY(mutex); 207 G4DummyThread() {} << 128 208 // a std::thread-like constructor that execu << 129 // Not clear why following two lines are needed... 209 template <typename _Func, typename... _Args> << 130 // 210 G4DummyThread(_Func func, _Args&&... _args) << 131 BOOL G4ReleaseMutex( __in G4Mutex m); 211 { << 132 #define G4MUTEXUNLOCK G4ReleaseMutex 212 func(std::forward<_Args>(_args)...); << 133 213 } << 134 #define G4THREADCREATE( worker, func, arg ) { *worker = CreateThread( NULL, 16*1024*1024 , func , arg , 0 , NULL ); } 214 << 135 #define G4THREADJOIN( worker ) WaitForSingleObject( worker , INFINITE); 215 public: << 136 #define G4THREADSELF GetCurrentThreadId 216 native_handle_type native_handle() const { r << 137 #define G4ThreadFunReturnType DWORD WINAPI 217 G4bool joinable() const { return true; } << 138 typedef LPVOID G4ThreadFunArgType; 218 id get_id() const noexcept { return std::thi << 139 typedef DWORD (*thread_lock)(G4Mutex); 219 void swap(G4DummyThread&) {} << 140 typedef BOOL (*thread_unlock)(G4Mutex); 220 void join() {} << 141 typedef DWORD G4Pid_t; 221 void detach() {} << 142 222 << 143 // Conditions 223 public: << 144 // 224 static unsigned int hardware_concurrency() n << 145 typedef CONDITION_VARIABLE G4Condition; 225 { << 146 #define G4CONDITION_INITIALIZER CONDITION_VARIABLE_INIT 226 return std::thread::hardware_concurrency() << 147 227 } << 148 #define G4CONDITIONWAIT( cond , criticalsectionmutex ) SleepConditionVariableCS( cond, criticalsectionmutex , INFINITE ); 228 }; << 149 #define G4CONDTIONBROADCAST( cond ) WakeAllConditionVariable( cond ); 229 << 150 230 // global thread types << 151 #else 231 using G4Thread = G4DummyThread; << 152 232 using G4NativeThread = G4DummyThread::native_h << 153 #error "No Threading model technology supported for this platform. Use sequential build !" 233 << 154 234 // mutex macros << 155 #endif 235 # define G4MUTEXLOCK(mutex) << 156 236 ; << 157 #else 237 ; << 158 //========================================== 238 # define G4MUTEXUNLOCK(mutex) << 159 // G4MULTITHREADED is OFF - Sequential build 239 ; << 160 //========================================== 240 ; << 161 typedef G4int G4Mutex; 241 << 162 typedef G4int G4Thread; 242 // Macro to join thread << 163 #define G4MUTEX_INITIALIZER 1 243 # define G4THREADJOIN(worker) << 164 G4int fake_mutex_lock_unlock( G4Mutex* );// { return 0; } 244 ; << 165 #define G4MUTEXINIT(mutex) ;; 245 ; << 166 #define G4MUTEXDESTROY(mutex) ;; 246 << 167 #define G4MUTEXLOCK fake_mutex_lock_unlock 247 using G4Pid_t = G4int; << 168 #define G4MUTEXUNLOCK fake_mutex_lock_unlock 248 << 169 #define G4THREADCREATE( worker , func , arg ) ;; 249 // Instead of previous macro taking one argume << 170 #define G4THREADJOIN( worker ) ;; 250 // unlimited arguments << 171 #define G4THREADSELF( nothing ) G4Thread(nothing); 251 template <typename _Worker, typename _Func, ty << 172 typedef void* G4ThreadFunReturnType; 252 void G4THREADCREATE(_Worker*& worker, _Func fu << 173 typedef void* G4ThreadFunArgType; 253 { << 174 typedef G4int (*thread_lock)(G4Mutex*); 254 *worker = G4Thread(func, std::forward<_Args> << 175 typedef G4int (*thread_unlock)(G4Mutex*); 255 } << 176 typedef G4int G4Pid_t; 256 << 177 typedef G4int G4Condition; 257 using G4Condition = G4int; << 178 #define G4CONDITION_INITIALIZER 1 258 # define G4CONDITION_INITIALIZER 1 << 179 #define G4CONDITIONWAIT( cond, mutex ) ;; 259 # define G4CONDITIONWAIT(cond, mutex) G4Consu << 180 #define G4CONDTIONBROADCAST( cond ) ;; 260 # define G4CONDITIONWAITLAMBDA(cond, mutex, l << 261 G4ConsumeParameters(cond, mutex, lambda); << 262 # define G4CONDITIONNOTIFY(cond) G4ConsumePar << 263 # define G4CONDITIONBROADCAST(cond) G4Consume << 264 << 265 #endif // G4MULTITHREADING << 266 181 267 //============================================ << 182 #endif //G4MULTITHREADING 268 << 269 // Define here after G4Thread has been typedef << 270 using G4ThreadId = G4Thread::id; << 271 << 272 //============================================ << 273 183 274 namespace G4Threading 184 namespace G4Threading 275 { 185 { 276 enum << 186 enum { 277 { << 187 SEQUENTIAL_ID = -2, 278 SEQUENTIAL_ID = -2, << 188 MASTER_ID = -1, 279 MASTER_ID = -1, << 189 WORKER_ID = 0, 280 WORKER_ID = 0, << 190 GENERICTHREAD_ID = -1000 281 GENERICTHREAD_ID = -1000 << 191 }; 282 }; << 283 << 284 G4Pid_t G4GetPidId(); 192 G4Pid_t G4GetPidId(); 285 G4int G4GetNumberOfCores(); 193 G4int G4GetNumberOfCores(); 286 G4int G4GetThreadId(); 194 G4int G4GetThreadId(); 287 G4bool IsWorkerThread(); 195 G4bool IsWorkerThread(); 288 G4bool IsMasterThread(); << 196 void G4SetThreadId( G4int aNewValue ); 289 void G4SetThreadId(G4int aNewValue); << 197 G4bool G4SetPinAffinity( G4int idx , G4Thread& at); 290 G4bool G4SetPinAffinity(G4int idx, G4NativeT << 291 void SetMultithreadedApplication(G4bool valu 198 void SetMultithreadedApplication(G4bool value); 292 G4bool IsMultithreadedApplication(); 199 G4bool IsMultithreadedApplication(); 293 G4int WorkerThreadLeavesPool(); << 200 } 294 G4int WorkerThreadJoinsPool(); << 295 G4int GetNumberOfRunningWorkerThreads(); << 296 } // namespace G4Threading << 297 201 298 #endif // G4Threading_hh << 202 #endif //G4Threading_hh 299 203