Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
PTL Namespace Reference

Namespaces

namespace  internal
 
namespace  mpl
 
namespace  tbb
 
namespace  ThisThread
 
namespace  thread_pool
 
namespace  Threading
 

Classes

class  Backtrace
 
class  EnvSettings
 
struct  JoinFunction
 
struct  JoinFunction< void, JoinArg >
 
struct  JoinFunction< void, void >
 
class  PackagedTask
 The task class is supplied to thread_pool. More...
 
struct  ScopeDestructor
 
class  Singleton
 Singleton object that allows a deleter class to be specified. More...
 
class  Task
 The task class is supplied to thread_pool. More...
 
class  Task< RetT, void >
 The task class is supplied to thread_pool. More...
 
class  Task< void, void >
 The task class is supplied to thread_pool. More...
 
class  TaskFuture
 The task class is supplied to thread_pool. More...
 
class  TaskGroup
 
class  TaskManager
 
class  TaskRunManager
 
class  TaskSubQueue
 
class  TemplateAutoLock
 
class  ThreadData
 
class  ThreadPool
 
class  Timer
 
class  UserTaskQueue
 
class  VTask
 VTask is the abstract class stored in thread_pool. More...
 
class  VUserTaskQueue
 

Typedefs

using AutoLock = TemplateAutoLock< Mutex >
 
using RecursiveAutoLock = TemplateAutoLock< RecursiveMutex >
 
template<typename FuncT , typename... ArgTypes>
using ResultOf_t = typename std::result_of< FuncT(ArgTypes...)>::type
 
template<typename T >
using decay_t = typename std::decay< T >::type
 
template<bool B, typename T = void>
using enable_if_t = typename std::enable_if< B, T >::type
 
template<typename Tp , typename Arg = Tp>
using TBBTaskGroup = TaskGroup< Tp, Arg >
 
using tbb_global_control_t = tbb::global_control
 
using tbb_task_group_t = tbb::task_group
 
using tbb_task_arena_t = tbb::task_arena
 
using Thread = std::thread
 
using NativeThread = std::thread::native_handle_type
 
using Pid_t = std::thread::id
 
using Condition = std::condition_variable
 
using ThreadId = Thread::id
 
template<typename Tp >
using Future = std::future< Tp >
 
template<typename Tp >
using SharedFuture = std::shared_future< Tp >
 
template<typename Tp >
using Promise = std::promise< Tp >
 
using Mutex = std::mutex
 
using RecursiveMutex = std::recursive_mutex
 
template<typename Tp >
using EnvChoice = std::tuple< Tp, std::string, std::string >
 
template<typename Tp >
using EnvChoiceList = std::set< EnvChoice< Tp > >
 

Functions

std::string Demangle (const char *_str)
 
std::string Demangle (const std::string &_str)
 
template<typename Tp >
std::string Demangle ()
 
template<typename Tp , typename MutexTp = Mutex, size_t N = 4>
MutexTp & TypeMutex (const unsigned int &_n=0)
 
std::ostream & operator<< (std::ostream &os, const Timer &t)
 
template<typename... Args>
void ConsumeParameters (Args &&...)
 
template<typename Tp >
Tp GetEnv (const std::string &env_id, Tp _default=Tp())
 
template<>
bool GetEnv (const std::string &env_id, bool _default)
 
template<typename Tp >
Tp GetEnv (const std::string &env_id, Tp _default, const std::string &msg)
 
template<typename Tp >
Tp GetEnv (const std::string &env_id, const EnvChoiceList< Tp > &_choices, Tp _default)
 
template<typename Tp >
Tp GetChoice (const EnvChoiceList< Tp > &_choices, const std::string &str_var)
 
void PrintEnv (std::ostream &os=std::cout)
 

Detailed Description

Class Description:

