CMS 3D CMS Logo

List of all members | Public Member Functions | Private Member Functions | Private Attributes
edm::impl::WaitingThread Class Reference

#include <WaitingThreadPool.h>

Public Member Functions

WaitingThreadoperator= (WaitingThread &&)=delete
 
WaitingThreadoperator= (WaitingThread const &)=delete
 
template<typename F , typename G >
void run (WaitingTaskWithArenaHolder holder, F &&func, G &&errorContextFunc, std::shared_ptr< WaitingThread > thisPtr)
 
 WaitingThread ()
 
 WaitingThread (WaitingThread const &)=delete
 
 WaitingThread (WaitingThread &&)=delete
 
 ~WaitingThread () noexcept
 

Private Member Functions

void stopThread ()
 
void threadLoop () noexcept
 

Private Attributes

std::condition_variable cond_
 
std::function< void()> func_
 
std::mutex mutex_
 
bool stopThread_ = false
 
std::shared_ptr< WaitingThreadthisPtr_
 
std::thread thread_
 

Detailed Description

Definition at line 14 of file WaitingThreadPool.h.

Constructor & Destructor Documentation

◆ WaitingThread() [1/3]

edm::impl::WaitingThread::WaitingThread ( )

Definition at line 9 of file WaitingThreadPool.cc.

References cms::cuda::assert(), ALPAKA_ACCELERATOR_NAMESPACE::brokenline::constexpr(), submitPVResolutionJobs::err, thread_, and threadLoop().

9  {
10  thread_ = std::thread(&WaitingThread::threadLoop, this);
11  static constexpr auto poolName = "edm async pool";
12  // pthread_setname_np() string length is limited to 16 characters,
13  // including the null termination
14  static_assert(std::string_view(poolName).size() < 16);
15 
16  int err = pthread_setname_np(thread_.native_handle(), poolName);
17  // According to the glibc documentation, the only error
18  // pthread_setname_np() can return is about the argument C-string
19  // being too long. We already check above the C-string is shorter
20  // than the limit was at the time of writing. In order to capture
21  // if the limit shortens, or other error conditions get added,
22  // let's assert() anyway (exception feels overkill)
23  assert(err == 0);
24  }
size
Write out results.
assert(be >=bs)

◆ ~WaitingThread()

edm::impl::WaitingThread::~WaitingThread ( )
noexcept

Definition at line 26 of file WaitingThreadPool.cc.

References CMS_SA_ALLOW, stopThread(), and thread_.

26  {
27  // When we are shutting down, we don't care about any possible
28  // system errors anymore
29  CMS_SA_ALLOW try {
30  stopThread();
31  thread_.join();
32  } catch (...) {
33  }
34  }
#define CMS_SA_ALLOW

◆ WaitingThread() [2/3]

edm::impl::WaitingThread::WaitingThread ( WaitingThread const &  )
delete

◆ WaitingThread() [3/3]

edm::impl::WaitingThread::WaitingThread ( WaitingThread &&  )
delete

Member Function Documentation

◆ operator=() [1/2]

WaitingThread& edm::impl::WaitingThread::operator= ( WaitingThread &&  )
delete

◆ operator=() [2/2]

WaitingThread& edm::impl::WaitingThread::operator= ( WaitingThread const &  )
delete

◆ run()

template<typename F , typename G >
void edm::impl::WaitingThread::run ( WaitingTaskWithArenaHolder  holder,
F &&  func,
G &&  errorContextFunc,
std::shared_ptr< WaitingThread thisPtr 
)
inline

Definition at line 25 of file WaitingThreadPool.h.

References cond_, MillePedeFileConverter_cfg::e, EcalMonitorTask_cff::func, func_, eostools::move(), mutex_, edm::WaitingTaskWithArenaHolder::presetTaskAsFailed(), thisPtr_, and edm::convertException::wrap().

Referenced by Types.EventID::cppID(), and Types.LuminosityBlockID::cppID().

28  {
29  std::unique_lock lk(mutex_);
30  func_ = [holder = std::move(holder),
31  func = std::forward<F>(func),
32  errorContext = std::forward<G>(errorContextFunc)]() mutable {
33  try {
34  convertException::wrap([&func]() { func(); });
35  } catch (cms::Exception& e) {
36  e.addContext(errorContext());
37  // doneWaiting() is intentionally not called here. The
38  // reference count decrement must be done only in
39  // threadLoop() (see the comment there)
40  holder.presetTaskAsFailed(std::current_exception());
41  }
42  };
43  thisPtr_ = std::move(thisPtr);
44  cond_.notify_one();
45  }
std::shared_ptr< WaitingThread > thisPtr_
auto wrap(F iFunc) -> decltype(iFunc())
std::condition_variable cond_
std::function< void()> func_
def move(src, dest)
Definition: eostools.py:511

◆ stopThread()

void edm::impl::WaitingThread::stopThread ( )
inlineprivate

Definition at line 48 of file WaitingThreadPool.h.

References cond_, mutex_, and stopThread_.

Referenced by ~WaitingThread().

48  {
49  std::unique_lock lk(mutex_);
50  stopThread_ = true;
51  cond_.notify_one();
52  }
std::condition_variable cond_

◆ threadLoop()

void edm::impl::WaitingThread::threadLoop ( )
privatenoexcept

Definition at line 36 of file WaitingThreadPool.cc.

References cms::cuda::assert(), cond_, func_, mutex_, or, stopThread_, edm::swap(), and thisPtr_.

Referenced by WaitingThread().

36  {
37  std::unique_lock lk(mutex_);
38 
39  while (true) {
40  cond_.wait(lk, [this]() { return static_cast<bool>(func_) or stopThread_; });
41  if (stopThread_) {
42  // There should be no way to stop the thread when it as the
43  // func_ assigned, but let's make sure
44  assert(not thisPtr_);
45  break;
46  }
47  func_();
48  // Must return this WaitingThread to the ReusableObjectHolder in
49  // the WaitingThreadPool before resettting func_ (that holds the
50  // WaitingTaskWithArenaHolder, that enables the progress in the
51  // TBB thread pool) in order to meet the requirement of
52  // ReusableObjectHolder destructor that there are no outstanding
53  // objects.
54  thisPtr_.reset();
55  decltype(func_)().swap(func_);
56  }
57  }
assert(be >=bs)
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:112
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
std::shared_ptr< WaitingThread > thisPtr_
std::condition_variable cond_
std::function< void()> func_

Member Data Documentation

◆ cond_

std::condition_variable edm::impl::WaitingThread::cond_
private

Definition at line 58 of file WaitingThreadPool.h.

Referenced by run(), stopThread(), and threadLoop().

◆ func_

std::function<void()> edm::impl::WaitingThread::func_
private

Definition at line 59 of file WaitingThreadPool.h.

Referenced by run(), and threadLoop().

◆ mutex_

std::mutex edm::impl::WaitingThread::mutex_
private

Definition at line 57 of file WaitingThreadPool.h.

Referenced by run(), stopThread(), and threadLoop().

◆ stopThread_

bool edm::impl::WaitingThread::stopThread_ = false
private

Definition at line 63 of file WaitingThreadPool.h.

Referenced by stopThread(), and threadLoop().

◆ thisPtr_

std::shared_ptr<WaitingThread> edm::impl::WaitingThread::thisPtr_
private

Definition at line 62 of file WaitingThreadPool.h.

Referenced by run(), and threadLoop().

◆ thread_

std::thread edm::impl::WaitingThread::thread_
private

Definition at line 56 of file WaitingThreadPool.h.

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