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 << 27 // 26 // 28 // Description: << 27 // --------------------------------------------------------------- >> 28 // GEANT 4 class header file 29 // 29 // 30 // This unit defines types and macros used to << 30 // Class Description: >> 31 // >> 32 // This file defines types and macros used to expose Geant4 threading model. 31 33 32 // Author: Andrea Dotti, 15 February 2013 - Fi << 34 // --------------------------------------------------------------- 33 // Revision: Jonathan R. Madsen, 21 February 2 << 35 // Author: Andrea Dotti (15 Feb 2013): First Implementation 34 // ------------------------------------------- << 36 // --------------------------------------------------------------- 35 #ifndef G4Threading_hh 37 #ifndef G4Threading_hh 36 #define G4Threading_hh 1 << 38 #define G4Threading_hh 37 39 38 #include "G4Types.hh" << 39 #include "globals.hh" 40 #include "globals.hh" >> 41 #include "G4Types.hh" 40 42 41 #include <chrono> 43 #include <chrono> >> 44 #include <thread> >> 45 #include <mutex> 42 #include <condition_variable> 46 #include <condition_variable> 43 #include <future> 47 #include <future> 44 #include <mutex> << 45 #include <thread> << 46 #include <vector> 48 #include <vector> 47 49 48 // Macro to put current thread to sleep 50 // Macro to put current thread to sleep 49 // 51 // 50 #define G4THREADSLEEP(tick) << 52 #define G4THREADSLEEP(tick) \ 51 std::this_thread::sleep_for(std::chrono::sec << 53 std::this_thread::sleep_for(std::chrono::seconds( tick )) 52 54 53 // Will be used in the future when migrating t << 55 // will be used in the future when migrating threading to task-based style 54 template <typename _Tp> << 56 template <typename _Tp> using G4Future = std::future<_Tp>; 55 using G4Future = std::future<_Tp>; << 57 template <typename _Tp> using G4SharedFuture = std::shared_future<_Tp>; 56 template <typename _Tp> << 58 template <typename _Tp> using G4Promise = std::promise<_Tp>; 57 using G4SharedFuture = std::shared_future<_Tp> << 58 template <typename _Tp> << 59 using G4Promise = std::promise<_Tp>; << 60 59 >> 60 // 61 // NOTE ON GEANT4 SERIAL BUILDS AND M 61 // NOTE ON GEANT4 SERIAL BUILDS AND MUTEX/UNIQUE_LOCK 62 // ================================== 62 // ================================================== 63 // 63 // 64 // G4Mutex and G4RecursiveMutex are always C++ 64 // G4Mutex and G4RecursiveMutex are always C++11 std::mutex types 65 // however, in serial mode, using G4MUTEXLOCK 65 // however, in serial mode, using G4MUTEXLOCK and G4MUTEXUNLOCK on these 66 // types has no effect -- i.e. the mutexes are 66 // types has no effect -- i.e. the mutexes are not actually locked or unlocked 67 // 67 // 68 // Additionally, when a G4Mutex or G4Recursive 68 // Additionally, when a G4Mutex or G4RecursiveMutex is used with G4AutoLock 69 // and G4RecursiveAutoLock, respectively, thes 69 // and G4RecursiveAutoLock, respectively, these classes also suppressing 70 // the locking and unlocking of the mutex. Reg 70 // the locking and unlocking of the mutex. Regardless of the build type, 71 // G4AutoLock and G4RecursiveAutoLock inherit 71 // G4AutoLock and G4RecursiveAutoLock inherit from std::unique_lock<std::mutex> 72 // and std::unique_lock<std::recursive_mutex>, 72 // and std::unique_lock<std::recursive_mutex>, respectively. This means 73 // that in situations (such as is needed by th 73 // that in situations (such as is needed by the analysis category), the 74 // G4AutoLock and G4RecursiveAutoLock can be p 74 // G4AutoLock and G4RecursiveAutoLock can be passed to functions requesting 75 // a std::unique_lock. Within these functions, 75 // a std::unique_lock. Within these functions, since std::unique_lock 76 // member functions are not virtual, they will 76 // member functions are not virtual, they will not retain the dummy locking 77 // and unlocking behavior 77 // and unlocking behavior 78 // --> An example of this behavior can be foun 78 // --> An example of this behavior can be found in G4AutoLock.hh >> 79 // >> 80 // Jonathan R. Madsen (February 21, 2018) >> 81 // 79 82 80 // Global mutex types << 83 // global mutex types 81 using G4Mutex = std::mutex; << 84 using G4Mutex = std::mutex; 82 using G4RecursiveMutex = std::recursive_mutex; 85 using G4RecursiveMutex = std::recursive_mutex; 83 86 84 // Mutex macros << 87 // mutex macros 85 #define G4MUTEX_INITIALIZER << 88 #define G4MUTEX_INITIALIZER {} 86 {} << 89 #define G4MUTEXINIT(mutex) ;; 87 #define G4MUTEXINIT(mutex) << 90 #define G4MUTEXDESTROY(mutex) ;; 88 ; << 89 ; << 90 #define G4MUTEXDESTROY(mutex) << 91 ; << 92 ; << 93 91 94 // Static functions: get_id(), sleep_for(...), << 92 // static functions: get_id(), sleep_for(...), sleep_until(...), yield(), 95 namespace G4ThisThread << 93 namespace G4ThisThread { using namespace std::this_thread; } 96 { << 97 using namespace std::this_thread; << 98 } << 99 94 100 // Will be used in the future when migrating t << 95 // will be used in the future when migrating threading to task-based style 101 // and are currently used in unit tests 96 // and are currently used in unit tests 102 template <typename _Tp> << 97 template <typename _Tp> using G4Promise = std::promise<_Tp>; 103 using G4Promise = std::promise<_Tp>; << 98 template <typename _Tp> using G4Future = std::future<_Tp>; 104 template <typename _Tp> << 99 template <typename _Tp> using G4SharedFuture = std::shared_future<_Tp>; 105 using G4Future = std::future<_Tp>; << 106 template <typename _Tp> << 107 using G4SharedFuture = std::shared_future<_Tp> << 108 100 109 // Some useful types 101 // Some useful types 110 using G4ThreadFunReturnType = void*; 102 using G4ThreadFunReturnType = void*; 111 using G4ThreadFunArgType = void*; << 103 using G4ThreadFunArgType = void*; 112 using thread_lock = << 104 using thread_lock = G4int(*)(G4Mutex*); // typedef G4int (*thread_lock)(G4Mutex*); 113 G4int (*)(G4Mutex*); // typedef G4int (*thr << 105 using thread_unlock = G4int(*)(G4Mutex*); // typedef G4int (*thread_unlock)(G4Mutex*); 114 using thread_unlock = << 115 G4int (*)(G4Mutex*); // typedef G4int (*thr << 116 106 117 // Helper function for getting a unique static 107 // Helper function for getting a unique static mutex for a specific 118 // class or type 108 // class or type 119 // Usage example: 109 // Usage example: 120 // a template class "G4Cache<T>" that requir << 110 // a template class "G4Cache<T>" that required a static 121 // mutex for specific to type T: << 111 // mutex for specific to type T: 122 // G4AutoLock l(G4TypeMutex<G4Cache<T>>() << 112 // G4AutoLock l(G4TypeMutex<G4Cache<T>>()); 123 template <typename _Tp> 113 template <typename _Tp> 124 G4Mutex& G4TypeMutex() << 114 G4Mutex& G4TypeMutex(const unsigned int& _n = 0) 125 { 115 { 126 static G4Mutex _mutex; << 116 static G4Mutex* _mutex = new G4Mutex(); 127 return _mutex; << 117 if(_n == 0) >> 118 return *_mutex; >> 119 >> 120 static std::vector<G4Mutex*> _mutexes; >> 121 if(_n > _mutexes.size()) >> 122 _mutexes.resize(_n, nullptr); >> 123 if(!_mutexes[_n]) >> 124 _mutexes[_n] = new G4Mutex(); >> 125 return *(_mutexes[_n-1]); 128 } 126 } 129 127 130 // Helper function for getting a unique static << 128 // Helper function for getting a unique static recursive_mutex for a 131 // specific class or type 129 // specific class or type 132 // Usage example: 130 // Usage example: 133 // a template class "G4Cache<T> << 131 // a template class "G4Cache<T>" that required a static 134 // recursive_mutex for specific << 132 // recursive_mutex for specific to type T: 135 // G4RecursiveAutoLock << 133 // G4RecursiveAutoLock l(G4TypeRecursiveMutex<G4Cache<T>>()); 136 // l(G4TypeRecursiveMut << 137 template <typename _Tp> 134 template <typename _Tp> 138 G4RecursiveMutex& G4TypeRecursiveMutex() << 135 G4RecursiveMutex& G4TypeRecursiveMutex(const unsigned int& _n = 0) 139 { 136 { 140 static G4RecursiveMutex _mutex; << 137 static G4RecursiveMutex* _mutex = new G4RecursiveMutex(); 141 return _mutex; << 138 if(_n == 0) >> 139 return *(_mutex); >> 140 >> 141 static std::vector<G4RecursiveMutex*> _mutexes; >> 142 if(_n > _mutexes.size()) >> 143 _mutexes.resize(_n, nullptr); >> 144 if(!_mutexes[_n]) >> 145 _mutexes[_n] = new G4RecursiveMutex(); >> 146 return *(_mutexes[_n-1]); 142 } 147 } 143 148 144 #if defined(G4MULTITHREADED) 149 #if defined(G4MULTITHREADED) 145 //========================================== << 150 //========================================== 146 // G4MULTITHREADED is ON - threading enabled << 151 // G4MULTITHREADED is ON - threading enabled 147 //========================================== << 152 //========================================== 148 << 153 149 // global thread types << 154 // global thread types 150 using G4Thread = std::thread; << 155 using G4Thread = std::thread; 151 using G4NativeThread = std::thread::native_han << 156 using G4NativeThread = std::thread::native_handle_type; 152 << 157 153 // mutex macros << 158 // mutex macros 154 # define G4MUTEXLOCK(mutex) << 159 #define G4MUTEXLOCK(mutex) { (mutex)->lock(); } 155 { << 160 #define G4MUTEXUNLOCK(mutex) { (mutex)->unlock(); } 156 (mutex)->lock(); << 161 157 } << 162 // Macro to join thread 158 # define G4MUTEXUNLOCK(mutex) << 163 #define G4THREADJOIN(worker) (worker).join() 159 { << 164 160 (mutex)->unlock(); << 165 // std::thread::id does not cast to integer >> 166 using G4Pid_t = std::thread::id; >> 167 >> 168 // Instead of previous macro taking one argument, define function taking >> 169 // unlimited arguments >> 170 template <typename _Worker, typename _Func, typename... _Args> >> 171 void G4THREADCREATE(_Worker*& worker, _Func func, _Args... args) >> 172 { >> 173 *worker = G4Thread(func, std::forward<_Args>(args)...); 161 } 174 } 162 175 163 // Macro to join thread << 176 // Conditions 164 # define G4THREADJOIN(worker) (worker).join() << 177 // 165 << 178 // See G4MTRunManager for example on how to use these 166 // std::thread::id does not cast to integer << 179 // 167 using G4Pid_t = std::thread::id; << 180 using G4Condition = std::condition_variable; 168 << 181 #define G4CONDITION_INITIALIZER {} 169 // Instead of previous macro taking one argume << 182 #define G4CONDITIONWAIT(cond, lock) (cond)->wait(*lock); 170 // unlimited arguments << 183 #define G4CONDITIONWAITLAMBDA(cond, lock, lambda) (cond)->wait(*lock, lambda); 171 template <typename _Worker, typename _Func, ty << 184 #define G4CONDITIONNOTIFY(cond) (cond)->notify_one(); 172 void G4THREADCREATE(_Worker*& worker, _Func fu << 185 #define G4CONDITIONBROADCAST(cond) (cond)->notify_all(); 173 { << 186 // 174 *worker = G4Thread(func, std::forward<_Args> << 187 // we don't define above globally so single-threaded code does not get 175 } << 188 // caught in condition with no other thread to wake it up 176 << 189 // 177 // Conditions << 190 178 // << 191 #else 179 // See G4MTRunManager for example on how to us << 192 //========================================== 180 // << 193 // G4MULTITHREADED is OFF - Sequential build 181 using G4Condition = std::condition_variable; << 194 //========================================== 182 # define G4CONDITION_INITIALIZER << 195 183 {} << 196 // implement a dummy thread class that acts like a thread 184 # define G4CONDITIONWAIT(cond, lock) (cond)-> << 197 class G4DummyThread 185 # define G4CONDITIONWAITLAMBDA(cond, lock, la << 198 { 186 # define G4CONDITIONNOTIFY(cond) (cond)->noti << 199 public: 187 # define G4CONDITIONBROADCAST(cond) (cond)->n << 200 using native_handle_type = G4int; 188 // << 201 using id = std::thread::id; 189 // we don't define above globally so single-th << 202 190 // caught in condition with no other thread to << 203 public: 191 // << 204 // does nothing 192 << 205 G4DummyThread() 193 #else << 206 { } 194 //========================================== << 207 // a std::thread-like constructor that execute upon construction 195 // G4MULTITHREADED is OFF - Sequential build << 208 template <typename _Func, typename... _Args> 196 //========================================== << 209 G4DummyThread(_Func func, _Args&&... _args) 197 << 210 { 198 // implement a dummy thread class that acts li << 211 func(std::forward<_Args>(_args)...); 199 class G4DummyThread << 212 } 200 { << 213 201 public: << 214 public: 202 using native_handle_type = G4int; << 215 native_handle_type native_handle() const { return native_handle_type(); } 203 using id = std::thread::id; << 216 bool joinable() const { return true; } 204 << 217 id get_id() const noexcept { return std::this_thread::get_id(); } 205 public: << 218 void swap(G4DummyThread&) { } 206 // does nothing << 219 void join() { } 207 G4DummyThread() {} << 220 void detach() { } 208 // a std::thread-like constructor that execu << 221 209 template <typename _Func, typename... _Args> << 222 public: 210 G4DummyThread(_Func func, _Args&&... _args) << 223 static unsigned int hardware_concurrency() noexcept 211 { << 224 { 212 func(std::forward<_Args>(_args)...); << 225 return std::thread::hardware_concurrency(); 213 } << 226 } 214 << 227 }; 215 public: << 228 216 native_handle_type native_handle() const { r << 229 // global thread types 217 G4bool joinable() const { return true; } << 230 using G4Thread = G4DummyThread; 218 id get_id() const noexcept { return std::thi << 231 using G4NativeThread = G4DummyThread::native_handle_type; 219 void swap(G4DummyThread&) {} << 232 220 void join() {} << 233 // mutex macros 221 void detach() {} << 234 #define G4MUTEXLOCK(mutex) ;; 222 << 235 #define G4MUTEXUNLOCK(mutex) ;; 223 public: << 236 224 static unsigned int hardware_concurrency() n << 237 // Macro to join thread 225 { << 238 #define G4THREADJOIN(worker) ;; 226 return std::thread::hardware_concurrency() << 239 227 } << 240 using G4Pid_t = G4int; 228 }; << 241 229 << 242 // Instead of previous macro taking one argument, define function taking 230 // global thread types << 243 // unlimited arguments 231 using G4Thread = G4DummyThread; << 244 template <typename _Worker, typename _Func, typename... _Args> 232 using G4NativeThread = G4DummyThread::native_h << 245 void G4THREADCREATE(_Worker*& worker, _Func func, _Args... args) 233 << 246 { 234 // mutex macros << 247 *worker = G4Thread(func, std::forward<_Args>(args)...); 235 # define G4MUTEXLOCK(mutex) << 248 } 236 ; << 237 ; << 238 # define G4MUTEXUNLOCK(mutex) << 239 ; << 240 ; << 241 << 242 // Macro to join thread << 243 # define G4THREADJOIN(worker) << 244 ; << 245 ; << 246 << 247 using G4Pid_t = G4int; << 248 << 249 // Instead of previous macro taking one argume << 250 // unlimited arguments << 251 template <typename _Worker, typename _Func, ty << 252 void G4THREADCREATE(_Worker*& worker, _Func fu << 253 { << 254 *worker = G4Thread(func, std::forward<_Args> << 255 } << 256 249 257 using G4Condition = G4int; << 250 using G4Condition = G4int; 258 # define G4CONDITION_INITIALIZER 1 << 251 #define G4CONDITION_INITIALIZER 1 259 # define G4CONDITIONWAIT(cond, mutex) G4Consu << 252 #define G4CONDITIONWAIT(cond, mutex) G4ConsumeParameters(cond, mutex); 260 # define G4CONDITIONWAITLAMBDA(cond, mutex, l << 253 #define G4CONDITIONWAITLAMBDA(cond, mutex, lambda) G4ConsumeParameters(cond, mutex, lambda); 261 G4ConsumeParameters(cond, mutex, lambda); << 254 #define G4CONDITIONNOTIFY(cond) G4ConsumeParameters(cond); 262 # define G4CONDITIONNOTIFY(cond) G4ConsumePar << 255 #define G4CONDITIONBROADCAST(cond) G4ConsumeParameters(cond); 263 # define G4CONDITIONBROADCAST(cond) G4Consume << 264 256 265 #endif // G4MULTITHREADING << 257 #endif //G4MULTITHREADING 266 258 267 //============================================ 259 //============================================================================// 268 260 269 // Define here after G4Thread has been typedef 261 // Define here after G4Thread has been typedef 270 using G4ThreadId = G4Thread::id; 262 using G4ThreadId = G4Thread::id; 271 263 272 //============================================ 264 //============================================================================// 273 265 274 namespace G4Threading 266 namespace G4Threading 275 { 267 { 276 enum << 268 enum 277 { << 269 { 278 SEQUENTIAL_ID = -2, << 270 SEQUENTIAL_ID = -2, 279 MASTER_ID = -1, << 271 MASTER_ID = -1, 280 WORKER_ID = 0, << 272 WORKER_ID = 0, 281 GENERICTHREAD_ID = -1000 << 273 GENERICTHREAD_ID = -1000 282 }; << 274 }; 283 << 275 284 G4Pid_t G4GetPidId(); << 276 G4Pid_t G4GetPidId(); 285 G4int G4GetNumberOfCores(); << 277 G4int G4GetNumberOfCores(); 286 G4int G4GetThreadId(); << 278 G4int G4GetThreadId(); 287 G4bool IsWorkerThread(); << 279 G4bool IsWorkerThread(); 288 G4bool IsMasterThread(); << 280 G4bool IsMasterThread(); 289 void G4SetThreadId(G4int aNewValue); << 281 void G4SetThreadId( G4int aNewValue ); 290 G4bool G4SetPinAffinity(G4int idx, G4NativeT << 282 G4bool G4SetPinAffinity( G4int idx , G4NativeThread& at); 291 void SetMultithreadedApplication(G4bool valu << 283 void SetMultithreadedApplication(G4bool value); 292 G4bool IsMultithreadedApplication(); << 284 G4bool IsMultithreadedApplication(); 293 G4int WorkerThreadLeavesPool(); << 285 int WorkerThreadLeavesPool(); 294 G4int WorkerThreadJoinsPool(); << 286 int WorkerThreadJoinsPool(); 295 G4int GetNumberOfRunningWorkerThreads(); << 287 G4int GetNumberOfRunningWorkerThreads(); 296 } // namespace G4Threading << 288 } 297 289 298 #endif // G4Threading_hh << 290 #endif //G4Threading_hh 299 291