Hedgehog  0.0.0
A library to generate hybrid pipeline workflow systems
core_queue_slot.h
1 // NIST-developed software is provided by NIST as a public service. You may use, copy and distribute copies of the
2 // software in any medium, provided that you keep intact this entire notice. You may improve, modify and create
3 // derivative works of the software or any portion of the software, and you may copy and distribute such modifications
4 // or works. Modified works should carry a notice stating that you changed the software and should note the date and
5 // nature of any such change. Please explicitly acknowledge the National Institute of Standards and Technology as the
6 // source of the software. NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND,
7 // EXPRESS, IMPLIED, IN FACT OR ARISING BY OPERATION OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
8 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT AND DATA ACCURACY. NIST NEITHER REPRESENTS NOR
9 // WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE
10 // CORRECTED. NIST DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR THE RESULTS
11 // THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, RELIABILITY, OR USEFULNESS OF THE SOFTWARE. You
12 // are solely responsible for determining the appropriateness of using and distributing the software and you assume
13 // all risks associated with its use, including but not limited to the risks and costs of program errors, compliance
14 // with applicable laws, damage to or loss of data, programs or equipment, and the unavailability or interruption of
15 // operation. This software is not intended to be used in any situation where a failure could cause risk of injury or
16 // damage to property. The software developed by NIST employees is not subject to copyright protection within the
17 // United States.
18 
19 
20 #ifndef HEDGEHOG_CORE_QUEUE_SLOT_H
21 #define HEDGEHOG_CORE_QUEUE_SLOT_H
22 #include <mutex>
23 #include <condition_variable>
24 #include <memory>
25 #include <set>
26 #include <cassert>
27 
28 #include "../../base/receiver/core_slot.h"
29 
31 namespace hh::core {
32 
34 class CoreQueueSlot : public virtual CoreSlot {
35  private:
36  std::shared_ptr<std::mutex> slotMutex_ = nullptr;
37  std::shared_ptr<std::condition_variable> notifyConditionVariable_ = nullptr;
38  std::shared_ptr<std::set<CoreNotifier *>> notifiers_ = nullptr;
40 
41  public:
42 
47  CoreQueueSlot(std::string_view const &name, NodeType const type, size_t const numberThreads) :
48  CoreSlot(name, type, numberThreads) {
49  HLOG_SELF(0, "Creating CoreQueueSlot with type: " << (int) type << " and name: " << name)
50  notifiers_ = std::make_shared<std::set<CoreNotifier *>>();
51  slotMutex_ = std::make_shared<std::mutex>();
52  notifyConditionVariable_ = std::make_shared<std::condition_variable>();
53  }
54 
56  ~CoreQueueSlot() override {HLOG_SELF(0, "Destructing CoreQueueSlot")}
57 
60  [[nodiscard]] std::shared_ptr<std::condition_variable> const &notifyConditionVariable() const {
62  }
63 
66  [[nodiscard]] std::shared_ptr<std::mutex> const &slotMutex() const { return slotMutex_; }
67 
71  [[nodiscard]] size_t numberInputNodes() const final { return this->notifiers()->size(); }
72 
76  void addNotifier(CoreNotifier *notifier) final {
77  std::lock_guard<std::mutex> lc(*(this->slotMutex_));
78  this->notifiers()->insert(notifier);
79  }
80 
84  void removeNotifier(CoreNotifier *notifier) final {
85  std::lock_guard<std::mutex> lc(*(this->slotMutex_));
86  this->notifiers()->erase(notifier);
87  }
88 
92  bool hasNotifierConnected() final {
93  HLOG_SELF(2,
94  "Test has notifier connected " << "(" << std::boolalpha << (bool) (this->numberInputNodes() != 0) << ")")
95  return this->numberInputNodes() != 0;
96  }
97 
99  void wakeUp() final {
100  HLOG_SELF(2, "Wake up and notify one")
101  this->notifyConditionVariable()->notify_one();
102  }
103 
106  HLOG_SELF(2, "Lock unique mutex " << this->slotMutex_.get())
107  slotMutex_->lock();
108  }
109 
112  HLOG_SELF(2, "Unlock unique mutex " << this->slotMutex_.get())
113  slotMutex_->unlock();
114  }
115 
119  HLOG_SELF(0, "Copy Cluster CoreQueueSlot information from " << rhs->name() << "(" << rhs->id() << ")")
120  this->slotMutex_ = rhs->slotMutex_;
121  this->notifyConditionVariable_ = rhs->notifyConditionVariable_;
122  this->notifiers_ = rhs->notifiers_;
123  }
124 
125  protected:
128  [[nodiscard]] std::shared_ptr<std::set<CoreNotifier *>> const &notifiers() const { return notifiers_; }
129 };
130 
131 }
132 #endif //HEDGEHOG_CORE_QUEUE_SLOT_H
NodeType type() const
Node type accessor.
Definition: core_node.h:132
void addNotifier(CoreNotifier *notifier) final
Add a notifier to set of CoreNotifier.
CoreQueueSlot(std::string_view const &name, NodeType const type, size_t const numberThreads)
CoreQueueSlot constructor.
bool hasNotifierConnected() final
Test if CoreNotifier are linked to this CoreQueueSlot.
Core Notifier interface, emit notification to CoreSlot.
Definition: core_notifier.h:34
std::shared_ptr< std::mutex > const & slotMutex() const
Mutex accessor.
Slot of CoreQueueMultiReceiver, receiving from CoreQueueNotifier.
std::shared_ptr< std::set< CoreNotifier * > > const & notifiers() const
Protected accessor to the set of notifiers connected to the CoreQueueSlot.
std::shared_ptr< std::condition_variable > notifyConditionVariable_
Condition Variable linked to the CoreQueueSlot::slotMutex_.
void copyInnerStructure(CoreQueueSlot *rhs)
Copy the inner structure of the receiver (mutex, condition variable and set of notifiers) ...
std::shared_ptr< std::mutex > slotMutex_
Mutex locking the CoreQueueMultiReceiver.
Slot interface, receive notification from CoreNotifier.
Definition: core_slot.h:34
NodeType
Hedgehog node&#39;s type.
Definition: core_node.h:40
void removeNotifier(CoreNotifier *notifier) final
Remove a notifier from set of CoreNotifier.
size_t numberInputNodes() const final
Number of CoreNotifier linked accessor.
void unlockUniqueMutex()
Unlock the mutex.
std::shared_ptr< std::condition_variable > const & notifyConditionVariable() const
Condition variable accessor.
Hedgehog core namespace.
Definition: core_execute.h:25
~CoreQueueSlot() override
CoreQueueSlot destructor.
std::string_view const & name() const
Node name accessor.
Definition: core_node.h:128
void wakeUp() final
Wake up and notify a node connected to the condition variable CoreQueueSlot::notifyConditionVariable_...
void lockUniqueMutex()
Lock the mutex.
std::shared_ptr< std::set< CoreNotifier * > > notifiers_
Set of notifiers linked to this CoreQueueSlot.
size_t numberThreads() const
Number of threads associated accessor.
Definition: core_node.h:152
virtual std::string id() const
Unique Id accessor.
Definition: core_node.h:114