From 0bf3c59ff99d91419e362e3451738f69eb9edb35 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Sun, 13 Feb 2022 20:06:27 +0100 Subject: [PATCH] Add meshgen_threads setting --- src/client/client_config.c | 10 ++++++++-- src/client/client_config.h | 1 + src/client/client_map.c | 9 ++++++--- src/client/client_map.h | 15 +++++++-------- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/client/client_config.c b/src/client/client_config.c index 658fe5e..2dbe24b 100644 --- a/src/client/client_config.c +++ b/src/client/client_config.c @@ -6,6 +6,7 @@ struct ClientConfig client_config = { .mipmap = true, .render_distance = 255.0, .vsync = true, + .meshgen_threads = 4, }; __attribute__((constructor)) static void client_config_init() @@ -30,7 +31,12 @@ __attribute__((constructor)) static void client_config_init() .type = CT_BOOL, .key = "vsync", .value = &client_config.vsync, - } - }, 4); + }, + { + .type = CT_UINT, + .key = "meshgen_threads", + .value = &client_config.meshgen_threads, + }, + }, 5); } diff --git a/src/client/client_config.h b/src/client/client_config.h index 21b40dd..4850e45 100644 --- a/src/client/client_config.h +++ b/src/client/client_config.h @@ -8,6 +8,7 @@ extern struct ClientConfig { bool mipmap; double render_distance; bool vsync; + unsigned int meshgen_threads; } client_config; #endif diff --git a/src/client/client_map.c b/src/client/client_map.c index 12a7a94..5d2d50e 100644 --- a/src/client/client_map.c +++ b/src/client/client_map.c @@ -2,6 +2,7 @@ #include #include "client/blockmesh.h" #include "client/facecache.h" +#include "client/client_config.h" #include "client/client_map.h" #include "client/client_player.h" #include "client/debug_menu.h" @@ -165,7 +166,8 @@ void client_map_init() client_map.sync_thread = 0; client_map_set_simulation_distance(10); - for (int i = 0; i < NUM_MESHGEN_THREADS; i++) + client_map.meshgen_threads = malloc(sizeof *client_map.meshgen_threads * client_config.meshgen_threads); + for (unsigned int i = 0; i < client_config.meshgen_threads; i++) client_map.meshgen_threads[i] = 0; } @@ -179,7 +181,7 @@ void client_map_deinit() // start meshgen and sync threads void client_map_start() { - for (int i = 0; i < NUM_MESHGEN_THREADS; i++) + for (unsigned int i = 0; i < client_config.meshgen_threads; i++) pthread_create(&client_map.meshgen_threads[i], NULL, &meshgen_thread, NULL); pthread_create(&client_map.sync_thread, NULL, &sync_thread, NULL); @@ -191,9 +193,10 @@ void client_map_stop() client_map.cancel = true; queue_cancel(client_map.queue); - for (int i = 0; i < NUM_MESHGEN_THREADS; i++) + for (unsigned int i = 0; i < client_config.meshgen_threads; i++) if (client_map.meshgen_threads[i]) pthread_join(client_map.meshgen_threads[i], NULL); + free(client_map.meshgen_threads); if (client_map.sync_thread) pthread_join(client_map.sync_thread, NULL); diff --git a/src/client/client_map.h b/src/client/client_map.h index 34998b5..f340a9a 100644 --- a/src/client/client_map.h +++ b/src/client/client_map.h @@ -6,7 +6,6 @@ #include #include "map.h" #include "client/object.h" -#define NUM_MESHGEN_THREADS 4 typedef enum { @@ -25,13 +24,13 @@ typedef struct extern struct ClientMap { - Map *map; // map object - Queue *queue; // MapBlock * queue (thread safe) - atomic_bool cancel; // used to notify meshgen and sync thread about quit - pthread_t meshgen_threads[NUM_MESHGEN_THREADS]; // consumer threads for meshgen queue - pthread_t sync_thread; // this thread requests new / changed blocks from server - u32 simulation_distance; // simulation distance sent by server - size_t blocks_count; // cached number of facecache positions to process every sync step (matches simulation distance) + Map *map; // map object + Queue *queue; // MapBlock * queue (thread safe) + atomic_bool cancel; // used to notify meshgen and sync thread about quit + pthread_t *meshgen_threads; // consumer threads for meshgen queue + pthread_t sync_thread; // this thread requests new / changed blocks from server + u32 simulation_distance; // simulation distance sent by server + size_t blocks_count; // cached number of facecache positions to process every sync step (matches simulation distance) } client_map; void client_map_init(); // ClientMap singleton constructor -- 2.44.0