This class provides a mechanism to create a mutex and locks/unlocks it. Can be used by applications to implement in a portable way a mutexing logic. Usage Example:

 #include "Threading.hh"
 #include "AutoLock.hh"

 /// defined somewhere -- static so all threads see the same mutex
 static Mutex aMutex;

 /// somewhere else:
 /// The AutoLock instance will automatically unlock the mutex when it
 /// goes out of scope. One typically defines the scope within { } if
 /// there is thread-safe code following the auto-lock

 {
     AutoLock l(&aMutex);
     ProtectedCode();
 }

 UnprotectedCode();

 /// When ProtectedCode() is calling a function that also tries to lock
 /// a normal AutoLock + Mutex will "deadlock". In other words, the
 /// the mutex in the ProtectedCode() function will wait forever to
 /// acquire the lock that is being held by the function that called
 /// ProtectedCode(). In this situation, use a RecursiveAutoLock +
 /// RecursiveMutex, e.g.

 /// defined somewhere -- static so all threads see the same mutex
 static RecursiveMutex aRecursiveMutex;

 /// this function is sometimes called directly and sometimes called
 /// from SomeFunction_B(), which also locks the mutex
 void SomeFunction_A()
 {
     /// when called from SomeFunction_B(), a Mutex + AutoLock will
     /// deadlock
     RecursiveAutoLock l(&aRecursiveMutex);
     /// do something
 }

 void SomeFunction_B()
 {

     {
         RecursiveAutoLock l(&aRecursiveMutex);
         SomeFunction_A();
     }

     UnprotectedCode();
 }

Author: Andrea Dotti (15 Feb 2013): First Implementation

Update: Jonathan Madsen (9 Feb 2018): Replaced custom implementation with inheritance from C++11 unique_lock, which inherits the following member functions:

  • unique_lock(unique_lock&& other) noexcept;
  • explicit unique_lock(mutex_type& m);
  • unique_lock(mutex_type& m, std::defer_lock_t t) noexcept;
  • unique_lock(mutex_type& m, std::try_to_lock_t t);
  • unique_lock(mutex_type& m, std::adopt_lock_t t);
  • template <typename Rep, typename Period> unique_lock(mutex_type& m, const std::chrono::duration<Rep,Period>& timeout_duration);
  • template<typename Clock, typename Duration> unique_lock(mutex_type& m, const std::chrono::time_point<Clock,Duration>& timeout_time);
  • void lock();
  • void unlock();
  • bool try_lock();
  • template <typename Rep, typename Period> bool try_lock_for(const std::chrono::duration<Rep,Period>&);
  • template <typename Rep, typename Period> bool try_lock_until(const std::chrono::time_point<Clock,Duration>&);
  • void swap(unique_lock& other) noexcept;
  • mutex_type* release() noexcept;
  • mutex_type* mutex() const noexcept;
  • bool owns_lock() const noexcept;
  • explicit operator bool() const noexcept;
  • unique_lock& operator=(unique_lock&& other);

Note that AutoLock is defined also for a sequential Tasking build but below regarding implementation (also found in Threading.hh)

     NOTE ON Tasking SERIAL BUILDS AND MUTEX/UNIQUE_LOCK
     ==================================================

Mutex and RecursiveMutex are always C++11 std::mutex types however, in serial mode, using MUTEXLOCK and MUTEXUNLOCK on these types has no effect – i.e. the mutexes are not actually locked or unlocked

Additionally, when a Mutex or RecursiveMutex is used with AutoLock and RecursiveAutoLock, respectively, these classes also suppressing the locking and unlocking of the mutex. Regardless of the build type, AutoLock and RecursiveAutoLock inherit from std::unique_lock<std::mutex> and std::unique_lock<std::recursive_mutex>, respectively. This means that in situations (such as is needed by the analysis category), the AutoLock and RecursiveAutoLock can be passed to functions requesting a std::unique_lock. Within these functions, since std::unique_lock member functions are not virtual, they will not retain the dummy locking and unlocking behavior --> An example of this behavior can be found below

Jonathan R. Madsen (February 21, 2018)

Typedef Documentation

◆ AutoLock

Definition at line 479 of file AutoLock.hh.

◆ Condition

