Geant4  10.03
G4Threading.cc
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 implementation file
30 //
31 // G4Threading.cc
32 //
33 // ---------------------------------------------------------------
34 // Author: Andrea Dotti (15 Feb 2013): First Implementation
35 // ---------------------------------------------------------------
36 
37 #include "G4Threading.hh"
38 #include "G4AutoDelete.hh"
39 #include "G4AutoLock.hh"
40 #include "globals.hh"
41 
42 #if defined (WIN32)
43  #include <Windows.h>
44 #else
45  #include <unistd.h>
46  #include <sys/types.h>
47  #include <sys/syscall.h>
48 #endif
49 
50 #if defined(G4MULTITHREADED)
51 
52 #include <atomic>
53 
54 namespace
55 {
57  G4bool isMTAppType = false;
58 }
59 
61 { // In multithreaded mode return Thread ID
62  #if defined(__MACH__)
63  #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12
64  uint64_t tid64;
65  pthread_threadid_np(NULL, &tid64);
66  return (pid_t)tid64;
67  #else
68  return syscall(SYS_thread_selfid);
69  #endif
70  #elif defined(WIN32)
71  return GetCurrentThreadId();
72  #else
73  return syscall(SYS_gettid);
74  #endif
75 }
76 
78 {
79  #if defined(WIN32)
80  SYSTEM_INFO sysinfo;
81  GetSystemInfo( &sysinfo );
82  return static_cast<G4int>( sysinfo.dwNumberOfProcessors );
83  #else
84  return static_cast<G4int>(sysconf( _SC_NPROCESSORS_ONLN ));
85  #endif
86 }
87 
88 void G4Threading::G4SetThreadId(G4int value ) { G4ThreadID = value; }
89 G4int G4Threading::G4GetThreadId() { return G4ThreadID; }
90 G4bool G4Threading::IsWorkerThread() { return (G4ThreadID>=0); }
91 G4bool G4Threading::IsMasterThread() { return (G4ThreadID==MASTER_ID); }
92 
93 #if defined(WIN32) // WIN32 stuff needed for MT
94 DWORD /*WINAPI*/ G4WaitForSingleObjectInf( __in G4Mutex m )
95  { return WaitForSingleObject( m , INFINITE); }
96 BOOL G4ReleaseMutex( __in G4Mutex m)
97  { return ReleaseMutex(m); }
98 #endif
99 
100 #if defined(__linux__) || defined(_AIX)
102 {
103  cpu_set_t* aset = new cpu_set_t;
105  CPU_ZERO(aset);
106  CPU_SET(cpu,aset);
107  return ( pthread_setaffinity_np(aT, sizeof(cpu_set_t), aset) == 0 );
108 }
109 #else //Not available for Mac, WIN,...
111 {
112  G4Exception("G4Threading::G4SetPinAffinity()",
113  "NotImplemented", JustWarning,
114  "Affinity setting not available for this architecture, ignoring...");
115  return true;
116 }
117 #endif
118 
119 void G4Threading::SetMultithreadedApplication(G4bool value ) { isMTAppType = value; }
120 G4bool G4Threading::IsMultithreadedApplication() { return isMTAppType; }
121 
122 namespace
123 {
124  std::atomic_int numActThreads(0);
125 }
126 int G4Threading::WorkerThreadLeavesPool() { return numActThreads--; }
127 int G4Threading::WorkerThreadJoinsPool() { return numActThreads++;}
128 G4int G4Threading::GetNumberOfRunningWorkerThreads() { return numActThreads.load(); }
129 
130 #else // Sequential mode
131 
133 
135 { // In sequential mode return Process ID and not Thread ID
136  #if defined(WIN32)
137  return GetCurrentProcessId();
138  #else
139  return getpid();
140  #endif
141 }
142 
148 
150 
156 
157 #endif
G4bool G4SetPinAffinity(G4int idx, G4Thread &at)
Definition: G4Threading.cc:149
void SetMultithreadedApplication(G4bool value)
Definition: G4Threading.cc:151
G4int GetNumberOfRunningWorkerThreads()
Definition: G4Threading.cc:155
int WorkerThreadLeavesPool()
Definition: G4Threading.cc:153
void G4SetThreadId(G4int aNewValue)
Definition: G4Threading.cc:147
#define G4ThreadLocal
Definition: tls.hh:89
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:134
void Register(T *inst)
Definition: G4AutoDelete.hh:65
static constexpr double m
Definition: G4SIunits.hh:129
bool G4bool
Definition: G4Types.hh:79
G4int G4GetNumberOfCores()
Definition: G4Threading.cc:143
int WorkerThreadJoinsPool()
Definition: G4Threading.cc:154
G4bool IsMultithreadedApplication()
Definition: G4Threading.cc:152
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4bool IsWorkerThread()
Definition: G4Threading.cc:145
G4int G4Mutex
Definition: G4Threading.hh:173
G4bool IsMasterThread()
Definition: G4Threading.cc:146
G4int G4GetThreadId()
Definition: G4Threading.cc:144
G4int fake_mutex_lock_unlock(G4Mutex *)
Definition: G4Threading.cc:132