From 7ac10398b87c417435e91fd00e9d840adb97a90b Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 19 Sep 2024 06:58:23 +0000 Subject: [PATCH] worker: Create scaffolding for a new universal worker Signed-off-by: Michael Tremer --- Makefile.am | 4 +- src/worker.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/worker.h | 33 ++++++++++++++++ 3 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 src/worker.c create mode 100644 src/worker.h diff --git a/Makefile.am b/Makefile.am index 8d605c4..16c30da 100644 --- a/Makefile.am +++ b/Makefile.am @@ -71,7 +71,9 @@ fireperf_SOURCES = \ src/server.c \ src/server.h \ src/util.c \ - src/util.h + src/util.h \ + src/worker.c \ + src/worker.h # ------------------------------------------------------------------------------ diff --git a/src/worker.c b/src/worker.c new file mode 100644 index 0000000..2eef429 --- /dev/null +++ b/src/worker.c @@ -0,0 +1,107 @@ +/*############################################################################# +# # +# fireperf - A network benchmarking tool # +# Copyright (C) 2024 IPFire Development Team # +# # +# 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 . # +# # +#############################################################################*/ + +#include +#include +#include +#include +#include + +#include "main.h" +#include "logging.h" +#include "worker.h" + +struct fireperf_worker { + pthread_t thread; + + // Configuration + struct fireperf_config* conf; + + // Epoll + int epollfd; +}; + +static void fireperf_worker_free(struct fireperf_worker* worker) { + if (worker->epollfd >= 0) + close(worker->epollfd); + + free(worker); +} + +int fireperf_worker_create(struct fireperf_worker** worker, struct fireperf_config* conf) { + struct fireperf_worker* w = NULL; + int r; + + // Allocate a new worker + w = calloc(1, sizeof(*w)); + if (!w) + return -errno; + + // Reference the configuration + w->conf = conf; + + // Create a new event loop + w->epollfd = epoll_create1(0); + if (w->epollfd < 0) { + ERROR(conf, "Could not create event loop: %m\n"); + r = -errno; + goto ERROR; + } + + DEBUG(conf, "Created a new worker\n"); + + // Return the worker + *worker = w; + + return 0; + +ERROR: + if (w) + fireperf_worker_free(w); + + return r; +} + +/* + The main function of the worker. +*/ +static void* fireperf_worker_main(void* w) { + struct fireperf_worker* worker = w; + + DEBUG(worker->conf, "New worker launched as %lu\n", pthread_self()); + + return 0; +} + +int fireperf_worker_launch(struct fireperf_worker* worker) { + int r; + + // Launch the worker + r = pthread_create(&worker->thread, NULL, fireperf_worker_main, worker); + if (r) { + ERROR(worker->conf, "Could not launch the worker: %m\n"); + } + + return r; +} + +int fireperf_worker_is_alive(struct fireperf_worker* worker) { + return pthread_tryjoin_np(worker->thread, NULL); +} diff --git a/src/worker.h b/src/worker.h new file mode 100644 index 0000000..e7d182b --- /dev/null +++ b/src/worker.h @@ -0,0 +1,33 @@ +/*############################################################################# +# # +# fireperf - A network benchmarking tool # +# Copyright (C) 2024 IPFire Development Team # +# # +# 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 FIREPERF_WORKER_H +#define FIREPERF_WORKER_H + +struct fireperf_worker; + +#include "main.h" + +int fireperf_worker_create(struct fireperf_worker** worker, struct fireperf_config* conf); + +int fireperf_worker_launch(struct fireperf_worker* worker); +int fireperf_worker_is_alive(struct fireperf_worker* worker); + +#endif /* FIREPERF_WORKER_H */ -- 2.47.2