CMS 3D CMS Logo

List of all members | Classes | Public Member Functions | Static Private Member Functions | Private Attributes
omt::ThreadHandoff Class Reference

#include <ThreadHandoff.h>

Classes

class  Functor
 
class  FunctorBase
 

Public Member Functions

const ThreadHandoffoperator= (const ThreadHandoff &)=delete
 
template<typename F >
void runAndWait (F &&iF)
 
void stopThread ()
 
 ThreadHandoff (int stackSize)
 
 ThreadHandoff (const ThreadHandoff &)=delete
 
 ~ThreadHandoff ()
 

Static Private Member Functions

static void * threadLoop (void *iArgs)
 

Private Attributes

bool m_loopReady {false}
 
std::mutex m_mutex
 
bool m_stopThread {false}
 
pthread_t m_thread
 
std::condition_variable m_threadHandoff
 
FunctorBasem_toRun {nullptr}
 

Detailed Description

Definition at line 33 of file ThreadHandoff.h.

Constructor & Destructor Documentation

◆ ThreadHandoff() [1/2]

ThreadHandoff::ThreadHandoff ( int  stackSize)
explicit

Definition at line 41 of file ThreadHandoff.cc.

References Exception, m_loopReady, m_mutex, m_thread, m_threadHandoff, and threadLoop().

41  {
42  pthread_attr_t attr;
43  int erno;
44  if (0 != (erno = pthread_attr_init(&attr))) {
45  throw cms::Exception("ThreadInitFailed")
46  << "Failed to initialize thread attributes (" << erno << ") " << errorMessage(erno);
47  }
48 
49  if (0 != (erno = pthread_attr_setstacksize(&attr, stackSize))) {
50  throw cms::Exception("ThreadStackSizeFailed")
51  << "Failed to set stack size " << stackSize << " " << errorMessage(erno);
52  }
53  std::unique_lock<std::mutex> lk(m_mutex);
54 
55  erno = pthread_create(&m_thread, &attr, threadLoop, this);
56  if (0 != erno) {
57  throw cms::Exception("ThreadCreateFailed") << " failed to create a pthread (" << erno << ") " << errorMessage(erno);
58  }
59  m_loopReady = false;
60  m_threadHandoff.wait(lk, [this]() { return m_loopReady; });
61 }
std::condition_variable m_threadHandoff
Definition: ThreadHandoff.h:91
std::mutex m_mutex
Definition: ThreadHandoff.h:90
static void * threadLoop(void *iArgs)

◆ ~ThreadHandoff()

ThreadHandoff::~ThreadHandoff ( )

Definition at line 68 of file ThreadHandoff.cc.

References m_stopThread, m_thread, runTheMatrix::ret, and stopThread().

68  {
69  if (not m_stopThread) {
70  stopThread();
71  }
72  void* ret;
73  pthread_join(m_thread, &ret);
74 }
ret
prodAgent to be discontinued

◆ ThreadHandoff() [2/2]

omt::ThreadHandoff::ThreadHandoff ( const ThreadHandoff )
delete

Member Function Documentation

◆ operator=()

const ThreadHandoff& omt::ThreadHandoff::operator= ( const ThreadHandoff )
delete

◆ runAndWait()

template<typename F >
void omt::ThreadHandoff::runAndWait ( F &&  iF)
inline

Definition at line 42 of file ThreadHandoff.h.

References MillePedeFileConverter_cfg::e, f, m_loopReady, m_mutex, m_threadHandoff, m_toRun, and eostools::move().

Referenced by OscarMTProducer::beginRun(), OscarMTProducer::endRun(), OscarMTProducer::produce(), stopThread(), and OscarMTProducer::~OscarMTProducer().

42  {
43  Functor<F> f{std::move(iF)};
44 
45  std::unique_lock<std::mutex> lck(m_mutex);
46  m_loopReady = false;
47  m_toRun = &f;
48 
49  m_threadHandoff.notify_one();
50 
51  m_threadHandoff.wait(lck, [this]() { return m_loopReady; });
52  auto e = f.exception();
53  if (e) {
54  std::rethrow_exception(e);
55  }
56  }
FunctorBase * m_toRun
Definition: ThreadHandoff.h:93
std::condition_variable m_threadHandoff
Definition: ThreadHandoff.h:91
std::mutex m_mutex
Definition: ThreadHandoff.h:90
double f[11][100]
def move(src, dest)
Definition: eostools.py:511

◆ stopThread()

void omt::ThreadHandoff::stopThread ( )
inline

Definition at line 58 of file ThreadHandoff.h.

References m_stopThread, and runAndWait().

Referenced by ~ThreadHandoff().

58  {
59  runAndWait([this]() { m_stopThread = true; });
60  }
void runAndWait(F &&iF)
Definition: ThreadHandoff.h:42

◆ threadLoop()

void * ThreadHandoff::threadLoop ( void *  iArgs)
staticprivate

Definition at line 99 of file ThreadHandoff.cc.

Referenced by ThreadHandoff().

99  {
100  auto theThis = reinterpret_cast<ThreadHandoff*>(iArgs);
101 
102  //need to hold lock until wait to avoid both threads
103  // being stuck in wait
104  std::unique_lock<std::mutex> lck(theThis->m_mutex);
105  theThis->m_loopReady = true;
106  theThis->m_threadHandoff.notify_one();
107 
108  do {
109  theThis->m_toRun = nullptr;
110  theThis->m_threadHandoff.wait(lck, [theThis]() { return nullptr != theThis->m_toRun; });
111  theThis->m_toRun->execute();
112  theThis->m_loopReady = true;
113  theThis->m_threadHandoff.notify_one();
114  } while (not theThis->m_stopThread);
115  theThis->m_loopReady = true;
116  return nullptr;
117 }

Member Data Documentation

◆ m_loopReady

bool omt::ThreadHandoff::m_loopReady {false}
private

Definition at line 94 of file ThreadHandoff.h.

Referenced by runAndWait(), and ThreadHandoff().

◆ m_mutex

std::mutex omt::ThreadHandoff::m_mutex
private

Definition at line 90 of file ThreadHandoff.h.

Referenced by runAndWait(), and ThreadHandoff().

◆ m_stopThread

bool omt::ThreadHandoff::m_stopThread {false}
private

Definition at line 95 of file ThreadHandoff.h.

Referenced by stopThread(), and ~ThreadHandoff().

◆ m_thread

pthread_t omt::ThreadHandoff::m_thread
private

Definition at line 89 of file ThreadHandoff.h.

Referenced by ThreadHandoff(), and ~ThreadHandoff().

◆ m_threadHandoff

std::condition_variable omt::ThreadHandoff::m_threadHandoff
private

Definition at line 91 of file ThreadHandoff.h.

Referenced by runAndWait(), and ThreadHandoff().

◆ m_toRun

FunctorBase* omt::ThreadHandoff::m_toRun {nullptr}
private

Definition at line 93 of file ThreadHandoff.h.

Referenced by runAndWait().