Geant4  10.01.p01
G4Threading.hh
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 // $Id$
27 //
28 // ---------------------------------------------------------------
29 // GEANT 4 class header file
30 //
31 // Class Description:
32 //
33 // This file defines types and macros used to expose Geant4 threading model.
34 
35 // ---------------------------------------------------------------
36 // Author: Andrea Dotti (15 Feb 2013): First Implementation
37 // ---------------------------------------------------------------
38 #ifndef G4Threading_hh
39 #define G4Threading_hh
40 
41 #include "G4Types.hh"
42 
43 #if defined(G4MULTITHREADED)
44  //===============================
45  // Multi-threaded build
46  //===============================
47 #if ( defined(__MACH__) && defined(__clang__) && defined(__x86_64__) ) || \
48  ( defined(__MACH__) && defined(__GNUC__) && __GNUC__>=4 && __GNUC_MINOR__>=7 ) || \
49  defined(__linux__) || defined(_AIX)
50  //
51  // Multi-threaded build: for POSIX systems
52  //
53  #include <pthread.h>
54  #if defined(__MACH__) // needed only for MacOSX for definition of pid_t
55  #include <sys/types.h>
56  #endif
57 
58  typedef pthread_mutex_t G4Mutex;
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 initialize a Mutex
71  //
72  #define G4MUTEXINIT(mutex) pthread_mutex_init( &mutex , NULL);
73  #define G4MUTEXDESTROY(mutex) pthread_mutex_destroy( &mutex );
74 
75  // Macro to put current thread to sleep
76  //
77  #define G4THREADSLEEP( tick ) { sleep(tick); }
78 
79  // Macro to create a G4Thread object
80  //
81  #define G4THREADCREATE( worker , func , arg ) { \
82  pthread_attr_t attr; \
83  pthread_attr_init(&attr); \
84  pthread_attr_setstacksize(&attr,16*1024*1024); \
85  pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE); \
86  pthread_create( worker, &attr, func , arg ); \
87  }
88 
89  // Macro to join thread
90  //
91  #define G4THREADJOIN( worker ) pthread_join( worker , NULL)
92 
93  // Macro to retrieve caller thread
94  //
95  #define G4THREADSELF pthread_self
96 
97  // Some useful types
98  //
99  typedef void* G4ThreadFunReturnType;
100  typedef void* G4ThreadFunArgType;
101  typedef G4int (*thread_lock)(G4Mutex*);
102  typedef G4int (*thread_unlock)(G4Mutex*);
103 
104  typedef pid_t G4Pid_t;
105 
106  // Conditions
107  //
108  // See G4MTRunManager for example on how to use these
109  // This complication is needed to be portable with WIN32
110  // Note that WIN32 requires an additional initialization step.
111  // See example code
112  //
113  typedef pthread_cond_t G4Condition;
114  #define G4CONDITION_INITIALIZER PTHREAD_COND_INITIALIZER
115 
116  #define G4CONDITIONWAIT( cond, mutex ) pthread_cond_wait( cond , mutex );
117  #define G4CONDTIONBROADCAST( cond ) pthread_cond_broadcast( cond );
118 
119  #elif defined(WIN32)
120  //
121  // Multi-threaded build: for Windows systems
122  //
123  #include "windefs.hh" // Include 'safe...' <windows.h>
124 
125  typedef HANDLE G4Mutex;
126  typedef HANDLE G4Thread;
127 
128  #define G4MUTEX_INITIALIZER CreateMutex(NULL,FALSE,NULL)
129  DWORD /*WINAPI*/ G4WaitForSingleObjectInf( __in G4Mutex m );
130  #define G4MUTEXLOCK G4WaitForSingleObjectInf
131 
132  // #define G4MUTEXINIT(mutex) InitializeCriticalSection( &mutex );
133  #define G4MUTEXINIT(mutex);
134  #define G4MUTEXDESTROY(mutex);
135 
136  // Not clear why following two lines are needed...
137  //
138  BOOL G4ReleaseMutex( __in G4Mutex m);
139  #define G4MUTEXUNLOCK G4ReleaseMutex
140 
141  #define G4THREADSLEEP( tick ) { Sleep(tick); }
142  #define G4THREADCREATE( worker, func, arg ) { *worker = CreateThread( NULL, 16*1024*1024 , func , arg , 0 , NULL ); }
143  #define G4THREADJOIN( worker ) WaitForSingleObject( worker , INFINITE);
144  #define G4THREADSELF GetCurrentThreadId
145  #define G4ThreadFunReturnType DWORD WINAPI
146  typedef LPVOID G4ThreadFunArgType;
147  typedef DWORD (*thread_lock)(G4Mutex);
148  typedef BOOL (*thread_unlock)(G4Mutex);
149  typedef DWORD G4Pid_t;
150 
151  // Conditions
152  //
153  typedef CONDITION_VARIABLE G4Condition;
154  #define G4CONDITION_INITIALIZER CONDITION_VARIABLE_INIT
155 
156  #define G4CONDITIONWAIT( cond , criticalsectionmutex ) SleepConditionVariableCS( cond, criticalsectionmutex , INFINITE );
157  #define G4CONDTIONBROADCAST( cond ) WakeAllConditionVariable( cond );
158 
159  #else
160 
161  #error "No Threading model technology supported for this platform. Use sequential build !"
162 
163  #endif
164 
165 #else
166  //==========================================
167  // G4MULTITHREADED is OFF - Sequential build
168  //==========================================
169  typedef G4int G4Mutex;
170  typedef G4int G4Thread;
171  #define G4MUTEX_INITIALIZER 1
172  G4int fake_mutex_lock_unlock( G4Mutex* );// { return 0; }
173  #define G4MUTEXINIT(mutex) ;;
174  #define G4MUTEXDESTROY(mutex) ;;
175  #define G4MUTEXLOCK fake_mutex_lock_unlock
176  #define G4MUTEXUNLOCK fake_mutex_lock_unlock
177  #define G4THREADSLEEP ( tick ) ;;
178  #define G4THREADCREATE( worker , func , arg ) ;;
179  #define G4THREADJOIN( worker ) ;;
180  #define G4THREADSELF( nothing ) G4Thread(nothing);
181  typedef void* G4ThreadFunReturnType;
182  typedef void* G4ThreadFunArgType;
183  typedef G4int (*thread_lock)(G4Mutex*);
185  typedef G4int G4Pid_t;
187  #define G4CONDITION_INITIALIZER 1
188  #define G4CONDITIONWAIT( cond, mutex ) ;;
189  #define G4CONDTIONBROADCAST( cond ) ;;
190 
191 #endif //G4MULTITHREADING
192 
193 namespace G4Threading
194 {
195  enum {
197  MASTER_ID = -1,
200  };
205  void G4SetThreadId( G4int aNewValue );
209 }
210 
211 #endif //G4Threading_hh
G4bool G4SetPinAffinity(G4int idx, G4Thread &at)
Definition: G4Threading.cc:131
G4int(* thread_lock)(G4Mutex *)
Definition: G4Threading.hh:183
void SetMultithreadedApplication(G4bool value)
Definition: G4Threading.cc:133
void G4SetThreadId(G4int aNewValue)
Definition: G4Threading.cc:129
G4int G4Pid_t
Definition: G4Threading.hh:185
int G4int
Definition: G4Types.hh:78
G4int G4Thread
Definition: G4Threading.hh:170
G4Pid_t G4GetPidId()
Definition: G4Threading.cc:117
bool G4bool
Definition: G4Types.hh:79
G4int G4GetNumberOfCores()
Definition: G4Threading.cc:126
G4bool IsMultithreadedApplication()
Definition: G4Threading.cc:134
G4int(* thread_unlock)(G4Mutex *)
Definition: G4Threading.hh:184
G4bool IsWorkerThread()
Definition: G4Threading.cc:128
G4int G4Mutex
Definition: G4Threading.hh:169
static const double m
Definition: G4SIunits.hh:110
G4int fake_mutex_lock_unlock(G4Mutex *)
Definition: G4Threading.cc:115
void * G4ThreadFunArgType
Definition: G4Threading.hh:182
G4int G4Condition
Definition: G4Threading.hh:186
G4int G4GetThreadId()
Definition: G4Threading.cc:127
void * G4ThreadFunReturnType
Definition: G4Threading.hh:181