Geant4  10.01
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 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 ); \
82  }
83 
84  // Macro to join thread
85  #define G4THREADJOIN( worker ) pthread_join( worker , NULL)
86 
87  // Macro to retrieve caller thread
88  #define G4THREADSELF pthread_self
89 
90  // Some useful types
91  //
92  typedef void* G4ThreadFunReturnType;
93  typedef void* G4ThreadFunArgType;
94  typedef G4int (*thread_lock)(G4Mutex*);
95  typedef G4int (*thread_unlock)(G4Mutex*);
96 
97  typedef pid_t G4Pid_t;
98 
99  // Conditions
100  //
101  // See G4MTRunManager for example on how to use these
102  // This complication is needed to be portable with WIN32
103  // Note that WIN32 requires an additional initialization step.
104  // See example code
105  //
106  typedef pthread_cond_t G4Condition;
107  #define G4CONDITION_INITIALIZER PTHREAD_COND_INITIALIZER
108 
109  #define G4CONDITIONWAIT( cond, mutex ) pthread_cond_wait( cond , mutex );
110  #define G4CONDTIONBROADCAST( cond ) pthread_cond_broadcast( cond );
111 
112  #elif defined(WIN32)
113  //
114  // Multi-threaded build: for Windows systems
115  //
116  #include "windefs.hh" // Include 'safe...' <windows.h>
117 
118  typedef HANDLE G4Mutex;
119  typedef HANDLE G4Thread;
120 
121  #define G4MUTEX_INITIALIZER CreateMutex(NULL,FALSE,NULL)
122  DWORD /*WINAPI*/ G4WaitForSingleObjectInf( __in G4Mutex m );
123  #define G4MUTEXLOCK G4WaitForSingleObjectInf
124 
125  // #define G4MUTEXINIT(mutex) InitializeCriticalSection( &mutex );
126  #define G4MUTEXINIT(mutex);
127  #define G4MUTEXDESTROY(mutex);
128 
129  // Not clear why following two lines are needed...
130  //
131  BOOL G4ReleaseMutex( __in G4Mutex m);
132  #define G4MUTEXUNLOCK G4ReleaseMutex
133 
134  #define G4THREADCREATE( worker, func, arg ) { *worker = CreateThread( NULL, 16*1024*1024 , func , arg , 0 , NULL ); }
135  #define G4THREADJOIN( worker ) WaitForSingleObject( worker , INFINITE);
136  #define G4THREADSELF GetCurrentThreadId
137  #define G4ThreadFunReturnType DWORD WINAPI
138  typedef LPVOID G4ThreadFunArgType;
139  typedef DWORD (*thread_lock)(G4Mutex);
140  typedef BOOL (*thread_unlock)(G4Mutex);
141  typedef DWORD G4Pid_t;
142 
143  // Conditions
144  //
145  typedef CONDITION_VARIABLE G4Condition;
146  #define G4CONDITION_INITIALIZER CONDITION_VARIABLE_INIT
147 
148  #define G4CONDITIONWAIT( cond , criticalsectionmutex ) SleepConditionVariableCS( cond, criticalsectionmutex , INFINITE );
149  #define G4CONDTIONBROADCAST( cond ) WakeAllConditionVariable( cond );
150 
151  #else
152 
153  #error "No Threading model technology supported for this platform. Use sequential build !"
154 
155  #endif
156 
157 #else
158  //==========================================
159  // G4MULTITHREADED is OFF - Sequential build
160  //==========================================
161  typedef G4int G4Mutex;
162  typedef G4int G4Thread;
163  #define G4MUTEX_INITIALIZER 1
164  G4int fake_mutex_lock_unlock( G4Mutex* );// { return 0; }
165  #define G4MUTEXINIT(mutex) ;;
166  #define G4MUTEXDESTROY(mutex) ;;
167  #define G4MUTEXLOCK fake_mutex_lock_unlock
168  #define G4MUTEXUNLOCK fake_mutex_lock_unlock
169  #define G4THREADCREATE( worker , func , arg ) ;;
170  #define G4THREADJOIN( worker ) ;;
171  #define G4THREADSELF( nothing ) G4Thread(nothing);
172  typedef void* G4ThreadFunReturnType;
173  typedef void* G4ThreadFunArgType;
174  typedef G4int (*thread_lock)(G4Mutex*);
176  typedef G4int G4Pid_t;
178  #define G4CONDITION_INITIALIZER 1
179  #define G4CONDITIONWAIT( cond, mutex ) ;;
180  #define G4CONDTIONBROADCAST( cond ) ;;
181 
182 #endif //G4MULTITHREADING
183 
184 namespace G4Threading
185 {
186  enum {
188  MASTER_ID = -1,
191  };
196  void G4SetThreadId( G4int aNewValue );
200 }
201 
202 #endif //G4Threading_hh
G4bool G4SetPinAffinity(G4int idx, G4Thread &at)
Definition: G4Threading.cc:131
G4int(* thread_lock)(G4Mutex *)
Definition: G4Threading.hh:174
void SetMultithreadedApplication(G4bool value)
Definition: G4Threading.cc:133
void G4SetThreadId(G4int aNewValue)
Definition: G4Threading.cc:129
G4int G4Pid_t
Definition: G4Threading.hh:176
int G4int
Definition: G4Types.hh:78
G4int G4Thread
Definition: G4Threading.hh:162
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:175
G4bool IsWorkerThread()
Definition: G4Threading.cc:128
G4int G4Mutex
Definition: G4Threading.hh:161
static const double m
Definition: G4SIunits.hh:110
G4int fake_mutex_lock_unlock(G4Mutex *)
Definition: G4Threading.cc:115
void * G4ThreadFunArgType
Definition: G4Threading.hh:173
G4int G4Condition
Definition: G4Threading.hh:177
G4int G4GetThreadId()
Definition: G4Threading.cc:127
void * G4ThreadFunReturnType
Definition: G4Threading.hh:172