Geant4-11
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#include "PTL/Threading.hh"
26#include "PTL/AutoLock.hh"
27#include "PTL/Globals.hh"
28
29#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
30# include <Windows.h>
31#else
32# include <sys/syscall.h>
33# include <sys/types.h>
34# include <unistd.h>
35#endif
36
37#include <atomic>
38
39using namespace PTL;
40
41//======================================================================================//
42
43namespace
44{
45thread_local int ThreadID = Threading::MASTER_ID;
46} // namespace
47
48//======================================================================================//
49
52{
53 // In multithreaded mode return Thread ID
54 return std::this_thread::get_id();
55}
56
57//======================================================================================//
58
59unsigned
61{
62 return std::thread::hardware_concurrency();
63}
64
65//======================================================================================//
66
67void
69{
70 ThreadID = value;
71}
72
73int
75{
76 return ThreadID;
77}
78
79//======================================================================================//
80
81bool
83{
84#if defined(__linux__) || defined(_AIX)
85 cpu_set_t* aset = new cpu_set_t;
86 CPU_ZERO(aset);
87 CPU_SET(cpu, aset);
88 pthread_t& _aT = static_cast<pthread_t&>(aT);
89 return (pthread_setaffinity_np(_aT, sizeof(cpu_set_t), aset) == 0);
90#else // Not available for Mac, WIN,...
91 ConsumeParameters(cpu, aT);
92 return true;
93#endif
94}
95
96//======================================================================================//
Pid_t GetPidId()
Definition: Threading.cc:51
unsigned GetNumberOfCores()
Definition: Threading.cc:60
void SetThreadId(int aNewValue)
Definition: Threading.cc:68
int GetThreadId()
Definition: Threading.cc:74
bool SetPinAffinity(int idx, NativeThread &at)
Definition: Threading.cc:82
Definition: AutoLock.hh:254
std::thread::native_handle_type NativeThread
Definition: Threading.hh:129
std::thread::id Pid_t
Definition: Threading.hh:132
void ConsumeParameters(Args...)
Definition: Utility.hh:44