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