Nektar++
Public Types | Public Member Functions | Private Attributes | List of all members
Nektar::Thread::ThreadMaster Class Reference

#include <Thread.h>

Public Types

enum  ThreadManagerName { SessionJob , THREADMANAGER_MAX }
 

Public Member Functions

 ThreadMaster ()
 Constructor. More...
 
 ~ThreadMaster ()
 Destructor. More...
 
void SetThreadingType (const std::string &p_type)
 Sets what ThreadManagers will be created in CreateInstance. More...
 
ThreadManagerSharedPtrGetInstance (const ThreadManagerName t)
 Gets the ThreadManager associated with string s. More...
 
ThreadManagerSharedPtr CreateInstance (const ThreadManagerName t, unsigned int nThr)
 Creates an instance of a ThreadManager (which one is determined by a previous call to SetThreadingType) and associates it with the string s. More...
 

Private Attributes

std::vector< ThreadManagerSharedPtrm_threadManagers
 
std::shared_mutex m_mutex
 
std::string m_threadingType
 

Detailed Description

A class to manage multiple ThreadManagers. It also acts as a cut-out during static initialisation, where code attempts to grab a ThreadManager before any can have been initialised. When this happens the ThreadMaster creates a simple implementation of ThreadManager, ThreadStartupManager, which behaves as a single-threaded ThreadManager that fails if anything non-trivial is attempted. This means code should be cautious about caching the value of GetInstance(string), as it might change once that particular threading system is initialised.

Multiple ThreadManagers should now be possible. Each is identified with a string and can be recovered by calling Thread::GetThreadMaster().GetInstance(string).

ThreadMaster is thread-safe apart from CreateInstance. Call CreateInstance in single threaded code only.

Definition at line 396 of file Thread.h.

Member Enumeration Documentation

◆ ThreadManagerName

Enumerator
SessionJob 
THREADMANAGER_MAX 

Definition at line 404 of file Thread.h.

Constructor & Destructor Documentation

◆ ThreadMaster()

Nektar::Thread::ThreadMaster::ThreadMaster ( )

Constructor.

ThreadMaster implementation

Definition at line 110 of file Thread.cpp.

112{
113 // empty
114}
std::shared_mutex m_mutex
Definition: Thread.h:400
std::vector< ThreadManagerSharedPtr > m_threadManagers
Definition: Thread.h:399
std::string m_threadingType
Definition: Thread.h:401

◆ ~ThreadMaster()

Nektar::Thread::ThreadMaster::~ThreadMaster ( )

Destructor.

Will clear the list of ThreadManagers, destructing them.

Definition at line 119 of file Thread.cpp.

120{
121 // Locking is a bit pointless, since the map is empty after this call.
122 m_threadManagers.clear();
123}

References m_threadManagers.

Member Function Documentation

◆ CreateInstance()

ThreadManagerSharedPtr Nektar::Thread::ThreadMaster::CreateInstance ( const ThreadManagerName  t,
unsigned int  nThr 
)

Creates an instance of a ThreadManager (which one is determined by a previous call to SetThreadingType) and associates it with the string s.

Returns
Shared pointer to the created ThreadManager.

An error occurs if this is called before SetThreadingType.

Definition at line 179 of file Thread.cpp.

181{
182 ASSERTL0(!m_threadingType.empty(),
183 "Trying to create a ThreadManager before SetThreadingType called");
184 return m_threadManagers[t] =
186 nThr);
187}
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
ThreadManagerFactory & GetThreadManagerFactory()
Definition: Thread.cpp:45

References ASSERTL0, Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::CreateInstance(), Nektar::Thread::GetThreadManagerFactory(), m_threadingType, and m_threadManagers.

◆ GetInstance()

ThreadManagerSharedPtr & Nektar::Thread::ThreadMaster::GetInstance ( const ThreadManagerName  t)

Gets the ThreadManager associated with string s.

Returns
a shared pointer to /em either a ThreadStartupManager or whatever ThreadManager has been created for the string s with CreateInstance.

Calling code may store the result if it is sure the call to GetInstance(s) has occurred after the call to CreateInstance(s). This cannot be before threadedcommReader::StartThreads(), as that's where SetThreadingType is called.

Warning
Static initialisation may want to access a ThreadManager. Such code must be able to cope with the temporary ThreadStartupManager.

Definition at line 163 of file Thread.cpp.

164{
165 if (!m_threadManagers[t])
166 {
168 ThreadManagerSharedPtr(new ThreadStartupManager());
169 return m_threadManagers[t];
170 }
171 return m_threadManagers[t];
172}
std::shared_ptr< ThreadManager > ThreadManagerSharedPtr
Definition: Thread.h:69

References m_threadManagers.

◆ SetThreadingType()

void Nektar::Thread::ThreadMaster::SetThreadingType ( const std::string &  p_type)

Sets what ThreadManagers will be created in CreateInstance.

Parameters
p_typeString to be passed to the ThreadManagerFactory

Subsequent CreateInstance calls will pass this string to the ThreadManagerFactory to create a ThreadManager.

It is an error to call this more than once (since having different kinds of ThreadManager active is probably a stupendously bad idea).

Definition at line 143 of file Thread.cpp.

144{
146 "Tried to SetThreadingType when it was already set");
147 m_threadingType = p_type;
148}

References ASSERTL0, and m_threadingType.

Member Data Documentation

◆ m_mutex

std::shared_mutex Nektar::Thread::ThreadMaster::m_mutex
private

Definition at line 400 of file Thread.h.

◆ m_threadingType

std::string Nektar::Thread::ThreadMaster::m_threadingType
private

Definition at line 401 of file Thread.h.

Referenced by CreateInstance(), and SetThreadingType().

◆ m_threadManagers

std::vector<ThreadManagerSharedPtr> Nektar::Thread::ThreadMaster::m_threadManagers
private

Definition at line 399 of file Thread.h.

Referenced by CreateInstance(), GetInstance(), and ~ThreadMaster().