]> git.ipfire.org Git - fireperf.git/commitdiff
worker: Create scaffolding for a new universal worker
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 19 Sep 2024 06:58:23 +0000 (06:58 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 19 Sep 2024 07:04:46 +0000 (07:04 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/worker.c [new file with mode: 0644]
src/worker.h [new file with mode: 0644]

index 8d605c49c74674619e71d5b08a2a9659c6abf876..16c30daeddd1bb9890dbf638cd4fdecdfb97a87b 100644 (file)
@@ -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 (file)
index 0000000..2eef429
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+#include <pthread.h>
+#include <stdlib.h>
+#include <sys/epoll.h>
+#include <unistd.h>
+
+#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 (file)
index 0000000..e7d182b
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#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 */