Geant4  10.01.p02
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 
84 #if defined(WIN32) // WIN32 stuff needed for MT
85 DWORD /*WINAPI*/ G4WaitForSingleObjectInf( __in G4Mutex m )
86  { return WaitForSingleObject( m , INFINITE); }
87 BOOL G4ReleaseMutex( __in G4Mutex m)
88  { return ReleaseMutex(m); }
89 #endif
90 
91 #if defined(__linux__) || defined(_AIX)
93 {
94  cpu_set_t* aset = new cpu_set_t;
96  CPU_ZERO(aset);
97  CPU_SET(cpu,aset);
98  return ( pthread_setaffinity_np(aT, sizeof(cpu_set_t), aset) == 0 );
99 }
100 #else //Not available for Mac, WIN,...
102 {
103  G4Exception("G4Threading::G4SetPinAffinity()",
104  "NotImplemented", JustWarning,
105  "Affinity setting not available for this architecture, ignoring...");
106  return true;
107 }
108 #endif
109 
110 void G4Threading::SetMultithreadedApplication(G4bool value ) { isMTAppType = value; }
111 G4bool G4Threading::IsMultithreadedApplication() { return isMTAppType; }
112 
113 #else // Sequential mode
114 
116 
118 { // In sequential mode return Process ID and not Thread ID
119  #if defined(WIN32)
120  return GetCurrentProcessId();
121  #else
122  return getpid();
123  #endif
124 }
125 
130 
132 
135 #endif
G4bool G4SetPinAffinity(G4int idx, G4Thread &at)
Definition: G4Threading.cc:131
void SetMultithreadedApplication(G4bool value)
Definition: G4Threading.cc:133
void G4SetThreadId(G4int aNewValue)
Definition: G4Threading.cc:129
#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:117
void Register(T *inst)
Definition: G4AutoDelete.hh:65
bool G4bool
Definition: G4Types.hh:79
G4int G4GetNumberOfCores()
Definition: G4Threading.cc:126
G4bool IsMultithreadedApplication()
Definition: G4Threading.cc:134
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4bool IsWorkerThread()
Definition: G4Threading.cc:128
G4int G4Mutex
Definition: G4Threading.hh:173
static const double m
Definition: G4SIunits.hh:110
G4int G4GetThreadId()
Definition: G4Threading.cc:127
G4int fake_mutex_lock_unlock(G4Mutex *)
Definition: G4Threading.cc:115