iceoryx_doc  1.0.1
active_object.hpp
1 // Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 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_UTILS_CONCURRENT_ACTIVE_OBJECT_HPP
18 #define IOX_UTILS_CONCURRENT_ACTIVE_OBJECT_HPP
19 
20 #include <functional>
21 #include <thread>
22 
23 #include "iceoryx_utils/internal/concurrent/fifo.hpp"
24 #include "iceoryx_utils/internal/concurrent/trigger_queue.hpp"
25 
26 namespace iox
27 {
28 namespace concurrent
29 {
31 {
32  protected:
33  ActiveObject() noexcept;
34  virtual ~ActiveObject() noexcept;
35  void addTask(const std::function<void()> f) noexcept;
36  void mainLoop() noexcept;
37  void stopRunning() noexcept;
38 
39  friend class cxx::optional<ActiveObject>;
40 
41  private:
42  static constexpr uint32_t taskQueueSize = 128;
43  using taskQueue_t = concurrent::TriggerQueue<std::function<void()>, taskQueueSize, concurrent::FiFo>;
44 
45  taskQueue_t m_tasks;
46 
47  bool m_keepRunning{true};
48  std::thread m_mainLoopThread;
49 };
50 } // namespace concurrent
51 } // namespace iox
52 
53 #endif // IOX_UTILS_CONCURRENT_ACTIVE_OBJECT_HPP
Definition: active_object.hpp:31
single pusher single pop'er thread safe fifo
Definition: fifo.hpp:31
Optional implementation from the C++17 standard with C++11. The interface is analog to the C++17 stan...
Definition: optional.hpp:63
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28