using PTL::Condition = typedef std::condition_variable

Definition at line 43 of file Threading.hh.

◆ decay_t

template<typename T >
using PTL::decay_t = typedef typename std::decay<T>::type

Definition at line 54 of file Globals.hh.

◆ enable_if_t

template<bool B, typename T = void>
using PTL::enable_if_t = typedef typename std::enable_if<B, T>::type

Definition at line 57 of file Globals.hh.

◆ EnvChoice

template<typename Tp >
using PTL::EnvChoice = typedef std::tuple<Tp, std::string, std::string>

Definition at line 50 of file Utility.hh.

◆ EnvChoiceList

template<typename Tp >
using PTL::EnvChoiceList = typedef std::set<EnvChoice<Tp> >

Definition at line 55 of file Utility.hh.

◆ Future

template<typename Tp >
using PTL::Future = typedef std::future<Tp>

Definition at line 50 of file Threading.hh.

◆ Mutex

using PTL::Mutex = typedef std::mutex

Definition at line 57 of file Threading.hh.

◆ NativeThread

using PTL::NativeThread = typedef std::thread::native_handle_type

Definition at line 38 of file Threading.hh.

◆ Pid_t

using PTL::Pid_t = typedef std::thread::id

Definition at line 40 of file Threading.hh.

◆ Promise

template<typename Tp >
using PTL::Promise = typedef std::promise<Tp>

Definition at line 54 of file Threading.hh.

◆ RecursiveAutoLock

Definition at line 480 of file AutoLock.hh.

◆ RecursiveMutex

using PTL::RecursiveMutex = typedef std::recursive_mutex

Definition at line 58 of file Threading.hh.

◆ ResultOf_t

template<typename FuncT , typename... ArgTypes>
using PTL::ResultOf_t = typedef typename std::result_of<FuncT(ArgTypes...)>::type

Definition at line 81 of file Backtrace.hh.

◆ SharedFuture

template<typename Tp >
using PTL::SharedFuture = typedef std::shared_future<Tp>

Definition at line 52 of file Threading.hh.

◆ tbb_global_control_t

Definition at line 123 of file ThreadData.hh.

◆ tbb_task_arena_t

Definition at line 125 of file ThreadData.hh.

◆ tbb_task_group_t

Definition at line 124 of file ThreadData.hh.

◆ TBBTaskGroup

template<typename Tp , typename Arg = Tp>
using PTL::TBBTaskGroup = typedef TaskGroup<Tp, Arg>

Definition at line 38 of file TBBTaskGroup.hh.

◆ Thread

using PTL::Thread = typedef std::thread

Definition at line 37 of file Threading.hh.

◆ ThreadId

using PTL::ThreadId = typedef Thread::id

Definition at line 46 of file Threading.hh.

Function Documentation

◆ ConsumeParameters()

template<typename... Args>
void PTL::ConsumeParameters ( Args &&  ...)

◆ Demangle() [1/3]

template<typename Tp >
std::string PTL::Demangle ( )
inline

Definition at line 135 of file Backtrace.hh.

136{
137 return Demangle(typeid(Tp).name());
138}
std::string Demangle(const char *_str)
Definition: Backtrace.hh:109

Referenced by Demangle().

◆ Demangle() [2/3]

std::string PTL::Demangle ( const char *  _str)
inline

Definition at line 109 of file Backtrace.hh.

110{
111#if defined(PTL_DEMANGLE_AVAILABLE)
112 // demangling a string when delimiting
113 int _status = 0;
114 char* _ret = ::abi::__cxa_demangle(_str, nullptr, nullptr, &_status);
115 if(_ret && _status == 0)
116 return std::string(const_cast<const char*>(_ret));
117 return _str;
118#else
119 return _str;
120#endif
121}

◆ Demangle() [3/3]

std::string PTL::Demangle ( const std::string &  _str)
inline

Definition at line 126 of file Backtrace.hh.

127{
128 return Demangle(_str.c_str());
129}

◆ GetChoice()

