iceoryx_doc  1.0.1
periodic_task.hpp
1 // Copyright (c) 2020 by Apex.AI Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // SPDX-License-Identifier: Apache-2.0
16 
17 #ifndef IOX_UTILS_CONCURRENT_PERIODIC_TASK_HPP
18 #define IOX_UTILS_CONCURRENT_PERIODIC_TASK_HPP
19 
20 #include "iceoryx_utils/cxx/string.hpp"
21 #include "iceoryx_utils/internal/units/duration.hpp"
22 #include "iceoryx_utils/posix_wrapper/semaphore.hpp"
23 #include "iceoryx_utils/posix_wrapper/thread.hpp"
24 
25 #include <thread>
26 
27 #include <iostream>
28 
29 namespace iox
30 {
31 namespace concurrent
32 {
35 {
36 };
37 static constexpr PeriodicTaskAutoStart_t PeriodicTaskAutoStart;
38 
41 {
42 };
43 static constexpr PeriodicTaskManualStart_t PeriodicTaskManualStart;
44 
63 template <typename T>
65 {
66  public:
74  template <typename... Args>
75  PeriodicTask(const PeriodicTaskManualStart_t, const posix::ThreadName_t taskName, Args&&... args) noexcept;
76 
85  template <typename... Args>
87  const units::Duration interval,
88  const posix::ThreadName_t taskName,
89  Args&&... args) noexcept;
90 
93  ~PeriodicTask() noexcept;
94 
95  PeriodicTask(const PeriodicTask&) = delete;
96  PeriodicTask(PeriodicTask&&) = delete;
97 
98  PeriodicTask& operator=(const PeriodicTask&) = delete;
99  PeriodicTask& operator=(PeriodicTask&&) = delete;
100 
106  void start(const units::Duration interval) noexcept;
107 
111  void stop() noexcept;
112 
115  bool isActive() const noexcept;
116 
117  private:
118  void run() noexcept;
119 
120  private:
121  T m_callable;
122  posix::ThreadName_t m_taskName;
125  posix::Semaphore m_stop{posix::Semaphore::create(posix::CreateUnnamedSingleProcessSemaphore, 0U).value()};
126  std::thread m_taskExecutor;
127 };
128 
129 } // namespace concurrent
130 } // namespace iox
131 
132 #include "iceoryx_utils/internal/concurrent/periodic_task.inl"
133 
134 #endif // IOX_UTILS_CONCURRENT_PERIODIC_TASK_HPP
static result_t create(Targs &&... args) noexcept
factory method which guarantees that either a working object is produced or an error value describing...
Definition: creation.inl:41
This class periodically executes a callable specified by the template parameter. This can be a struct...
Definition: periodic_task.hpp:65
bool isActive() const noexcept
This method check if a thread is spawned and running, potentially executing a task.
Definition: periodic_task.inl:71
PeriodicTask(const PeriodicTaskManualStart_t, const posix::ThreadName_t taskName, Args &&... args) noexcept
Creates a periodic task. The specified callable is stored but not executed. To run the task,...
Definition: periodic_task.inl:26
~PeriodicTask() noexcept
Stops and joins the thread spawned by the constructor.
Definition: periodic_task.inl:46
void stop() noexcept
This stops the thread if it's running, otherwise does nothing. When this method returns,...
Definition: periodic_task.inl:61
void start(const units::Duration interval) noexcept
Spawns a thread and immediately executes the callable specified with the constructor....
Definition: periodic_task.inl:52
Posix semaphore C++ Wrapping class.
Definition: semaphore.hpp:84
Definition: duration.hpp:77
static constexpr Duration fromMilliseconds(const T value) noexcept
Constructs a new Duration object from milliseconds.
Definition: duration.inl:88
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28
This is a helper struct to make the immediate start of the task in the PeriodicTask ctor obvious to t...
Definition: periodic_task.hpp:35
This is a helper struct to make the manual start of the task with the start method obvious to the use...
Definition: periodic_task.hpp:41