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

Implementation class for ThreadManagerBoost. More...

#include <ThreadBoost.h>

Public Member Functions

 ThreadWorkerBoost (ThreadManagerBoost *threadManager, unsigned int workerNum)
 Constructor. More...
 
 ~ThreadWorkerBoost ()
 Destructor. More...
 
void operator() ()
 This provides the interface that boost::thread uses to start the worker. More...
 
unsigned int GetWorkerNum ()
 Return the index of the worker thread. More...
 
void Stop ()
 A signal to shut down. More...
 

Private Member Functions

 ThreadWorkerBoost ()
 
 ThreadWorkerBoost (const ThreadWorkerBoost &)
 
void MainLoop ()
 
void LoadJobs ()
 
unsigned int GetNumToLoad ()
 
void WaitForActive ()
 
void RunJobs ()
 

Private Attributes

ThreadManagerBoostm_threadManager
 
std::queue< ThreadJob * > m_workerQueue
 
bool m_keepgoing
 
unsigned int m_threadNum
 

Detailed Description

Implementation class for ThreadManagerBoost.

Each instance of this class corresponds to a worker thread. Instances manage their own queue of jobs to run, grabbing new jobs from the master queue when it is exhausted.

Definition at line 126 of file ThreadBoost.h.

Constructor & Destructor Documentation

◆ ThreadWorkerBoost() [1/3]

Nektar::Thread::ThreadWorkerBoost::ThreadWorkerBoost ( ThreadManagerBoost tm,
unsigned int  workerNum 
)

Constructor.

Parameters
threadManagerPointer to the ThreadManagerBoost that is controlling this worker.
workerNumUnique number from 0..(number_of_threads - 1)

Called by the ThreadManagerBoost instance.

Definition at line 313 of file ThreadBoost.cpp.

316 m_threadNum(workerNum)
317{
318 // Nothing to see here
319}
std::queue< ThreadJob * > m_workerQueue
Definition: ThreadBoost.h:171
ThreadManagerBoost * m_threadManager
Definition: ThreadBoost.h:170

◆ ~ThreadWorkerBoost()

Nektar::Thread::ThreadWorkerBoost::~ThreadWorkerBoost ( )

Destructor.

Winds up this thread's execution. Jobs in its queue are lost.

Definition at line 324 of file ThreadBoost.cpp.

325{
326 if (m_keepgoing)
327 {
328 std::cerr << "Warning: ThreadWorker: " << m_threadNum
329 << "destroyed while running!" << std::endl;
330 }
331 // on destuction the m_workerQueue will be destructed and that
332 // will destruct any ThreadJobs still in there.
333}

References m_keepgoing, and m_threadNum.

◆ ThreadWorkerBoost() [2/3]

Nektar::Thread::ThreadWorkerBoost::ThreadWorkerBoost ( )
private

◆ ThreadWorkerBoost() [3/3]

Nektar::Thread::ThreadWorkerBoost::ThreadWorkerBoost ( const ThreadWorkerBoost )
private

Member Function Documentation

◆ GetNumToLoad()

unsigned int Nektar::Thread::ThreadWorkerBoost::GetNumToLoad ( )
private

Definition at line 372 of file ThreadBoost.cpp.

373{
374 unsigned int numToLoad = 0;
376 {
377 case e_guided:
378 numToLoad = std::max(
379 static_cast<unsigned long>(m_threadManager->m_chunkSize),
380 static_cast<unsigned long>(
382 (2 * m_threadManager->m_numWorkers + 1)));
383 break;
384
385 case e_dynamic:
386 numToLoad = m_threadManager->m_chunkSize;
387 break;
388
389 default:
390 NEKERROR(ErrorUtil::efatal, "Invalid value for SchedType.");
391 break;
392 }
393 return numToLoad;
394}
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:209
std::queue< ThreadJob * > m_masterQueue
Definition: ThreadBoost.h:101

References Nektar::Thread::e_dynamic, Nektar::Thread::e_guided, Nektar::ErrorUtil::efatal, Nektar::Thread::ThreadManagerBoost::m_chunkSize, Nektar::Thread::ThreadManagerBoost::m_masterQueue, Nektar::Thread::ThreadManagerBoost::m_numWorkers, Nektar::Thread::ThreadManagerBoost::m_schedType, m_threadManager, and NEKERROR.

Referenced by LoadJobs().

◆ GetWorkerNum()

unsigned int Nektar::Thread::ThreadWorkerBoost::GetWorkerNum ( )
inline

Return the index of the worker thread.

Returns
Index of worker thread, an integer between 0 and (number_of_threads - 1)

Definition at line 145 of file ThreadBoost.h.

146 {
147 return m_threadNum;
148 };

References m_threadNum.

◆ LoadJobs()

void Nektar::Thread::ThreadWorkerBoost::LoadJobs ( )
private

Definition at line 338 of file ThreadBoost.cpp.

