Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4Threading.cc
이 파일의 문서화 페이지로 가기
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 {
62  // In multithreaded mode return Thread ID
63  return std::this_thread::get_id();
64 }
65 
67 {
68  return std::thread::hardware_concurrency();
69 }
70 
71 void G4Threading::G4SetThreadId(G4int value ) { G4ThreadID = value; }
72 G4int G4Threading::G4GetThreadId() { return G4ThreadID; }
73 G4bool G4Threading::IsWorkerThread() { return (G4ThreadID>=0); }
74 G4bool G4Threading::IsMasterThread() { return (G4ThreadID==MASTER_ID); }
75 
76 #if defined(__linux__) || defined(_AIX)
78 {
79  cpu_set_t* aset = new cpu_set_t;
81  CPU_ZERO(aset);
82  CPU_SET(cpu, aset);
83  pthread_t& _aT = (pthread_t&) (aT);
84  return (pthread_setaffinity_np(_aT, sizeof(cpu_set_t), aset) == 0);
85 }
86 #else //Not available for Mac, WIN,...
88 {
89  G4Exception("G4Threading::G4SetPinAffinity()",
90  "NotImplemented", JustWarning,
91  "Affinity setting not available for this architecture, "
92  "ignoring...");
93  return true;
94 }
95 #endif
96 
98 {
99  isMTAppType = value;
100 }
101 
103 {
104  return isMTAppType;
105 }
106 
107 namespace
108 {
109  std::atomic_int numActThreads(0);
110 }
111 int G4Threading::WorkerThreadLeavesPool() { return numActThreads--; }
112 int G4Threading::WorkerThreadJoinsPool() { return numActThreads++;}
113 G4int G4Threading::GetNumberOfRunningWorkerThreads() { return numActThreads.load(); }
114 
115 #else // Sequential mode
116 
118 {
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 
140 
141 #endif
G4int G4Pid_t
Definition: G4Threading.hh:239
int WorkerThreadJoinsPool()
Definition: G4Threading.cc:138
G4bool IsMasterThread()
Definition: G4Threading.cc:130
G4int G4GetNumberOfCores()
Definition: G4Threading.cc:127
#define G4ThreadLocal
Definition: tls.hh:69
void Register(T *inst)
Definition: G4AutoDelete.hh:65
int WorkerThreadLeavesPool()
Definition: G4Threading.cc:137
G4int GetNumberOfRunningWorkerThreads()
Definition: G4Threading.cc:139
bool G4bool
Definition: G4Types.hh:79
const XML_Char int const XML_Char * value
Definition: expat.h:331
G4bool IsWorkerThread()
Definition: G4Threading.cc:129
G4bool G4SetPinAffinity(G4int idx, G4NativeThread &at)
Definition: G4Threading.cc:133
void SetMultithreadedApplication(G4bool value)
Definition: G4Threading.cc:135
G4Pid_t G4GetPidId()
Definition: G4Threading.cc:117
G4DummyThread::native_handle_type G4NativeThread
Definition: G4Threading.hh:230
void G4SetThreadId(G4int aNewValue)
Definition: G4Threading.cc:131
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
G4bool IsMultithreadedApplication()
Definition: G4Threading.cc:136
int G4int
Definition: G4Types.hh:78
G4int G4GetThreadId()
Definition: G4Threading.cc:128