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
 
boost::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 342 of file Thread.h.

Member Enumeration Documentation

◆ ThreadManagerName

Enumerator
SessionJob 
THREADMANAGER_MAX 

Definition at line 350 of file Thread.h.

Constructor & Destructor Documentation

◆ ThreadMaster()

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

Constructor.

ThreadMaster implementation

Definition at line 121 of file Thread.cpp.

123 {
124  // empty
125 }
std::vector< ThreadManagerSharedPtr > m_threadManagers
Definition: Thread.h:345
boost::shared_mutex m_mutex
Definition: Thread.h:346
std::string m_threadingType
Definition: Thread.h:347

◆ ~ThreadMaster()

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

Destructor.

Will clear the list of ThreadManagers, destructing them.

Definition at line 131 of file Thread.cpp.

132 {
133  // Locking is a bit pointless, since the map is empty after this call.
134  m_threadManagers.clear();
135 }

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 195 of file Thread.cpp.

197 {
198  ASSERTL0(!m_threadingType.empty(),
199  "Trying to create a ThreadManager before SetThreadingType called");
200  return m_threadManagers[t] =
202 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
Definition: NekFactory.hpp:145
ThreadManagerFactory & GetThreadManagerFactory()
Definition: Thread.cpp:50

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 178 of file Thread.cpp.

179 {
180  if ( !m_threadManagers[t] )
181  {
183  new ThreadStartupManager());
184  return m_threadManagers[t];
185  }
186  return m_threadManagers[t];
187 }
std::shared_ptr< ThreadManager > ThreadManagerSharedPtr
Definition: Thread.h:72

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 157 of file Thread.cpp.

158 {
159  ASSERTL0(m_threadingType.empty(),
160  "Tried to SetThreadingType when it was already set");
161  m_threadingType = p_type;
162 }

References ASSERTL0, and m_threadingType.

Member Data Documentation

◆ m_mutex

boost::shared_mutex Nektar::Thread::ThreadMaster::m_mutex
private

Definition at line 346 of file Thread.h.

◆ m_threadingType

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

Definition at line 347 of file Thread.h.

Referenced by CreateInstance(), and SetThreadingType().

◆ m_threadManagers

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

Definition at line 345 of file Thread.h.

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