Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:15:47

0001 #include "HeterogeneousCore/SonicCore/interface/SonicDispatcherPseudoAsync.h"
0002 #include "HeterogeneousCore/SonicCore/interface/SonicClientBase.h"
0003 #include "FWCore/Utilities/interface/thread_safety_macros.h"
0004 
0005 SonicDispatcherPseudoAsync::SonicDispatcherPseudoAsync(SonicClientBase* client)
0006     : SonicDispatcher(client), hasCall_(false), stop_(false) {
0007   thread_ = std::make_unique<std::thread>([this]() { waitForNext(); });
0008 }
0009 
0010 SonicDispatcherPseudoAsync::~SonicDispatcherPseudoAsync() {
0011   stop_ = true;
0012   cond_.notify_one();
0013   if (thread_) {
0014     // avoid throwing in destructor
0015     CMS_SA_ALLOW try {
0016       thread_->join();
0017       thread_.reset();
0018     } catch (...) {
0019     }
0020   }
0021 }
0022 
0023 void SonicDispatcherPseudoAsync::dispatch(edm::WaitingTaskWithArenaHolder holder) {
0024   //do all read/writes inside lock to ensure cache synchronization
0025   {
0026     std::lock_guard<std::mutex> guard(mutex_);
0027     client_->start(std::move(holder));
0028 
0029     //activate thread to wait for response, and return
0030     hasCall_ = true;
0031   }
0032   cond_.notify_one();
0033 }
0034 
0035 void SonicDispatcherPseudoAsync::waitForNext() {
0036   while (true) {
0037     //wait for condition
0038     {
0039       std::unique_lock<std::mutex> lk(mutex_);
0040       cond_.wait(lk, [this]() { return (hasCall_ or stop_); });
0041       if (stop_)
0042         break;
0043 
0044       //do everything inside lock
0045       client_->evaluate();
0046 
0047       //reset condition
0048       hasCall_ = false;
0049     }
0050   }
0051 }