commit 555e3b5ac27f5ada97dcb0a4857b4d72ecfd8b0f
parent db500a5de62e41525e3eca40529c563533b59309
Author: Yersa Nordman <yersa@finwo.nl>
Date: Thu, 26 Oct 2023 22:59:09 +0200
Breaking: move thread trigger outside of http_server_main
Diffstat:
3 files changed, 10 insertions(+), 7 deletions(-)
diff --git a/example.c b/example.c
@@ -5,8 +5,10 @@
#include "finwo/fnet.h"
#include "finwo/http-parser.h"
+#include "pierreguillot/thread.h"
#include "tidwall/buf.h"
+
#include "http-server.h"
uint16_t targetPort = 8080;
@@ -82,8 +84,14 @@ int main() {
http_server_route("GET" , "/hello", route_get_hello);
http_server_route("POST", "/port" , route_post_port);
+ // Launch network management thread
+ thd_thread thread;
+ thd_thread_detach(&thread, http_server_fnet_thread, NULL);
+
http_server_main(&opts);
fnet_shutdown();
+ thd_thread_join(&thread);
+
printf("Server has shut down\n");
}
diff --git a/src/http-server.c b/src/http-server.c
@@ -5,7 +5,6 @@
#include "finwo/http-parser.h"
#include "finwo/fnet.h"
-#include "pierreguillot/thread.h"
#include "http-server.h"
@@ -147,7 +146,7 @@ void _hs_onListenClose(struct fnet_ev *ev) {
}
}
-void _thread_network(void *arg) {
+void http_server_fnet_thread(void *arg) {
UNUSED(arg);
FNET_RETURNCODE ret = fnet_main();
}
@@ -181,11 +180,6 @@ void http_server_main(struct http_server_opts *opts) {
exit(1);
}
- // Launch network management thread
- // May or may not keep running (either is fine)
- thd_thread thread;
- thd_thread_detach(&thread, _thread_network, NULL);
-
// This is a forever function, controlled by network thread
while(!opts->shutdown) {
sleep_ms(100);
diff --git a/src/http-server.h b/src/http-server.h
@@ -27,6 +27,7 @@ struct http_server_events {
void (*tick)(void *udata);
};
+void http_server_fnet_thread(void *arg);
void http_server_main(struct http_server_opts *opts);
void http_server_response_send(struct http_server_reqdata *reqdata, bool close);
void http_server_route(const char *method, const char *path, void (*fn)(struct http_server_reqdata*));