template<typename Tp >
Tp PTL::GetChoice ( const EnvChoiceList< Tp > &  _choices,
const std::string &  str_var 
)

Definition at line 315 of file Utility.hh.

316{
317 auto asupper = [](std::string var) {
318 for(auto& itr : var)
319 itr = (char)std::toupper(itr);
320 return var;
321 };
322
323 std::string upp_var = asupper(str_var);
324 Tp var = Tp();
325 // check to see if string matches a choice
326 for(const auto& itr : _choices)
327 {
328 if(asupper(std::get<1>(itr)) == upp_var)
329 {
330 // record value defined by environment
331 return std::get<0>(itr);
332 }
333 }
334 std::istringstream iss(str_var);
335 iss >> var;
336 // check to see if string matches a choice
337 for(const auto& itr : _choices)
338 {
339 if(var == std::get<0>(itr))
340 {
341 // record value defined by environment
342 return var;
343 }
344 }
345 // the value set in env did not match any choices
346 std::stringstream ss;
347 ss << "\n### Environment setting error @ " << __FUNCTION__ << " (line " << __LINE__
348 << ")! Invalid selection \"" << str_var << "\". Valid choices are:\n";
349 for(const auto& itr : _choices)
350 ss << "\t\"" << std::get<0>(itr) << "\" or \"" << std::get<1>(itr) << "\" ("
351 << std::get<2>(itr) << ")\n";
352 std::cerr << ss.str() << std::endl;
353 abort();
354}

◆ GetEnv() [1/4]

template<>
bool PTL::GetEnv ( const std::string &  env_id,
bool  _default 
)
inline

Definition at line 180 of file Utility.hh.

181{
182 char* env_var = std::getenv(env_id.c_str());
183 if(env_var)
184 {
185 std::string var = std::string(env_var);
186 bool val = true;
187 if(var.find_first_not_of("0123456789") == std::string::npos)
188 val = (bool) atoi(var.c_str());
189 else
190 {
191 for(auto& itr : var)
192 itr = (char)std::tolower(itr);
193 if(var == "off" || var == "false")
194 val = false;
195 }
196 // record value defined by environment
197 EnvSettings::GetInstance()->insert<bool>(env_id, val);
198 return val;
199 }
200 // record default value
201 EnvSettings::GetInstance()->insert<bool>(env_id, false);
202
203 // return default if not specified in environment
204 return _default;
205}

◆ GetEnv() [2/4]

template<typename Tp >
Tp PTL::GetEnv ( const std::string &  env_id,
const EnvChoiceList< Tp > &  _choices,
Tp  _default 
)

Definition at line 246 of file Utility.hh.

247{
248 auto asupper = [](std::string var) {
249 for(auto& itr : var)
250 itr = (char)std::toupper(itr);
251 return var;
252 };
253
254 char* env_var = std::getenv(env_id.c_str());
255 if(env_var)
256 {
257 std::string str_var = std::string(env_var);
258 std::string upp_var = asupper(str_var);
259 Tp var = Tp();
260 // check to see if string matches a choice
261 for(const auto& itr : _choices)
262 {
263 if(asupper(std::get<1>(itr)) == upp_var)
264 {
265 // record value defined by environment
266 EnvSettings::GetInstance()->insert(env_id, itr);
267 return std::get<0>(itr);
268 }
269 }
270 std::istringstream iss(str_var);
271 iss >> var;
272 // check to see if string matches a choice
273 for(const auto& itr : _choices)
274 {
275 if(var == std::get<0>(itr))
276 {
277 // record value defined by environment
278 EnvSettings::GetInstance()->insert(env_id, itr);
279 return var;
280 }
281 }
282 // the value set in env did not match any choices
283 std::stringstream ss;
284 ss << "\n### Environment setting error @ " << __FUNCTION__ << " (line "
285 << __LINE__ << ")! Invalid selection for \"" << env_id
286 << "\". Valid choices are:\n";
287 for(const auto& itr : _choices)
288 ss << "\t\"" << std::get<0>(itr) << "\" or \"" << std::get<1>(itr) << "\" ("
289 << std::get<2>(itr) << ")\n";
290 std::cerr << ss.str() << std::endl;
291 abort();
292 }
293
294 std::string _name = "???";
295 std::string _desc = "description not provided";
296 for(const auto& itr : _choices)
297 if(std::get<0>(itr) == _default)
298 {
299 _name = std::get<1>(itr);
300 _desc = std::get<2>(itr);
301 break;
302 }
303
304 // record default value
305 EnvSettings::GetInstance()->insert(env_id, EnvChoice<Tp>(_default, _name, _desc));
306
307 // return default if not specified in environment
308 return _default;
309}

