ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4Threading.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file 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 //
27 // --------------------------------------------------------------
28 // GEANT 4 class implementation file
29 //
30 // G4Threading.cc
31 //
32 // ---------------------------------------------------------------
33 // Author: Andrea Dotti (15 Feb 2013): First Implementation
34 // ---------------------------------------------------------------
35 
36 #include "G4Threading.hh"
37 #include "G4AutoDelete.hh"
38 #include "G4AutoLock.hh"
39 #include "globals.hh"
40 
41 #if defined (WIN32)
42  #include <Windows.h>
43 #else
44  #include <unistd.h>
45  #include <sys/types.h>
46  #include <sys/syscall.h>
47 #endif
48 
49 #if defined(G4MULTITHREADED)
50 
51 #include <atomic>
52 
53 namespace
54 {
56  G4bool isMTAppType = false;
57 }
58 
60 {
61  // In multithreaded mode return Thread ID
62  return std::this_thread::get_id();
63 }
64 
66 {
67  return std::thread::hardware_concurrency();
68 }
69 
70 void G4Threading::G4SetThreadId(G4int value ) { G4ThreadID = value; }
71 G4int G4Threading::G4GetThreadId() { return G4ThreadID; }
72 G4bool G4Threading::IsWorkerThread() { return (G4ThreadID>=0); }
73 G4bool G4Threading::IsMasterThread() { return (G4ThreadID==MASTER_ID); }
74 
75 #if defined(__linux__) || defined(_AIX)
77 {
78  cpu_set_t* aset = new cpu_set_t;
80  CPU_ZERO(aset);
81  CPU_SET(cpu, aset);
82  pthread_t& _aT = (pthread_t&) (aT);
83  return (pthread_setaffinity_np(_aT, sizeof(cpu_set_t), aset) == 0);
84 }
85 #else //Not available for Mac, WIN,...
87 {
88  G4Exception("G4Threading::G4SetPinAffinity()",
89  "NotImplemented", JustWarning,
90  "Affinity setting not available for this architecture, "
91  "ignoring...");
92  return true;
93 }
94 #endif
95 
97 {
98  isMTAppType = value;
99 }
100 
102 {
103  return isMTAppType;
104 }
105 
106 namespace
107 {
108  std::atomic_int numActThreads(0);
109 }
110 int G4Threading::WorkerThreadLeavesPool() { return numActThreads--; }
111 int G4Threading::WorkerThreadJoinsPool() { return numActThreads++;}
112 G4int G4Threading::GetNumberOfRunningWorkerThreads() { return numActThreads.load(); }
113 
114 #else // Sequential mode
115 
117 {
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 
131 
133 
139 
140 #endif