CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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::ThreadHandoff ( int  stackSize)
explicit

Definition at line 40 of file ThreadHandoff.cc.

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

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

Definition at line 67 of file ThreadHandoff.cc.

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

67  {
68  if (not m_stopThread) {
69  stopThread();
70  }
71  void* ret;
72  pthread_join(m_thread, &ret);
73 }
tuple ret
prodAgent to be discontinued
omt::ThreadHandoff::ThreadHandoff ( const ThreadHandoff )
delete

Member Function Documentation

const ThreadHandoff& omt::ThreadHandoff::operator= ( const ThreadHandoff )
delete
template<typename F >
void omt::ThreadHandoff::runAndWait ( F &&  iF)
inline

Definition at line 42 of file ThreadHandoff.h.

References alignCSCRings::e, validate-o2o-wbm::f, m_loopReady, m_mutex, m_threadHandoff, m_toRun, and eostools::move().

Referenced by OscarMTProducer::beginRun(), OscarMTProducer::endRun(), OscarMTProducer::OscarMTProducer(), 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
def move
Definition: eostools.py:511
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
void * ThreadHandoff::threadLoop ( void *  iArgs)
staticprivate

Definition at line 98 of file ThreadHandoff.cc.

Referenced by ThreadHandoff().

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

Member Data Documentation

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

Definition at line 94 of file ThreadHandoff.h.

Referenced by runAndWait(), and ThreadHandoff().

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

Definition at line 90 of file ThreadHandoff.h.

Referenced by runAndWait(), and ThreadHandoff().

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

Definition at line 95 of file ThreadHandoff.h.

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

pthread_t omt::ThreadHandoff::m_thread
private

Definition at line 89 of file ThreadHandoff.h.

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

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

Definition at line 91 of file ThreadHandoff.h.

Referenced by runAndWait(), and ThreadHandoff().

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

Definition at line 93 of file ThreadHandoff.h.

Referenced by runAndWait().