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
BufferInfo * toWorkerBufferInfo()
This can be used with WriteBuffer to keep Controller and Worker in sync.
boost::interprocess::named_condition cndToMain_
double seconds()
BufferInfo * fromWorkerBufferInfo()
This can be used with ReadBuffer to keep Controller and Worker in sync.
boost::interprocess::named_mutex mutex_
bool wait(boost::interprocess::scoped_lock< boost::interprocess::named_mutex > &lock, edm::Transition iTrans, unsigned long long iTransID)
std::string const & sharedMemoryName() const
Transition
Definition: Transition.h:12
uint16_t mem[nChs][nEvts]
const ControllerChannel & operator=(const ControllerChannel &)=delete
boost::interprocess::named_condition cndFromMain_
ControllerChannel(std::string const &iName, int iID, unsigned int iMaxWaitInSeconds)
boost::interprocess::managed_shared_memory managed_sm_
std::string uniqueName(std::string iBase) const
static BufferInfo * bufferInfo(const char *iWhich, boost::interprocess::managed_shared_memory &mem)
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:281
bool doTransition(F &&iF, edm::Transition iTrans, unsigned long long iTransitionID)