Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
Threading.cc
Go to the documentation of this file.
1//
2// MIT License
3// Copyright (c) 2020 Jonathan R. Madsen
4// Permission is hereby granted, free of charge, to any person obtaining a copy
5// of this software and associated documentation files (the "Software"), to deal
6// in the Software without restriction, including without limitation the rights
7// to use, copy, modify, merge, publish, distribute, sublicense, and
8// copies of the Software, and to permit persons to whom the Software is
9// furnished to do so, subject to the following conditions:
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
12// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
13// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
15// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
16// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
17// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18//
19// ---------------------------------------------------------------
20// Tasking class implementation
21//
22// Threading.cc
23//
24// ---------------------------------------------------------------
25// Author: Andrea Dotti (15 Feb 2013): First Implementation
26// ---------------------------------------------------------------
27
28#include "PTL/Threading.hh"
29#include "PTL/AutoLock.hh"
30#include "PTL/Globals.hh"
31
32#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
33# include <Windows.h>
34#else
35# include <sys/syscall.h>
36# include <sys/types.h>
37# include <unistd.h>
38#endif
39
40#include <atomic>
41
42using namespace PTL;
43
44//======================================================================================//
45
46namespace
47{
48thread_local int ThreadID = Threading::MASTER_ID;
49std::atomic_int numActThreads(0);
50} // namespace
51
52//======================================================================================//
53
56{
57 // In multithreaded mode return Thread ID
58 return std::this_thread::get_id();
59}
60
61//======================================================================================//
62
63unsigned
65{
66 return std::thread::hardware_concurrency();
67}
68
69//======================================================================================//
70
71void
73{
74 ThreadID = value;
75}
76int
78{
79 return ThreadID;
80}
81bool
83{
84 return (ThreadID >= 0);
85}
86bool
88{
89 return (ThreadID == MASTER_ID);
90}
91
92//======================================================================================//
93
94bool
96{
97#if defined(__linux__) || defined(_AIX)
98 cpu_set_t* aset = new cpu_set_t;
99 CPU_ZERO(aset);
100 CPU_SET(cpu, aset);
101 pthread_t& _aT = static_cast<pthread_t&>(aT);
102 return (pthread_setaffinity_np(_aT, sizeof(cpu_set_t), aset) == 0);
103#else // Not available for Mac, WIN,...
104 ConsumeParameters(cpu, aT);
105 return true;
106#endif
107}
108
109//======================================================================================//
110
111int
113{
114 return numActThreads--;
115}
116int
118{
119 return numActThreads++;
120}
121int
123{
124 return numActThreads.load();
125}
126
127//======================================================================================//
Pid_t GetPidId()
Definition: Threading.cc:55
int WorkerThreadLeavesPool()
Definition: Threading.cc:112
unsigned GetNumberOfCores()
Definition: Threading.cc:64
bool IsMasterThread()
Definition: Threading.cc:87
int WorkerThreadJoinsPool()
Definition: Threading.cc:117
void SetThreadId(int aNewValue)
Definition: Threading.cc:72
bool IsWorkerThread()
Definition: Threading.cc:82
int GetNumberOfRunningWorkerThreads()
Definition: Threading.cc:122
int GetThreadId()
Definition: Threading.cc:77
bool SetPinAffinity(int idx, NativeThread &at)
Definition: Threading.cc:95
Definition: AutoLock.hh:254
std::thread::native_handle_type NativeThread
Definition: Threading.hh:158
std::thread::id Pid_t
Definition: Threading.hh:174
void ConsumeParameters(Args...)
Definition: Utility.hh:44