Geant4-11
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 "PTL/Globals.hh"
29#include "PTL/Types.hh"
30
31#include <array>
32#include <chrono>
33#include <condition_variable>
34#include <future>
35#include <mutex>
36#include <thread>
37#include <vector>
38
39namespace PTL
40{
41// Macro to put current thread to sleep
42//
43#define THREADSLEEP(tick) std::this_thread::sleep_for(std::chrono::seconds(tick))
44
45// will be used in the future when migrating threading to task-based style
46template <typename Tp>
47using Future = std::future<Tp>;
48template <typename Tp>
49using SharedFuture = std::shared_future<Tp>;
50template <typename Tp>
51using Promise = std::promise<Tp>;
52
53//
54// NOTE ON Tasking SERIAL BUILDS AND MUTEX/UNIQUE_LOCK
55// ==================================================
56//
57// Mutex and RecursiveMutex are always C++11 std::mutex types
58// however, in serial mode, using MUTEXLOCK and MUTEXUNLOCK on these
59// types has no effect -- i.e. the mutexes are not actually locked or unlocked
60//
61// Additionally, when a Mutex or RecursiveMutex is used with AutoLock
62// and RecursiveAutoLock, respectively, these classes also suppressing
63// the locking and unlocking of the mutex. Regardless of the build type,
64// AutoLock and RecursiveAutoLock inherit from std::unique_lock<std::mutex>
65// and std::unique_lock<std::recursive_mutex>, respectively. This means
66// that in situations (such as is needed by the analysis category), the
67// AutoLock and RecursiveAutoLock can be passed to functions requesting
68// a std::unique_lock. Within these functions, since std::unique_lock
69// member functions are not virtual, they will not retain the dummy locking
70// and unlocking behavior
71// --> An example of this behavior can be found in AutoLock.hh
72//
73// Jonathan R. Madsen (February 21, 2018)
74//
75
76// global mutex types
78typedef std::recursive_mutex RecursiveMutex;
79
80// mutex macros
81#define MUTEX_INITIALIZER \
82 {}
83#define MUTEXINIT(mutex) \
84 ; \
85 ;
86#define MUTEXDESTROY(mutex) \
87 ; \
88 ;
89
90// static functions: get_id(), sleep_for(...), sleep_until(...), yield(),
91namespace ThisThread
92{
93using namespace std::this_thread;
94}
95
96// will be used in the future when migrating threading to task-based style
97// and are currently used in unit tests
98template <typename Tp>
99using Promise = std::promise<Tp>;
100template <typename Tp>
101using Future = std::future<Tp>;
102template <typename Tp>
103using SharedFuture = std::shared_future<Tp>;
104
105// Some useful types
107typedef void* ThreadFunArgType;
108typedef int (*thread_lock)(Mutex*);
109typedef int (*thread_unlock)(Mutex*);
110
111// Helper function for getting a unique static mutex for a specific
112// class or type
113// Usage example:
114// a template class "Cache<T>" that required a static
115// mutex for specific to type T:
116// AutoLock l(TypeMutex<Cache<T>>());
117template <typename Tp, typename MutexTp = Mutex, size_t N = 4>
118MutexTp&
119TypeMutex(const unsigned int& _n = 0)
120{
121 static std::array<MutexTp, N> _mutex_array{};
122 return _mutex_array[_n % N];
123}
124
125//======================================================================================//
126
127// global thread types
128typedef std::thread Thread;
129typedef std::thread::native_handle_type NativeThread;
130
131// std::thread::id does not cast to integer
132typedef std::thread::id Pid_t;
133
134// Condition
135typedef std::condition_variable Condition;
136//
137
138// Thread identifier
139typedef Thread::id ThreadId;
140
141//======================================================================================//
142
143namespace Threading
144{
145enum
146{
150 GENERICTHREAD_ID = -1000
152
153Pid_t
154GetPidId();
155
156unsigned
158
159int
161
162void
163SetThreadId(int aNewValue);
164
165bool
166SetPinAffinity(int idx, NativeThread& at);
167
168} // namespace Threading
169} // namespace PTL
G4int(*)(G4Mutex *) thread_unlock
Definition: G4Threading.hh:115
G4int(*)(G4Mutex *) thread_lock
Definition: G4Threading.hh:113
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::recursive_mutex RecursiveMutex
Definition: Threading.hh:78
std::promise< Tp > Promise
Definition: Threading.hh:51
void * ThreadFunArgType
Definition: Threading.hh:107
void * ThreadFunReturnType
Definition: Threading.hh:106
std::future< Tp > Future
Definition: Threading.hh:47
MutexTp & TypeMutex(const unsigned int &_n=0)
Definition: Threading.hh:119
std::condition_variable Condition
Definition: Threading.hh:135
std::mutex Mutex
Definition: Threading.hh:77
std::thread::id Pid_t
Definition: Threading.hh:132
std::thread Thread
Definition: Threading.hh:128
std::shared_future< Tp > SharedFuture
Definition: Threading.hh:49
Thread::id ThreadId
Definition: Threading.hh:139