CMS 3D CMS Logo

ControllerChannel.h
Go to the documentation of this file.
1 #ifndef FWCore_SharedMemory_ControllerChannel_h
2 #define FWCore_SharedMemory_ControllerChannel_h
3 // -*- C++ -*-
4 //
5 // Package: FWCore/SharedMemory
6 // Class : ControllerChannel
7 //
16 //
17 // Original Author: Chris Jones
18 // Created: 21/01/2020
19 //
20 
21 // system include files
22 #include <string>
23 #include <iostream>
24 #include "boost/interprocess/managed_shared_memory.hpp"
25 #include "boost/interprocess/sync/named_mutex.hpp"
26 #include "boost/interprocess/sync/named_condition.hpp"
27 #include "boost/interprocess/sync/scoped_lock.hpp"
28 
29 // user include files
33 
34 // forward declarations
35 
36 namespace edm::shared_memory {
38  public:
42  ControllerChannel(std::string const& iName, int iID, unsigned int iMaxWaitInSeconds);
44  ControllerChannel(const ControllerChannel&) = delete;
45  const ControllerChannel& operator=(const ControllerChannel&) = delete;
48 
49  // ---------- member functions ---------------------------
50 
55  template <typename F>
56  void setupWorker(F&& iF) {
57  using namespace boost::interprocess;
58  scoped_lock<named_mutex> lock(mutex_);
59  iF();
60  using namespace boost::posix_time;
61  //std::cout << id_ << " waiting for external process" << std::endl;
62 
63  if (not cndToMain_.timed_wait(lock, microsec_clock::universal_time() + seconds(maxWaitInSeconds_))) {
64  //std::cout << id_ << " FAILED waiting for external process" << std::endl;
65  throw cms::Exception("ExternalFailed");
66  } else {
67  //std::cout << id_ << " done waiting for external process" << std::endl;
68  }
69  }
70 
71  template <typename F>
72  bool doTransition(F&& iF, edm::Transition iTrans, unsigned long long iTransitionID) {
73  using namespace boost::interprocess;
74 
75  //std::cout << id_ << " taking from lock" << std::endl;
76  scoped_lock<named_mutex> lock(mutex_);
77 
78  if (not wait(lock, iTrans, iTransitionID)) {
79  return false;
80  }
81  //std::cout <<id_<<"running doTranstion command"<<std::endl;
82  iF();
83  return true;
84  }
85 
90 
91  void stopWorker() {
92  //std::cout <<"stopWorker"<<std::endl;
93  using namespace boost::interprocess;
94  scoped_lock<named_mutex> lock(mutex_);
95  *stop_ = true;
96  //std::cout <<"stopWorker sending notification"<<std::endl;
97  cndFromMain_.notify_all();
98  }
99 
100  // ---------- const member functions ---------------------------
101  std::string const& sharedMemoryName() const { return smName_; }
102  std::string uniqueID() const { return uniqueName(""); }
103 
104  //should only be called after calling `doTransition`
105  bool shouldKeepEvent() const { return *keepEvent_; }
106 
107  private:
108  static BufferInfo* bufferInfo(const char* iWhich, boost::interprocess::managed_shared_memory& mem);
109 
110  std::string uniqueName(std::string iBase) const;
111 
112  bool wait(boost::interprocess::scoped_lock<boost::interprocess::named_mutex>& lock,
113  edm::Transition iTrans,
114  unsigned long long iTransID);
115 
116  // ---------- member data --------------------------------
117  int id_;
118  unsigned int maxWaitInSeconds_;
120  boost::interprocess::managed_shared_memory managed_sm_;
123 
124  boost::interprocess::named_mutex mutex_;
125  boost::interprocess::named_condition cndFromMain_;
126 
127  boost::interprocess::named_condition cndToMain_;
128 
130  unsigned long long* transitionID_;
131  bool* stop_;
132  bool* keepEvent_;
133  };
134 } // namespace edm::shared_memory
135 
136 #endif
edm::shared_memory::ControllerChannel::cndToMain_
boost::interprocess::named_condition cndToMain_
Definition: ControllerChannel.h:127
edm::shared_memory::ControllerChannel::transitionType_
edm::Transition * transitionType_
Definition: ControllerChannel.h:129
edm::shared_memory::ControllerChannel::setupWorker
void setupWorker(F &&iF)
Definition: ControllerChannel.h:56
edm::shared_memory::ControllerChannel::toWorkerBufferInfo
BufferInfo * toWorkerBufferInfo()
This can be used with WriteBuffer to keep Controller and Worker in sync.
Definition: ControllerChannel.h:87
edm::shared_memory::ControllerChannel::keepEvent_
bool * keepEvent_
Definition: ControllerChannel.h:132
edm::shared_memory::ControllerChannel::managed_sm_
boost::interprocess::managed_shared_memory managed_sm_
Definition: ControllerChannel.h:120
BufferInfo.h
edm::shared_memory::ControllerChannel::uniqueName
std::string uniqueName(std::string iBase) const
Definition: ControllerChannel.cc:76
edm::shared_memory::ControllerChannel::uniqueID
std::string uniqueID() const
Definition: ControllerChannel.h:102
mem
uint16_t mem[nChs][nEvts]
Definition: recycleTccEmu.cc:13
edm::shared_memory::ControllerChannel::fromWorkerBufferInfo_
BufferInfo * fromWorkerBufferInfo_
Definition: ControllerChannel.h:122
edm::shared_memory::ControllerChannel::transitionID_
unsigned long long * transitionID_
Definition: ControllerChannel.h:130
edm::shared_memory::ControllerChannel::shouldKeepEvent
bool shouldKeepEvent() const
Definition: ControllerChannel.h:105
F
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
edm::shared_memory::ControllerChannel::smName_
std::string smName_
Definition: ControllerChannel.h:119
seconds
double seconds()
edm::shared_memory::BufferInfo
Definition: BufferInfo.h:27
edm::shared_memory::ControllerChannel::cndFromMain_
boost::interprocess::named_condition cndFromMain_
Definition: ControllerChannel.h:125
edm::shared_memory::ControllerChannel::toWorkerBufferInfo_
BufferInfo * toWorkerBufferInfo_
Definition: ControllerChannel.h:121
edm::shared_memory::ControllerChannel::wait
bool wait(boost::interprocess::scoped_lock< boost::interprocess::named_mutex > &lock, edm::Transition iTrans, unsigned long long iTransID)
Definition: ControllerChannel.cc:85
edm::shared_memory::ControllerChannel::stopWorker
void stopWorker()
Definition: ControllerChannel.h:91
Transition.h
edm::shared_memory::ControllerChannel::bufferInfo
static BufferInfo * bufferInfo(const char *iWhich, boost::interprocess::managed_shared_memory &mem)
Definition: ControllerChannel.cc:107
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::shared_memory::ControllerChannel::id_
int id_
Definition: ControllerChannel.h:117
edm::shared_memory::ControllerChannel::~ControllerChannel
~ControllerChannel()
Definition: ControllerChannel.cc:60
edm::Transition
Transition
Definition: Transition.h:12
edm::shared_memory::ControllerChannel::fromWorkerBufferInfo
BufferInfo * fromWorkerBufferInfo()
This can be used with ReadBuffer to keep Controller and Worker in sync.
Definition: ControllerChannel.h:89
CommonMethods.lock
def lock()
Definition: CommonMethods.py:82
edm::shared_memory::ControllerChannel::ControllerChannel
ControllerChannel(std::string const &iName, int iID, unsigned int iMaxWaitInSeconds)
Definition: ControllerChannel.cc:34
edm::shared_memory
Definition: buffer_names.h:27
edm::shared_memory::ControllerChannel::sharedMemoryName
std::string const & sharedMemoryName() const
Definition: ControllerChannel.h:101
edm::shared_memory::ControllerChannel::stop_
bool * stop_
Definition: ControllerChannel.h:131
edm::shared_memory::ControllerChannel::mutex_
boost::interprocess::named_mutex mutex_
Definition: ControllerChannel.h:124
edm::shared_memory::ControllerChannel::operator=
const ControllerChannel & operator=(const ControllerChannel &)=delete
Exception
Definition: hltDiff.cc:246
edm::shared_memory::ControllerChannel::maxWaitInSeconds_
unsigned int maxWaitInSeconds_
Definition: ControllerChannel.h:118
Exception.h
edm::shared_memory::ControllerChannel
Definition: ControllerChannel.h:37
edm::shared_memory::ControllerChannel::doTransition
bool doTransition(F &&iF, edm::Transition iTrans, unsigned long long iTransitionID)
Definition: ControllerChannel.h:72