Skip to content

iceoryx_utils/internal/concurrent/periodic_task.hpp🔗

Namespaces🔗

Name
iox
building block to easily create free function for logging in a library context
iox::concurrent

Classes🔗

Name
struct iox::concurrent::PeriodicTaskAutoStart_t
This is a helper struct to make the immediate start of the task in the PeriodicTask ctor obvious to the user.
struct iox::concurrent::PeriodicTaskManualStart_t
This is a helper struct to make the manual start of the task with the start method obvious to the user.
class iox::concurrent::PeriodicTask
This class periodically executes a callable specified by the template parameter. This can be a struct with a operator()() overload, a [cxx::function_ref]()<void()> or std::fuction<void()>.

Source code🔗

// Copyright (c) 2020 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

#ifndef IOX_UTILS_CONCURRENT_PERIODIC_TASK_HPP
#define IOX_UTILS_CONCURRENT_PERIODIC_TASK_HPP

#include "iceoryx_utils/cxx/string.hpp"
#include "iceoryx_utils/internal/units/duration.hpp"
#include "iceoryx_utils/posix_wrapper/semaphore.hpp"
#include "iceoryx_utils/posix_wrapper/thread.hpp"

#include <thread>

#include <iostream>

namespace iox
{
namespace concurrent
{
struct PeriodicTaskAutoStart_t
{
};
static constexpr PeriodicTaskAutoStart_t PeriodicTaskAutoStart;

struct PeriodicTaskManualStart_t
{
};
static constexpr PeriodicTaskManualStart_t PeriodicTaskManualStart;

template <typename T>
class PeriodicTask
{
  public:
    template <typename... Args>
    PeriodicTask(const PeriodicTaskManualStart_t, const posix::ThreadName_t taskName, Args&&... args) noexcept;

    template <typename... Args>
    PeriodicTask(const PeriodicTaskAutoStart_t,
                 const units::Duration interval,
                 const posix::ThreadName_t taskName,
                 Args&&... args) noexcept;

    ~PeriodicTask() noexcept;

    PeriodicTask(const PeriodicTask&) = delete;
    PeriodicTask(PeriodicTask&&) = delete;

    PeriodicTask& operator=(const PeriodicTask&) = delete;
    PeriodicTask& operator=(PeriodicTask&&) = delete;

    void start(const units::Duration interval) noexcept;

    void stop() noexcept;

    bool isActive() const noexcept;

  private:
    void run() noexcept;

  private:
    T m_callable;
    posix::ThreadName_t m_taskName;
    units::Duration m_interval{units::Duration::fromMilliseconds(0U)};
    posix::Semaphore m_stop{posix::Semaphore::create(posix::CreateUnnamedSingleProcessSemaphore, 0U).value()};
    std::thread m_taskExecutor;
};

} // namespace concurrent
} // namespace iox

#include "iceoryx_utils/internal/concurrent/periodic_task.inl"

#endif // IOX_UTILS_CONCURRENT_PERIODIC_TASK_HPP

Updated on 31 May 2022 at 15:29:15 CEST