CMS 3D CMS Logo

WaitingThreadPool.cc
Go to the documentation of this file.
2 
3 #include <cassert>
4 #include <string_view>
5 
6 #include <pthread.h>
7 
8 namespace edm::impl {
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  }
25 
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  }
35 
36  void WaitingThread::threadLoop() noexcept {
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  }
58 } // namespace edm::impl
#define CMS_SA_ALLOW
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_