iceoryx_doc  1.0.1
wait_set.hpp
1 // Copyright (c) 2020 - 2021 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2020 - 2021 by Apex.AI Inc. All rights reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // SPDX-License-Identifier: Apache-2.0
17 #ifndef IOX_POSH_POPO_WAIT_SET_HPP
18 #define IOX_POSH_POPO_WAIT_SET_HPP
19 
20 #include "iceoryx_posh/iceoryx_posh_types.hpp"
21 #include "iceoryx_posh/internal/popo/building_blocks/condition_listener.hpp"
22 #include "iceoryx_posh/internal/popo/building_blocks/condition_variable_data.hpp"
23 #include "iceoryx_posh/popo/enum_trigger_type.hpp"
24 #include "iceoryx_posh/popo/notification_attorney.hpp"
25 #include "iceoryx_posh/popo/notification_callback.hpp"
26 #include "iceoryx_posh/popo/notification_info.hpp"
27 #include "iceoryx_posh/popo/trigger.hpp"
28 #include "iceoryx_posh/popo/trigger_handle.hpp"
29 #include "iceoryx_posh/runtime/posh_runtime.hpp"
30 #include "iceoryx_utils/cxx/algorithm.hpp"
31 #include "iceoryx_utils/cxx/function_ref.hpp"
32 #include "iceoryx_utils/cxx/helplets.hpp"
33 #include "iceoryx_utils/cxx/list.hpp"
34 #include "iceoryx_utils/cxx/method_callback.hpp"
35 #include "iceoryx_utils/cxx/stack.hpp"
36 #include "iceoryx_utils/cxx/vector.hpp"
37 
38 #include <typeinfo>
39 
40 namespace iox
41 {
42 namespace popo
43 {
44 class Condition;
45 
46 enum class WaitSetError : uint8_t
47 {
48  INVALID_STATE,
49  WAIT_SET_FULL,
50  ALREADY_ATTACHED,
51 };
52 
53 
60 template <uint64_t Capacity = MAX_NUMBER_OF_ATTACHMENTS_PER_WAITSET>
61 class WaitSet
62 {
63  public:
64  static constexpr uint64_t CAPACITY = Capacity;
65  using TriggerArray = cxx::optional<Trigger>[Capacity];
66  using NotificationInfoVector = cxx::vector<const NotificationInfo*, CAPACITY>;
67 
68  WaitSet() noexcept;
69  ~WaitSet() noexcept;
70 
73  WaitSet(const WaitSet& rhs) = delete;
74  WaitSet(WaitSet&& rhs) = delete;
75  WaitSet& operator=(const WaitSet& rhs) = delete;
76  WaitSet& operator=(WaitSet&& rhs) = delete;
77 
82  void markForDestruction() noexcept;
83 
91  template <typename T,
92  typename EventType,
93  typename ContextDataType = internal::NoType_t,
94  typename = std::enable_if_t<std::is_enum<EventType>::value>>
95  cxx::expected<WaitSetError>
96  attachEvent(T& eventOrigin,
97  const EventType eventType,
98  const uint64_t notificationId = 0U,
99  const NotificationCallback<T, ContextDataType>& eventCallback = {}) noexcept;
100 
107  template <typename T,
108  typename EventType,
109  typename ContextDataType = internal::NoType_t,
110  typename = std::enable_if_t<std::is_enum<EventType>::value, void>>
111  cxx::expected<WaitSetError> attachEvent(T& eventOrigin,
112  const EventType eventType,
113  const NotificationCallback<T, ContextDataType>& eventCallback) noexcept;
114 
121  template <typename T, typename ContextDataType = internal::NoType_t>
122  cxx::expected<WaitSetError>
123  attachEvent(T& eventOrigin,
124  const uint64_t notificationId = 0U,
125  const NotificationCallback<T, ContextDataType>& eventCallback = {}) noexcept;
126 
132  template <typename T, typename ContextDataType = internal::NoType_t>
133  cxx::expected<WaitSetError> attachEvent(T& eventOrigin,
134  const NotificationCallback<T, ContextDataType>& eventCallback) noexcept;
135 
143  template <typename T,
144  typename StateType,
145  typename ContextDataType = internal::NoType_t,
146  typename = std::enable_if_t<std::is_enum<StateType>::value>>
147  cxx::expected<WaitSetError>
148  attachState(T& stateOrigin,
149  const StateType stateType,
150  const uint64_t id = 0U,
151  const NotificationCallback<T, ContextDataType>& stateCallback = {}) noexcept;
152 
159  template <typename T,
160  typename StateType,
161  typename ContextDataType = internal::NoType_t,
162  typename = std::enable_if_t<std::is_enum<StateType>::value, void>>
163  cxx::expected<WaitSetError> attachState(T& stateOrigin,
164  const StateType stateType,
165  const NotificationCallback<T, ContextDataType>& stateCallback) noexcept;
166 
173  template <typename T, typename ContextDataType = internal::NoType_t>
174  cxx::expected<WaitSetError>
175  attachState(T& stateOrigin,
176  const uint64_t id = 0U,
177  const NotificationCallback<T, ContextDataType>& stateCallback = {}) noexcept;
178 
184  template <typename T, typename ContextDataType = internal::NoType_t>
185  cxx::expected<WaitSetError> attachState(T& stateOrigin,
186  const NotificationCallback<T, ContextDataType>& stateCallback) noexcept;
187 
191  template <typename T, typename... Targs>
192  void detachEvent(T& eventOrigin, const Targs&... args) noexcept;
193 
197  template <typename T, typename... Targs>
198  void detachState(T& stateOrigin, const Targs&... args) noexcept;
199 
203  NotificationInfoVector timedWait(const units::Duration timeout) noexcept;
204 
207  NotificationInfoVector wait() noexcept;
208 
210  uint64_t size() const noexcept;
211 
213  static constexpr uint64_t capacity() noexcept;
214 
215  protected:
216  explicit WaitSet(ConditionVariableData& condVarData) noexcept;
217 
218  private:
219  enum class NoStateEnumUsed : StateEnumIdentifier
220  {
221  PLACEHOLDER
222  };
223 
224  enum class NoEventEnumUsed : EventEnumIdentifier
225  {
226  PLACEHOLDER
227  };
228 
229  using WaitFunction = cxx::function_ref<ConditionListener::NotificationVector_t()>;
230  template <typename T, typename ContextDataType>
231  cxx::expected<uint64_t, WaitSetError> attachImpl(T& eventOrigin,
232  const WaitSetIsConditionSatisfiedCallback& hasTriggeredCallback,
233  const uint64_t notificationId,
234  const NotificationCallback<T, ContextDataType>& eventCallback,
235  const uint64_t originType,
236  const uint64_t originTypeHash) noexcept;
237 
238  NotificationInfoVector waitAndReturnTriggeredTriggers(const WaitFunction& wait) noexcept;
239  NotificationInfoVector createVectorWithTriggeredTriggers() noexcept;
240 
241  void removeTrigger(const uint64_t uniqueTriggerId) noexcept;
242  void removeAllTriggers() noexcept;
243  void acquireNotifications(const WaitFunction& wait) noexcept;
244 
245  private:
247  TriggerArray m_triggerArray;
248  ConditionVariableData* m_conditionVariableDataPtr{nullptr};
249  ConditionListener m_conditionListener;
250 
251  cxx::stack<uint64_t, Capacity> m_indexRepository;
252  ConditionListener::NotificationVector_t m_activeNotifications;
253 };
254 
255 } // namespace popo
256 } // namespace iox
257 
258 #include "iceoryx_posh/internal/popo/wait_set.inl"
259 
260 #endif // IOX_POSH_POPO_WAIT_SET_HPP
Logical disjunction of a certain number of Triggers.
Definition: wait_set.hpp:62
NotificationInfoVector wait() noexcept
Blocking wait till one or more of the triggers are triggered.
Definition: wait_set.inl:270
void markForDestruction() noexcept
Non-reversible call. After this call wait() and timedWait() do not block any longer and never return ...
Definition: wait_set.inl:49
cxx::expected< WaitSetError > attachEvent(T &eventOrigin, const EventType eventType, const uint64_t notificationId=0U, const NotificationCallback< T, ContextDataType > &eventCallback={}) noexcept
attaches an event of a given class to the WaitSet.
Definition: wait_set.inl:111
cxx::expected< WaitSetError > attachState(T &stateOrigin, const StateType stateType, const uint64_t id=0U, const NotificationCallback< T, ContextDataType > &stateCallback={}) noexcept
attaches a state of a given class to the WaitSet.
Definition: wait_set.inl:168
NotificationInfoVector timedWait(const units::Duration timeout) noexcept
Blocking wait with time limit till one or more of the triggers are triggered.
Definition: wait_set.inl:264
void detachState(T &stateOrigin, const Targs &... args) noexcept
detaches a state based trigger from the WaitSet
Definition: wait_set.inl:233
WaitSet(const WaitSet &rhs)=delete
all the Trigger have a pointer pointing to this waitset for cleanup calls, therefore the WaitSet cann...
void detachEvent(T &eventOrigin, const Targs &... args) noexcept
detaches an event from the WaitSet
Definition: wait_set.inl:226
uint64_t size() const noexcept
Returns the amount of stored Trigger inside of the WaitSet.
Definition: wait_set.inl:344
static constexpr uint64_t capacity() noexcept
returns the maximum amount of triggers which can be acquired from a waitset
Definition: wait_set.inl:350
Definition: service_description.hpp:29
the struct describes a callback with a user defined type which can be attached to a WaitSet or a List...
Definition: notification_callback.hpp:58
Definition: notification_callback.hpp:32