From 7dc1ba4a6dddd0c2c225b1bb986a7ec56c3a01e3 Mon Sep 17 00:00:00 2001 From: Fabian Posch Date: Mon, 8 Jan 2024 17:12:26 -0500 Subject: [PATCH] actually commit the files for the workers as well --- include/actsim_agent/worker.hpp | 54 +++++++++++++++++++++++++++++++++ src/actsim_agent/worker.cpp | 54 +++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 include/actsim_agent/worker.hpp create mode 100644 src/actsim_agent/worker.cpp diff --git a/include/actsim_agent/worker.hpp b/include/actsim_agent/worker.hpp new file mode 100644 index 0000000..6c20864 --- /dev/null +++ b/include/actsim_agent/worker.hpp @@ -0,0 +1,54 @@ + +/************************************************************************* + * + * 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 __WORKER_H__ +#define __WORKER_H__ + +#include +#include +#include + +class Worker { + + public: + + Worker(volatile std::atomic_bool& stop_flag); + + void start(); + void cancel_current(); + void join(); + + db::uuid_t get_current_task() { return this->current_task.load(std::memory_order_relaxed); }; + + private: + + void thread_run(); + + std::unique_ptr worker_thread; + std::atomic current_task; + volatile std::atomic_bool& stop_flag; +}; + +#endif \ No newline at end of file diff --git a/src/actsim_agent/worker.cpp b/src/actsim_agent/worker.cpp new file mode 100644 index 0000000..e0d2229 --- /dev/null +++ b/src/actsim_agent/worker.cpp @@ -0,0 +1,54 @@ + +/************************************************************************* + * + * 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. + * + ************************************************************************** + */ + +#include +#include +#include "worker.hpp" + +Worker::Worker(volatile std::atomic_bool& stop_flag) : stop_flag(stop_flag) {} + +void Worker::start() { + std::cout << "Worker started" << std::endl; + this->worker_thread = std::make_unique([this]() { thread_run(); }); +} + +void Worker::cancel_current() { + std::cout << "Current simulation cancelled." << std::endl; +} + +void Worker::join() { + worker_thread->join(); +} + +void Worker::thread_run() { + while (true) { + std::cout << "Worker ping" << std::endl; + if (this->stop_flag.load(std::memory_order_relaxed)) { + break; + } + + usleep(100000); + } +}