Geant4  10.02.p03
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 namespace
53 {
55  G4bool isMTAppType = false;
56 }
57 
59 { // In multithreaded mode return Thread ID
60  #if defined(__MACH__)
61  #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12
62  uint64_t tid64;
63  pthread_threadid_np(NULL, &tid64);
64  return (pid_t)tid64;
65  #else
66  return syscall(SYS_thread_selfid);
67  #endif
68  #elif defined(WIN32)
69  return GetCurrentThreadId();
70  #else
71  return syscall(SYS_gettid);
72  #endif
73 }
74 
76 {
77  #if defined(WIN32)
78  SYSTEM_INFO sysinfo;
79  GetSystemInfo( &sysinfo );
80  return static_cast<G4int>( sysinfo.dwNumberOfProcessors );
81  #else
82  return static_cast<G4int>(sysconf( _SC_NPROCESSORS_ONLN ));
83  #endif
84 }
85 
86 void G4Threading::G4SetThreadId(G4int value ) { G4ThreadID = value; }
87 G4int G4Threading::G4GetThreadId() { return G4ThreadID; }
88 G4bool G4Threading::IsWorkerThread() { return (G4ThreadID>=0); }
89 G4bool G4Threading::IsMasterThread() { return (G4ThreadID==MASTER_ID); }
90 
91 #if defined(WIN32) // WIN32 stuff needed for MT
92 DWORD /*WINAPI*/ G4WaitForSingleObjectInf( __in G4Mutex m )
93  { return WaitForSingleObject( m , INFINITE); }
94 BOOL G4ReleaseMutex( __in G4Mutex m)
95  { return ReleaseMutex(m); }
96 #endif
97 
98 #if defined(__linux__) || defined(_AIX)
100 {
101  cpu_set_t* aset = new cpu_set_t;
103  CPU_ZERO(aset);
104  CPU_SET(cpu,aset);
105  return ( pthread_setaffinity_np(aT, sizeof(cpu_set_t), aset) == 0 );
106 }
107 #else //Not available for Mac, WIN,...
109 {
110  G4Exception("G4Threading::G4SetPinAffinity()",
111  "NotImplemented", JustWarning,
112  "Affinity setting not available for this architecture, ignoring...");
113  return true;
114 }
115 #endif
116 
117 void G4Threading::SetMultithreadedApplication(G4bool value ) { isMTAppType = value; }
118 G4bool G4Threading::IsMultithreadedApplication() { return isMTAppType; }
119 
120 #else // Sequential mode
121 
123 
125 { // In sequential mode return Process ID and not Thread ID
126  #if defined(WIN32)
127  return GetCurrentProcessId();
128  #else
129  return getpid();
130  #endif
131 }
132 
138 
140 
143 #endif
G4bool G4SetPinAffinity(G4int idx, G4Thread &at)
Definition: G4Threading.cc:139
void SetMultithreadedApplication(G4bool value)
Definition: G4Threading.cc:141
void G4SetThreadId(G4int aNewValue)
Definition: G4Threading.cc:137
#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:124
void Register(T *inst)
Definition: G4AutoDelete.hh:65
bool G4bool
Definition: G4Types.hh:79
G4int G4GetNumberOfCores()
Definition: G4Threading.cc:133
G4bool IsMultithreadedApplication()
Definition: G4Threading.cc:142
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4bool IsWorkerThread()
Definition: G4Threading.cc:135
G4int G4Mutex
Definition: G4Threading.hh:173
static const double m
Definition: G4SIunits.hh:128
G4bool IsMasterThread()
Definition: G4Threading.cc:136
G4int G4GetThreadId()
Definition: G4Threading.cc:134
G4int fake_mutex_lock_unlock(G4Mutex *)
Definition: G4Threading.cc:122