timemory 3.3.0
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
tim::singleton< Type, PointerT, TagT > Class Template Reference

Thread-safe singleton management. More...

#include "timemory/utility/singleton.hpp"

+ Collaboration diagram for tim::singleton< Type, PointerT, TagT >:

Public Types

using this_type = singleton< Type, PointerT, TagT >
 
using thread_id_t = std::thread::id
 
using mutex_t = std::recursive_mutex
 
using auto_lock_t = std::unique_lock< mutex_t >
 
using pointer = Type *
 
using children_t = std::set< pointer >
 
using smart_pointer = PointerT
 
using deleter_t = std::function< void(PointerT &)>
 
using dtor_map_t = std::map< pointer, std::function< void()> >
 
template<bool B, typename T = int>
using enable_if_t = typename std::enable_if< B, T >::type
 

Public Member Functions

 singleton ()
 
 ~singleton ()
 
 singleton (const singleton &)=delete
 
 singleton (singleton &&)=delete
 
singletonoperator= (const singleton &)=delete
 
singletonoperator= (singleton &&)=delete
 
void initialize ()
 
void reset (pointer ptr)
 
void reset ()
 

Static Public Member Functions

static pointer instance ()
 
static pointer master_instance ()
 
static smart_pointersmart_instance ()
 
static smart_pointersmart_master_instance ()
 
static pointer instance_ptr ()
 
static pointer master_instance_ptr ()
 
static thread_id_t master_thread_id ()
 
static children_t children ()
 
static bool is_master (pointer ptr)
 
static bool is_master_thread ()
 
static void insert (smart_pointer &itr)
 
static void remove (pointer itr)
 
static mutex_tget_mutex ()
 

Detailed Description

template<typename Type, typename PointerT, typename TagT>
class tim::singleton< Type, PointerT, TagT >

Thread-safe singleton management.

Definition at line 111 of file singleton.hpp.

Member Typedef Documentation

◆ auto_lock_t

template<typename Type , typename PointerT , typename TagT >
using tim::singleton< Type, PointerT, TagT >::auto_lock_t = std::unique_lock<mutex_t>

Definition at line 117 of file singleton.hpp.

◆ children_t

template<typename Type , typename PointerT , typename TagT >
using tim::singleton< Type, PointerT, TagT >::children_t = std::set<pointer>

Definition at line 119 of file singleton.hpp.

◆ deleter_t

template<typename Type , typename PointerT , typename TagT >
using tim::singleton< Type, PointerT, TagT >::deleter_t = std::function<void(PointerT&)>

Definition at line 121 of file singleton.hpp.

◆ dtor_map_t

template<typename Type , typename PointerT , typename TagT >
using tim::singleton< Type, PointerT, TagT >::dtor_map_t = std::map<pointer, std::function<void()> >

Definition at line 122 of file singleton.hpp.

◆ enable_if_t

template<typename Type , typename PointerT , typename TagT >
template<bool B, typename T = int>
using tim::singleton< Type, PointerT, TagT >::enable_if_t = typename std::enable_if<B, T>::type

Definition at line 125 of file singleton.hpp.

◆ mutex_t

template<typename Type , typename PointerT , typename TagT >
using tim::singleton< Type, PointerT, TagT >::mutex_t = std::recursive_mutex

Definition at line 116 of file singleton.hpp.

◆ pointer

template<typename Type , typename PointerT , typename TagT >
using tim::singleton< Type, PointerT, TagT >::pointer = Type*

Definition at line 118 of file singleton.hpp.

◆ smart_pointer

template<typename Type , typename PointerT , typename TagT >
using tim::singleton< Type, PointerT, TagT >::smart_pointer = PointerT

Definition at line 120 of file singleton.hpp.

◆ this_type

template<typename Type , typename PointerT , typename TagT >
using tim::singleton< Type, PointerT, TagT >::this_type = singleton<Type, PointerT, TagT>

Definition at line 114 of file singleton.hpp.

◆ thread_id_t

template<typename Type , typename PointerT , typename TagT >
using tim::singleton< Type, PointerT, TagT >::thread_id_t = std::thread::id

Definition at line 115 of file singleton.hpp.

Constructor & Destructor Documentation

◆ singleton() [1/3]

template<typename Type , typename PointerT , typename TagT >
tim::singleton< Type, PointerT, TagT >::singleton

Definition at line 267 of file singleton.hpp.

268{
269 initialize();
270}
void initialize()
Definition: singleton.hpp:288

