.. _program_listing_file_liberate_concurrency_tasklet.h: Program Listing for File tasklet.h ================================== |exhale_lsh| :ref:`Return to documentation for file ` (``liberate/concurrency/tasklet.h``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp /* * This file is part of liberate. * * Author(s): Jens Finkhaeuser * * Copyright (c) 2014 Unwesen Ltd. * Copyright (c) 2015-2021 Jens Finkhaeuser. * Copyright (c) 2022 Interpeer gUG (haftungsbeschränkt) * * SPDX-License-Identifier: GPL-3.0-only * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef LIBERATE_CONCURRENCY_TASKLET_H #define LIBERATE_CONCURRENCY_TASKLET_H #ifndef __cplusplus #error You are trying to include a C++ only header file #endif #include #include #include #include #include namespace liberate::concurrency { class LIBERATE_API tasklet { public: /*************************************************************************** * Types **/ struct LIBERATE_API sleep_condition { std::condition_variable condition; std::mutex mutex; }; struct LIBERATE_API context { std::atomic running; sleep_condition * condition; explicit context(sleep_condition * condition); virtual ~context(); template inline bool sleep(durationT const & duration) const { return context::nanosleep(std::chrono::round(duration)); } inline bool sleep() const { return context::nanosleep(std::chrono::nanoseconds(-1)); } private: /*************************************************************************** * Implementation functions **/ bool nanosleep(std::chrono::nanoseconds nsecs) const; }; using task_function = std::function; /*************************************************************************** * Constructor/destructor **/ explicit tasklet(task_function && func, bool start_now = false); tasklet(task_function && func, sleep_condition * condition, bool start_now = false); ~tasklet(); /*************************************************************************** * Main interface **/ bool start(); bool stop(); void wait(); void wakeup(); private: tasklet(tasklet const &) = delete; tasklet(tasklet &&) = delete; tasklet & operator=(tasklet const &) = delete; /*************************************************************************** * Data **/ mutable std::unique_ptr m_context; }; } // namespace liberate::concurrency #endif // guard