CMS 3D CMS Logo

SonicClientPseudoAsync.h
Go to the documentation of this file.
1 #ifndef HeterogeneousCore_SonicCore_SonicClientPseudoAsync
2 #define HeterogeneousCore_SonicCore_SonicClientPseudoAsync
3 
7 
8 #include <memory>
9 #include <condition_variable>
10 #include <mutex>
11 #include <thread>
12 #include <atomic>
13 #include <exception>
14 
15 //pretend to be async + non-blocking by waiting for blocking calls to return in separate std::thread
16 template <typename InputT, typename OutputT = InputT>
17 class SonicClientPseudoAsync : public SonicClientBase, public SonicClientTypes<InputT, OutputT> {
18 public:
19  //constructor
21  thread_ = std::make_unique<std::thread>([this]() { waitForNext(); });
22  }
23  //destructor
25  stop_ = true;
26  cond_.notify_one();
27  if (thread_) {
28  try {
29  thread_->join();
30  thread_.reset();
31  } catch (...) {
32  }
33  }
34  }
35  //accessor
37  //do all read/writes inside lock to ensure cache synchronization
38  {
39  std::lock_guard<std::mutex> guard(mutex_);
40  holder_ = std::move(holder);
41  setStartTime();
42 
43  //activate thread to wait for response, and return
44  hasCall_ = true;
45  }
46  cond_.notify_one();
47  }
48 
49 private:
50  void waitForNext() {
51  while (true) {
52  //wait for condition
53  {
54  std::unique_lock<std::mutex> lk(mutex_);
55  cond_.wait(lk, [this]() { return (hasCall_ or stop_); });
56  if (stop_)
57  break;
58 
59  //do everything inside lock
60  evaluate();
61 
62  //reset condition
63  hasCall_ = false;
64  }
65  }
66  }
67 
68  //members
69  bool hasCall_;
71  std::condition_variable cond_;
72  std::atomic<bool> stop_;
73  std::unique_ptr<std::thread> thread_;
74 };
75 
76 #endif
funct::false
false
Definition: Factorize.h:34
SonicClientTypes.h
SonicClientBase
Definition: SonicClientBase.h:11
SonicClientPseudoAsync::SonicClientPseudoAsync
SonicClientPseudoAsync()
Definition: SonicClientPseudoAsync.h:20
SonicClientTypes
Definition: SonicClientTypes.h:6
edm::WaitingTaskWithArenaHolder
Definition: WaitingTaskWithArenaHolder.h:31
SonicClientBase::holder_
edm::WaitingTaskWithArenaHolder holder_
Definition: SonicClientBase.h:65
SonicClientPseudoAsync::cond_
std::condition_variable cond_
Definition: SonicClientPseudoAsync.h:71
SonicClientPseudoAsync
Definition: SonicClientPseudoAsync.h:17
SonicClientBase::evaluate
virtual void evaluate()=0
mutex
static boost::mutex mutex
Definition: Proxy.cc:9
SonicClientBase::setStartTime
void setStartTime()
Definition: SonicClientBase.h:31
WaitingTaskWithArenaHolder.h
SonicClientPseudoAsync::thread_
std::unique_ptr< std::thread > thread_
Definition: SonicClientPseudoAsync.h:73
SonicClientBase.h
SonicClientPseudoAsync::mutex_
std::mutex mutex_
Definition: SonicClientPseudoAsync.h:70
SonicClientPseudoAsync::hasCall_
bool hasCall_
Definition: SonicClientPseudoAsync.h:69
eostools.move
def move(src, dest)
Definition: eostools.py:511
SonicClientPseudoAsync::waitForNext
void waitForNext()
Definition: SonicClientPseudoAsync.h:50
SonicClientPseudoAsync::stop_
std::atomic< bool > stop_
Definition: SonicClientPseudoAsync.h:72
SonicClientPseudoAsync::~SonicClientPseudoAsync
~SonicClientPseudoAsync() override
Definition: SonicClientPseudoAsync.h:24
or
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
SonicClientPseudoAsync::dispatch
void dispatch(edm::WaitingTaskWithArenaHolder holder) final
Definition: SonicClientPseudoAsync.h:36