Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Types | Public Member Functions | Private Attributes | List of all members
Nektar::Thread::ThreadMaster Class Reference

#include <Thread.h>

Collaboration diagram for Nektar::Thread::ThreadMaster:
Collaboration graph
[legend]

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
< ThreadManagerSharedPtr
m_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 344 of file Thread.h.

Member Enumeration Documentation

Enumerator
SessionJob 
THREADMANAGER_MAX 

Definition at line 352 of file Thread.h.

Constructor & Destructor Documentation

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

Constructor.

ThreadMaster implementation

Definition at line 123 of file Thread.cpp.

125 {
126  // empty
127 }
std::vector< ThreadManagerSharedPtr > m_threadManagers
Definition: Thread.h:347
std::string m_threadingType
Definition: Thread.h:349
boost::shared_mutex m_mutex
Definition: Thread.h:348
Nektar::Thread::ThreadMaster::~ThreadMaster ( )

Destructor.

Will clear the list of ThreadManagers, destructing them.

Definition at line 133 of file Thread.cpp.

References m_threadManagers.

134 {
135  // Locking is a bit pointless, since the map is empty after this call.
136  m_threadManagers.clear();
137 }
std::vector< ThreadManagerSharedPtr > m_threadManagers
Definition: Thread.h:347

Member Function Documentation

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

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

202 {
203  ASSERTL0(!m_threadingType.empty(),
204  "Trying to create a ThreadManager before SetThreadingType called");
205  return m_threadManagers[t] =
207 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:188
tBaseSharedPtr CreateInstance(tKey idKey BOOST_PP_COMMA_IF(MAX_PARAM) BOOST_PP_ENUM_BINARY_PARAMS(MAX_PARAM, tParam, x))
Create an instance of the class referred to by idKey.
Definition: NekFactory.hpp:162
ThreadManagerFactory & GetThreadManagerFactory()
Definition: Thread.cpp:49
std::vector< ThreadManagerSharedPtr > m_threadManagers
Definition: Thread.h:347
std::string m_threadingType
Definition: Thread.h:349
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 183 of file Thread.cpp.

References m_threadManagers.

184 {
185  if ( !m_threadManagers[t] )
186  {
188  new ThreadStartupManager());
189  return m_threadManagers[t];
190  }
191  return m_threadManagers[t];
192 }
std::vector< ThreadManagerSharedPtr > m_threadManagers
Definition: Thread.h:347
boost::shared_ptr< ThreadManager > ThreadManagerSharedPtr
Definition: Thread.h:74
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 162 of file Thread.cpp.

References ASSERTL0, and m_threadingType.

163 {
164  ASSERTL0(m_threadingType.empty(),
165  "Tried to SetThreadingType when it was already set");
166  m_threadingType = p_type;
167 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:188
std::string m_threadingType
Definition: Thread.h:349

Member Data Documentation

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

Definition at line 348 of file Thread.h.

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

Definition at line 349 of file Thread.h.

Referenced by CreateInstance(), and SetThreadingType().

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

Definition at line 347 of file Thread.h.

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