339{
340 // Lock the master queue
341 Lock masterQueueLock(m_threadManager->m_masterQueueMutex);
344 while (m_threadManager->m_masterQueue.empty() && m_keepgoing)
345 {
346 // while waiting, master queue is unlocked
347 m_threadManager->m_masterQueueCondVar.wait(masterQueueLock);
348 // on exiting wait master queue is locked again
349 }
350 bool active;
351 {
352 Lock masterActiveLock(m_threadManager->m_masterActiveMutex);
354 }
355 if (active && m_keepgoing)
356 {
357 unsigned int numToLoad = GetNumToLoad();
358 while (m_workerQueue.size() < numToLoad &&
360 {
361 ThreadJob *tj = m_threadManager->m_masterQueue.front();
362 m_workerQueue.push(tj);
364 }
365 }
367} // lock on master queue released here
boost::condition_variable m_masterQueueCondVar
Definition: ThreadBoost.h:104
boost::unique_lock< boost::mutex > Lock
Definition: ThreadBoost.h:51

References GetNumToLoad(), m_keepgoing, Nektar::Thread::ThreadManagerBoost::m_masterActiveMutex, Nektar::Thread::ThreadManagerBoost::m_masterQueue, Nektar::Thread::ThreadManagerBoost::m_masterQueueCondVar, Nektar::Thread::ThreadManagerBoost::m_masterQueueMutex, Nektar::Thread::ThreadManagerBoost::m_threadActiveList, Nektar::Thread::ThreadManagerBoost::m_threadBusyList, m_threadManager, m_threadNum, and m_workerQueue.

Referenced by MainLoop().

◆ MainLoop()

void Nektar::Thread::ThreadWorkerBoost::MainLoop ( )
private

Definition at line 414 of file ThreadBoost.cpp.

415{
416 while (m_keepgoing)
417 {
419 LoadJobs();
420 RunJobs();
421 }
422} // exiting here should terminate the thread

References LoadJobs(), m_keepgoing, RunJobs(), and WaitForActive().

Referenced by operator()().

◆ operator()()

void Nektar::Thread::ThreadWorkerBoost::operator() ( )
inline

This provides the interface that boost::thread uses to start the worker.

Definition at line 136 of file ThreadBoost.h.

137 {
138 MainLoop();
139 };

References MainLoop().

◆ RunJobs()

void Nektar::Thread::ThreadWorkerBoost::RunJobs ( )
private

Definition at line 427 of file ThreadBoost.cpp.

428{
429 while (!m_workerQueue.empty() && m_keepgoing)
430 {
431 ThreadJob *tj;
432 try
433 {
434 tj = m_workerQueue.front();
435 tj->SetWorkerNum(m_threadNum);
436 tj->Run();
437 m_workerQueue.pop();
438 delete tj;
439 }
440 catch (...)
441 {
442 // something bad happened, probably time to die
443 // maybe signal ThreadManager
444 throw;
445 }
446 }
447}

References m_keepgoing, m_threadNum, m_workerQueue, Nektar::Thread::ThreadJob::Run(), and Nektar::Thread::ThreadJob::SetWorkerNum().

Referenced by MainLoop().

◆ Stop()

void Nektar::Thread::ThreadWorkerBoost::Stop ( )
inline

A signal to shut down.

If this method is called the worker will shut down. Used by the ThreadManagerBoost to stop threading.

Definition at line 155 of file ThreadBoost.h.

156 {
157 m_keepgoing = false;
158 };

References m_keepgoing.

Referenced by Nektar::Thread::ThreadManagerBoost::~ThreadManagerBoost().

◆ WaitForActive()

void Nektar::Thread::ThreadWorkerBoost::WaitForActive ( )
private

Definition at line 399 of file ThreadBoost.cpp.

400{
401 Lock masterActiveLock(m_threadManager->m_masterActiveMutex);
402
404 {
405 // while waiting, master active is unlocked
406 m_threadManager->m_masterActiveCondVar.wait(masterActiveLock);
407 // on exiting wait master active is locked again
408 }
409}
boost::condition_variable m_masterActiveCondVar
Definition: ThreadBoost.h:105

References m_keepgoing, Nektar::Thread::ThreadManagerBoost::m_masterActiveCondVar, Nektar::Thread::ThreadManagerBoost::m_masterActiveMutex, Nektar::Thread::ThreadManagerBoost::m_threadActiveList, m_threadManager, and m_threadNum.

Referenced by MainLoop().

Member Data Documentation

◆ m_keepgoing

bool Nektar::Thread::ThreadWorkerBoost::m_keepgoing
private

Definition at line 172 of file ThreadBoost.h.

Referenced by LoadJobs(), MainLoop(), RunJobs(), Stop(), WaitForActive(), and ~ThreadWorkerBoost().

◆ m_threadManager

ThreadManagerBoost* Nektar::Thread::ThreadWorkerBoost::m_threadManager
private

Definition at line 170 of file ThreadBoost.h.

Referenced by GetNumToLoad(), LoadJobs(), and WaitForActive().

◆ m_threadNum

unsigned int Nektar::Thread::ThreadWorkerBoost::m_threadNum
private

Definition at line 173 of file ThreadBoost.h.

Referenced by GetWorkerNum(), LoadJobs(), RunJobs(), WaitForActive(), and ~ThreadWorkerBoost().

◆ m_workerQueue

std::queue<ThreadJob *> Nektar::Thread::ThreadWorkerBoost::m_workerQueue
private

Definition at line 171 of file ThreadBoost.h.

Referenced by LoadJobs(), and RunJobs().