Geant4  10.01.p02
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 // Macro to put current thread to sleep
44 //
45 #if defined(WIN32)
46 #define G4THREADSLEEP( tick ) { Sleep(tick); }
47 #else
48 #include <unistd.h> // needed for sleep()
49 #define G4THREADSLEEP( tick ) { sleep(tick); }
50 #endif
51 
52 #if defined(G4MULTITHREADED)
53  //===============================
54  // Multi-threaded build
55  //===============================
56 #if ( defined(__MACH__) && defined(__clang__) && defined(__x86_64__) ) || \
57  ( defined(__MACH__) && defined(__GNUC__) && __GNUC__>=4 && __GNUC_MINOR__>=7 ) || \
58  defined(__linux__) || defined(_AIX)
59  //
60  // Multi-threaded build: for POSIX systems
61  //
62  #include <pthread.h>
63  #if defined(__MACH__) // needed only for MacOSX for definition of pid_t
64  #include <sys/types.h>
65  #endif
66 
67  typedef pthread_mutex_t G4Mutex;
68  typedef pthread_t G4Thread;
69 
70  // G4Mutex initializer macro
71  //
72  #define G4MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
73 
74  // Lock/unlock a G4Mutex function name
75  //
76  #define G4MUTEXLOCK pthread_mutex_lock
77  #define G4MUTEXUNLOCK pthread_mutex_unlock
78 
79  // Macro to initialize a Mutex
80  //
81  #define G4MUTEXINIT(mutex) pthread_mutex_init( &mutex , NULL);
82  #define G4MUTEXDESTROY(mutex) pthread_mutex_destroy( &mutex );
83 
84  // Macro to create a G4Thread object
85  //
86  #define G4THREADCREATE( worker , func , arg ) { \
87  pthread_attr_t attr; \
88  pthread_attr_init(&attr); \
89  pthread_attr_setstacksize(&attr,16*1024*1024); \
90  pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE); \
91  pthread_create( worker, &attr, func , arg ); \
92  }
93 
94  // Macro to join thread
95  //
96  #define G4THREADJOIN( worker ) pthread_join( worker , NULL)
97 
98  // Macro to retrieve caller thread
99  //
100  #define G4THREADSELF pthread_self
101 
102  // Some useful types
103  //
104  typedef void* G4ThreadFunReturnType;
105  typedef void* G4ThreadFunArgType;
106  typedef G4int (*thread_lock)(G4Mutex*);
107  typedef G4int (*thread_unlock)(G4Mutex*);
108 
109  typedef pid_t G4Pid_t;
110 
111  // Conditions
112  //
113  // See G4MTRunManager for example on how to use these
114  // This complication is needed to be portable with WIN32
115  // Note that WIN32 requires an additional initialization step.
116  // See example code
117  //
118  typedef pthread_cond_t G4Condition;
119  #define G4CONDITION_INITIALIZER PTHREAD_COND_INITIALIZER
120 
121  #define G4CONDITIONWAIT( cond, mutex ) pthread_cond_wait( cond , mutex );
122  #define G4CONDTIONBROADCAST( cond ) pthread_cond_broadcast( cond );
123 
124  #elif defined(WIN32)
125  //
126  // Multi-threaded build: for Windows systems
127  //
128  #include "windefs.hh" // Include 'safe...' <windows.h>
129 
130  typedef HANDLE G4Mutex;
131  typedef HANDLE G4Thread;
132 
133  #define G4MUTEX_INITIALIZER CreateMutex(NULL,FALSE,NULL)
134  DWORD /*WINAPI*/ G4WaitForSingleObjectInf( __in G4Mutex m );
135  #define G4MUTEXLOCK G4WaitForSingleObjectInf
136 
137  // #define G4MUTEXINIT(mutex) InitializeCriticalSection( &mutex );
138  #define G4MUTEXINIT(mutex);
139  #define G4MUTEXDESTROY(mutex);
140 
141  // Not clear why following two lines are needed...
142  //
143  BOOL G4ReleaseMutex( __in G4Mutex m);
144  #define G4MUTEXUNLOCK G4ReleaseMutex
145 
146  #define G4THREADCREATE( worker, func, arg ) { *worker = CreateThread( NULL, 16*1024*1024 , func , arg , 0 , NULL ); }
147  #define G4THREADJOIN( worker ) WaitForSingleObject( worker , INFINITE);
148  #define G4THREADSELF GetCurrentThreadId
149  #define G4ThreadFunReturnType DWORD WINAPI
150  typedef LPVOID G4ThreadFunArgType;
151  typedef DWORD (*thread_lock)(G4Mutex);
152  typedef BOOL (*thread_unlock)(G4Mutex);
153  typedef DWORD G4Pid_t;
154 
155  // Conditions
156  //
157  typedef CONDITION_VARIABLE G4Condition;
158  #define G4CONDITION_INITIALIZER CONDITION_VARIABLE_INIT
159 
160  #define G4CONDITIONWAIT( cond , criticalsectionmutex ) SleepConditionVariableCS( cond, criticalsectionmutex , INFINITE );
161  #define G4CONDTIONBROADCAST( cond ) WakeAllConditionVariable( cond );
162 
163  #else
164 
165  #error "No Threading model technology supported for this platform. Use sequential build !"
166 
167  #endif
168 
169 #else
170  //==========================================
171  // G4MULTITHREADED is OFF - Sequential build
172  //==========================================
173  typedef G4int G4Mutex;
174  typedef G4int G4Thread;
175  #define G4MUTEX_INITIALIZER 1
176  G4int fake_mutex_lock_unlock( G4Mutex* );// { return 0; }
177  #define G4MUTEXINIT(mutex) ;;
178  #define G4MUTEXDESTROY(mutex) ;;
179  #define G4MUTEXLOCK fake_mutex_lock_unlock
180  #define G4MUTEXUNLOCK fake_mutex_lock_unlock
181  #define G4THREADCREATE( worker , func , arg ) ;;
182  #define G4THREADJOIN( worker ) ;;
183  #define G4THREADSELF( nothing ) G4Thread(nothing);
184  typedef void* G4ThreadFunReturnType;
185  typedef void* G4ThreadFunArgType;
186  typedef G4int (*thread_lock)(G4Mutex*);
188  typedef G4int G4Pid_t;
190  #define G4CONDITION_INITIALIZER 1
191  #define G4CONDITIONWAIT( cond, mutex ) ;;
192  #define G4CONDTIONBROADCAST( cond ) ;;
193 
194 #endif //G4MULTITHREADING
195 
196 namespace G4Threading
197 {
198  enum {
200  MASTER_ID = -1,
203  };
208  void G4SetThreadId( G4int aNewValue );
212 }
213 
214 #endif //G4Threading_hh
G4bool G4SetPinAffinity(G4int idx, G4Thread &at)
Definition: G4Threading.cc:131
G4int(* thread_lock)(G4Mutex *)
Definition: G4Threading.hh:186
void SetMultithreadedApplication(G4bool value)
Definition: G4Threading.cc:133
void G4SetThreadId(G4int aNewValue)
Definition: G4Threading.cc:129
G4int G4Pid_t
Definition: G4Threading.hh:188
int G4int
Definition: G4Types.hh:78
G4int G4Thread
Definition: G4Threading.hh:174
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:187
G4bool IsWorkerThread()
Definition: G4Threading.cc:128
G4int G4Mutex
Definition: G4Threading.hh:173
static const double m
Definition: G4SIunits.hh:110
G4int fake_mutex_lock_unlock(G4Mutex *)
Definition: G4Threading.cc:115
void * G4ThreadFunArgType
Definition: G4Threading.hh:185
G4int G4Condition
Definition: G4Threading.hh:189
G4int G4GetThreadId()
Definition: G4Threading.cc:127
void * G4ThreadFunReturnType
Definition: G4Threading.hh:184