Geant4_10
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 #if defined (WIN32)
39  #include <Windows.h>
40 #else
41  #include <unistd.h>
42  #include <sys/types.h>
43  #include <sys/syscall.h>
44 #endif
45 
46 #if defined(G4MULTITHREADED)
47 
48 #include <map>
49 
50 namespace {
51  G4ThreadLocal G4int G4ThreadID = -1;
52 }
53 
55 { // In multithreaded mode return Thread ID
56  #if defined(__MACH__)
57  return syscall(SYS_thread_selfid);
58  #elif defined(WIN32)
59  return GetCurrentThreadId();
60  #else
61  return syscall(SYS_gettid);
62  #endif
63 }
64 
66 {
67  #if defined(WIN32)
68  SYSTEM_INFO sysinfo;
69  GetSystemInfo( &sysinfo );
70  return static_cast<G4int>( sysinfo.dwNumberOfProcessors );
71  #else
72  return static_cast<G4int>(sysconf( _SC_NPROCESSORS_ONLN ));
73  #endif
74 }
75 
76 void G4Threading::G4SetThreadId(G4int value ) { G4ThreadID = value; }
77 G4int G4Threading::G4GetThreadId() { return G4ThreadID; }
78 G4bool G4Threading::IsWorkerThread() { return (G4ThreadID>=0); }
79 
80 #if defined(WIN32) // WIN32 stuff needed for MT
81 DWORD /*WINAPI*/ G4WaitForSingleObjectInf( __in G4Mutex m )
82  { return WaitForSingleObject( m , INFINITE); }
83 BOOL G4ReleaseMutex( __in G4Mutex m)
84  { return ReleaseMutex(m); }
85 #endif
86 
87 #else // Sequential mode
88 
89 #include "globals.hh"
90 
92 
93 G4Pid_t G4Threading::G4GetPidId() // In sequential mode return Process ID and not Thread ID
94 {
95  #if defined(WIN32)
96  return GetCurrentProcessId();
97  #else
98  return getpid();
99  #endif
100 }
101 
106 
107 #endif
108 
109 
void G4SetThreadId(G4int aNewValue)
Definition: G4Threading.cc:105
#define G4ThreadLocal
Definition: tls.hh:52
G4int G4Pid_t
Definition: G4Threading.hh:169
int G4int
Definition: G4Types.hh:78
G4Pid_t G4GetPidId()
Definition: G4Threading.cc:93
bool G4bool
Definition: G4Types.hh:79
G4int G4GetNumberOfCores()
Definition: G4Threading.cc:102
G4bool IsWorkerThread()
Definition: G4Threading.cc:104
G4int G4Mutex
Definition: G4Threading.hh:156
const XML_Char int const XML_Char * value
Definition: expat.h:331
G4int fake_mutex_lock_unlock(G4Mutex *)
Definition: G4Threading.cc:91
G4int G4GetThreadId()
Definition: G4Threading.cc:103