Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
Threading.hh
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 header file
21//
22// Class Description:
23//
24// This file defines types and macros used to expose Tasking threading model.
25
26#pragma once
27
28#include <array>
29#include <cstddef>
30#include <future>
31#include <mutex>
32#include <thread>
33
34namespace PTL
35{
36// global thread types
37using Thread = std::thread;
38using NativeThread = std::thread::native_handle_type;
39// std::thread::id does not cast to integer
40using Pid_t = std::thread::id;
41
42// Condition
43using Condition = std::condition_variable;
44
45// Thread identifier
46using ThreadId = Thread::id;
47
48// will be used in the future when migrating threading to task-based style
49template <typename Tp>
50using Future = std::future<Tp>;
51template <typename Tp>
52using SharedFuture = std::shared_future<Tp>;
53template <typename Tp>
54using Promise = std::promise<Tp>;
55
56// global mutex types
57using Mutex = std::mutex;
58using RecursiveMutex = std::recursive_mutex;
59
60// static functions: get_id(), sleep_for(...), sleep_until(...), yield(),
61namespace ThisThread
62{
63using namespace std::this_thread;
64}
65
66// Helper function for getting a unique static mutex for a specific
67// class or type
68// Usage example:
69// a template class "Cache<T>" that required a static
70// mutex for specific to type T:
71// AutoLock l(TypeMutex<Cache<T>>());
72template <typename Tp, typename MutexTp = Mutex, size_t N = 4>
73MutexTp&
74TypeMutex(const unsigned int& _n = 0)
75{
76 static std::array<MutexTp, N> _mutex_array{};
77 return _mutex_array[_n % N];
78}
79
80//======================================================================================//
81
82namespace Threading
83{
84enum
85{
89 GENERICTHREAD_ID = -1000
90};
91
93GetPidId();
94
95unsigned
97
98unsigned
100
101int
103
104void
105SetThreadId(int aNewValue);
106
107bool
108SetPinAffinity(int idx);
109
110bool
111SetThreadPriority(int _v);
112
113bool
114SetPinAffinity(int idx, NativeThread& _t);
115
116bool
118
119} // namespace Threading
120} // namespace PTL
#define N
Definition: crc32.c:56
Pid_t GetPidId()
Definition: Threading.cc:54
unsigned GetNumberOfCores()
Definition: Threading.cc:63
unsigned GetNumberOfPhysicalCpus()
Definition: Threading.cc:71
bool SetPinAffinity(int idx)
Definition: Threading.cc:129
bool SetThreadPriority(int _v)
Definition: Threading.cc:146
void SetThreadId(int aNewValue)
Definition: Threading.cc:115
int GetThreadId()
Definition: Threading.cc:121
Definition: AutoLock.hh:255
std::thread::id Pid_t
Definition: Threading.hh:40
std::thread::native_handle_type NativeThread
Definition: Threading.hh:38
std::condition_variable Condition
Definition: Threading.hh:43
Thread::id ThreadId
Definition: Threading.hh:46
std::mutex Mutex
Definition: Threading.hh:57
std::thread Thread
Definition: Threading.hh:37
std::promise< Tp > Promise
Definition: Threading.hh:54
std::future< Tp > Future
Definition: Threading.hh:50
MutexTp & TypeMutex(const unsigned int &_n=0)
Definition: Threading.hh:74
std::recursive_mutex RecursiveMutex
Definition: Threading.hh:58
std::shared_future< Tp > SharedFuture
Definition: Threading.hh:52