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