◆ GetEnv() [3/4]

template<typename Tp >
Tp PTL::GetEnv ( const std::string &  env_id,
Tp  _default,
const std::string &  msg 
)

Definition at line 212 of file Utility.hh.

213{
214 char* env_var = std::getenv(env_id.c_str());
215 if(env_var)
216 {
217 std::string str_var = std::string(env_var);
218 std::istringstream iss(str_var);
219 Tp var = Tp();
220 iss >> var;
221 std::cout << "Environment variable \"" << env_id << "\" enabled with "
222 << "value == " << var << ". " << msg << std::endl;
223 // record value defined by environment
224 EnvSettings::GetInstance()->insert<Tp>(env_id, var);
225 return var;
226 }
227 // record default value
228 EnvSettings::GetInstance()->insert<Tp>(env_id, _default);
229
230 // return default if not specified in environment
231 return _default;
232}

◆ GetEnv() [4/4]

template<typename Tp >
Tp PTL::GetEnv ( const std::string &  env_id,
Tp  _default = Tp() 
)

Definition at line 155 of file Utility.hh.

156{
157 char* env_var = std::getenv(env_id.c_str());
158 if(env_var)
159 {
160 std::string str_var = std::string(env_var);
161 std::istringstream iss(str_var);
162 Tp var = Tp();
163 iss >> var;
164 // record value defined by environment
165 EnvSettings::GetInstance()->insert<Tp>(env_id, var);
166 return var;
167 }
168 // record default value
169 EnvSettings::GetInstance()->insert<Tp>(env_id, _default);
170
171 // return default if not specified in environment
172 return _default;
173}

◆ operator<<()

std::ostream & PTL::operator<< ( std::ostream &  os,
const Timer t 
)
inline

Definition at line 165 of file Timer.hh.

167{
168 // so fixed doesn't propagate
169 std::stringstream ss;
170 ss << std::fixed;
171 if(t.IsValid())
172 {
173 ss << "Real=" << t.GetRealElapsed() << "s User=" << t.GetUserElapsed()
174 << "s Sys=" << t.GetSystemElapsed() << "s";
175
176 // avoid possible FPE error
177 if(t.GetRealElapsed() > 1.0e-6)
178 {
179 double cpu_util =
180 (t.GetUserElapsed() + t.GetSystemElapsed()) / t.GetRealElapsed() * 100.0;
181 ss << std::setprecision(1);
182 ss << " [Cpu=" << std::setprecision(1) << cpu_util << "%]";
183 }
184 }
185 else
186 {
187 ss << "Real=****s User=****s Sys=****s";
188 }
189 os << ss.str();
190
191 return os;
192}
bool IsValid() const
Definition: Timer.hh:149
double GetUserElapsed() const
Definition: Timer.cc:113
double GetRealElapsed() const
Definition: Timer.cc:85
double GetSystemElapsed() const
Definition: Timer.cc:99

◆ PrintEnv()

void PTL::PrintEnv ( std::ostream &  os = std::cout)
inline

Definition at line 359 of file Utility.hh.

360{
361 os << (*EnvSettings::GetInstance());
362}

◆ TypeMutex()

template<typename Tp , typename MutexTp = Mutex, size_t N = 4>
MutexTp & PTL::TypeMutex ( const unsigned int &  _n = 0)