References tim::initialize().

◆ ~singleton()

template<typename Type , typename PointerT , typename TagT >
tim::singleton< Type, PointerT, TagT >::~singleton

Definition at line 275 of file singleton.hpp.

276{
277 if(std::this_thread::get_id() == f_master_thread())
278 f_master_instance() = nullptr;
279 auto& del = get_deleter();
280 if(del)
281 del(_master_instance());
282}

◆ singleton() [2/3]

template<typename Type , typename PointerT , typename TagT >
tim::singleton< Type, PointerT, TagT >::singleton ( const singleton< Type, PointerT, TagT > &  )
delete

◆ singleton() [3/3]

template<typename Type , typename PointerT , typename TagT >
tim::singleton< Type, PointerT, TagT >::singleton ( singleton< Type, PointerT, TagT > &&  )
delete

Member Function Documentation

◆ children()

template<typename Type , typename PointerT , typename TagT >
static children_t tim::singleton< Type, PointerT, TagT >::children ( )
inlinestatic

Definition at line 152 of file singleton.hpp.

152{ return f_children(); }

◆ get_mutex()

template<typename Type , typename PointerT , typename TagT >
static mutex_t & tim::singleton< Type, PointerT, TagT >::get_mutex ( )
inlinestatic

Definition at line 157 of file singleton.hpp.

157{ return f_mutex(); }

◆ initialize()

template<typename Type , typename PointerT , typename TagT >
void tim::singleton< Type, PointerT, TagT >::initialize

Definition at line 288 of file singleton.hpp.

289{
290 if(!f_master_instance())
291 {
292 f_master_thread() = std::this_thread::get_id();
293 f_master_instance() = new Type{};
294 }
295}

◆ insert()

template<typename Type , typename PointerT , typename TagT >
void tim::singleton< Type, PointerT, TagT >::insert ( smart_pointer itr)
static

Definition at line 351 of file singleton.hpp.

352{
353 auto_lock_t _lk{ f_mutex(), std::defer_lock };
354 if(!_lk.owns_lock())
355 _lk.lock();
356 f_children().insert(itr.get());
357 f_dtors().emplace(itr.get(), [&]() { itr.reset(); });
358}
std::unique_lock< mutex_t > auto_lock_t
Definition: singleton.hpp:117

◆ instance()

template<typename Type , typename PointerT , typename TagT >
singleton< Type, PointerT, TagT >::pointer tim::singleton< Type, PointerT, TagT >::instance
static

Definition at line 301 of file singleton.hpp.

302{
303 if(std::this_thread::get_id() == f_master_thread())
304 {
305 return master_instance();
306 }
307 if(!_local_instance().get())
308 {
309 _local_instance().reset(new Type{});
310 insert(_local_instance());
311 }
312 return _local_instance().get();
313}
static pointer master_instance()
Definition: singleton.hpp:319
static void insert(smart_pointer &itr)
Definition: singleton.hpp:351
auto get(const auto_bundle< Tag, Types... > &_obj)

References tim::get().

◆ instance_ptr()

template<typename Type , typename PointerT , typename TagT >
singleton< Type, PointerT, TagT >::pointer tim::singleton< Type, PointerT, TagT >::instance_ptr
static

Definition at line 333 of file singleton.hpp.

334{
335 return is_master_thread() ? f_master_instance() : _local_instance().get();
336}
static bool is_master_thread()
Definition: singleton.hpp:342

◆ is_master()

template<typename Type , typename PointerT , typename TagT >
static bool tim::singleton< Type, PointerT, TagT >::is_master ( pointer  ptr)
inlinestatic

Definition at line 153 of file singleton.hpp.

153{ return ptr == master_instance_ptr(); }
static pointer master_instance_ptr()
Definition: singleton.hpp:147

References tim::singleton< Type, PointerT, TagT >::master_instance_ptr().

◆ is_master_thread()

template<typename Type , typename PointerT , typename TagT >
bool tim::singleton< Type, PointerT, TagT >::is_master_thread
static

Definition at line 342 of file singleton.hpp.

343{
344 return std::this_thread::get_id() == f_master_thread();
345}

◆ master_instance()

template<typename Type , typename PointerT , typename TagT >
singleton< Type, PointerT, TagT >::pointer tim::singleton< Type, PointerT, TagT >::master_instance
static

Definition at line 319 of file singleton.hpp.

320{
321 if(!f_master_instance())
322 {
323 f_master_thread() = std::this_thread::get_id();
324 f_master_instance() = new Type{};
325 }
326 return f_master_instance();
327}

