/************************************************************************* * * This file is part of the ACT library * * Copyright (c) 2024 Fabian Posch * * 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 2 * 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, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * ************************************************************************** */ #ifndef __TASK_INTERFACE__ #define __TASK_INTERFACE__ #include #include #include #include #include #include // if you want to use this interface for different types, you only need to change it here using InputType = pl::SimConfigArtifact; using OutputType = pl::SimOutputArtifact; class TaskInterface { public: TaskInterface(size_t buffer_size); void wait_for_fresh(); void wait_for_finished(); void wait_for_buffer_consume(); void wait_for_cleanup_ready(); void notify_cleanup_ready(); void notify_download_program_halt(); void notify_workers_program_halt(); void push_fresh(std::unique_ptr task); std::unique_ptr pop_fresh(bool& empty); void push_finished(std::unique_ptr task); std::unique_ptr pop_finished(bool& empty); size_t get_buffer_space(); bool search_and_increment(db::uuid_t id, std::string& design); void decrement(db::uuid_t id); void store(db::uuid_t, std::string& design); bool running() { return this->running_.load(std::memory_order_relaxed); }; bool is_stop_immediate() { return !this->immediate_stop.load(std::memory_order_relaxed); }; void stop() { this->running_.store(false, std::memory_order_relaxed); }; void stop_immediately() { this->immediate_stop.store(false, std::memory_order_relaxed); }; bool fresh_queue_empty(); bool finished_queue_empty(); private: size_t buffer_size; std::queue> fresh_queue; std::queue> finished_queue; volatile std::atomic_bool running_ = std::atomic_bool(true); volatile std::atomic_bool immediate_stop; std::unordered_map> designs; ////// Mutexes ////// // access to task queues std::mutex fresh_queue_mutex, finished_queue_mutex; // design map access std::mutex designs_mutex; // notify upload thread that the finished queue is ready for cleanup std::atomic_bool cleanup_ready; std::mutex cleanup_ready_mutex; std::condition_variable cleanup_ready_condition; // inform the worker threads that there is data in the fresh task queue std::mutex fresh_queue_empty_mutex; std::condition_variable fresh_queue_empty_condition; // inform the upload thread that there is data in the fresh task queue std::mutex finished_queue_empty_mutex; std::condition_variable finished_queue_empty_condition; // inform the download thread that data was consumed std::mutex fresh_queue_full_mutex; std::condition_variable fresh_queue_full_condition; }; #endif