◆ master_instance_ptr()

template<typename Type , typename PointerT , typename TagT >
static pointer tim::singleton< Type, PointerT, TagT >::master_instance_ptr ( )
inlinestatic

Definition at line 147 of file singleton.hpp.

147{ return f_master_instance(); }

Referenced by tim::singleton< Type, PointerT, TagT >::is_master().

◆ master_thread_id()

template<typename Type , typename PointerT , typename TagT >
static thread_id_t tim::singleton< Type, PointerT, TagT >::master_thread_id ( )
inlinestatic

Definition at line 150 of file singleton.hpp.

150{ return f_master_thread(); }

◆ operator=() [1/2]

template<typename Type , typename PointerT , typename TagT >
singleton & tim::singleton< Type, PointerT, TagT >::operator= ( const singleton< Type, PointerT, TagT > &  )
delete

◆ operator=() [2/2]

template<typename Type , typename PointerT , typename TagT >
singleton & tim::singleton< Type, PointerT, TagT >::operator= ( singleton< Type, PointerT, TagT > &&  )
delete

◆ remove()

template<typename Type , typename PointerT , typename TagT >
void tim::singleton< Type, PointerT, TagT >::remove ( pointer  itr)
static

Definition at line 364 of file singleton.hpp.

365{
366 if(!itr)
367 return;
368 auto_lock_t _lk{ f_mutex(), std::defer_lock };
369 if(!_lk.owns_lock())
370 _lk.lock();
371 for(auto litr = f_children().begin(); litr != f_children().end(); ++litr)
372 {
373 if(*litr == itr)
374 {
375 f_children().erase(litr);
376 break;
377 }
378 }
379 auto ditr = f_dtors().find(itr);
380 if(ditr != f_dtors().end())
381 f_dtors().erase(ditr);
382}
return _hash_map end()

◆ reset() [1/2]

template<typename Type , typename PointerT , typename TagT >
void tim::singleton< Type, PointerT, TagT >::reset

Definition at line 423 of file singleton.hpp.

424{
425 if(_local_instance())
426 {
427 remove(_local_instance().get());
428 _local_instance().reset();
429 }
430
431 if(is_master_thread())
432 {
433 if(!f_dtors().empty())
434 {
435 dtor_map_t _dtors{};
436 std::swap(f_dtors(), _dtors);
437 for(auto& itr : _dtors)
438 itr.second();
439 }
440
441 if(_master_instance().get())
442 {
443 _master_instance().reset();
444 }
445 else if(f_master_instance())
446 {
447 auto& del = get_deleter();
448 del(_master_instance());
449 f_master_instance() = nullptr;
450 }
451 }
452 f_persistent_data().reset();
453}
std::map< pointer, std::function< void()> > dtor_map_t
Definition: singleton.hpp:122
static void remove(pointer itr)
Definition: singleton.hpp:364

References tim::get().

◆ reset() [2/2]

template<typename Type , typename PointerT , typename TagT >
void tim::singleton< Type, PointerT, TagT >::reset ( pointer  ptr)

Definition at line 388 of file singleton.hpp.

389{
390 if(is_master(ptr))
391 {
392 if(!f_dtors().empty())
393 {
394 dtor_map_t _dtors{};
395 std::swap(f_dtors(), _dtors);
396 for(auto& itr : _dtors)
397 itr.second();
398 }
399
400 if(_master_instance().get())
401 {
402 _master_instance().reset();
403 }
404 else if(f_master_instance())
405 {
406 auto& del = get_deleter();
407 del(_master_instance());
408 f_master_instance() = nullptr;
409 }
410 f_persistent_data().reset();
411 }
412 else
413 {
414 remove(_local_instance().get());
415 _local_instance().reset();
416 }
417}
static bool is_master(pointer ptr)
Definition: singleton.hpp:153

References tim::get().

◆ smart_instance()

template<typename Type , typename PointerT , typename TagT >
static smart_pointer & tim::singleton< Type, PointerT, TagT >::smart_instance ( )
inlinestatic

Definition at line 142 of file singleton.hpp.

142{ return _local_instance(); }

◆ smart_master_instance()

template<typename Type , typename PointerT , typename TagT >
static smart_pointer & tim::singleton< Type, PointerT, TagT >::smart_master_instance ( )
inlinestatic

Definition at line 143 of file singleton.hpp.

143{ return _master_instance(); }

The documentation for this class was generated from the following file: