]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Threads: assigning IDs from hmap
authorKaterina Kubecova <katerina.kubecova@nic.cz>
Wed, 18 Dec 2024 13:24:25 +0000 (14:24 +0100)
committerMaria Matejka <mq@ucw.cz>
Wed, 19 Feb 2025 08:42:45 +0000 (09:42 +0100)
For certain upcoming data structures, we actually need to use thread IDs
as a functional information to index things, not just a logging token.
Thus, we need them to be dense, not just flying around as they were until now.

To achieve this, we assign the IDs from a global hmap when the threads
are started, and properly return them when the threads are finished.
This way, the IDs of stopping threads are expected to be recycled,
whereas until now it wasn't expected to happen.

You may need to take care about this in your log reading apparatus.

Also there is now a maximum thread count hard limit because unlimited
thread count is too crazy to handle. But the limit is still ridiculously
high and nobody is ever expected to hit it anyway.

lib/birdlib.h
lib/io-loop.h
sysdep/unix/config.Y
sysdep/unix/io-loop.c
sysdep/unix/io-loop.h
sysdep/unix/main.c

index bc7c334cb36bfc68c8ff5f433fa0a934dd625cb1..165dc98215919637c11abd090df5739f49586897 100644 (file)
@@ -232,7 +232,7 @@ void debug_safe(const char *msg);   /* Printf to debug output, async-safe */
 /* Internal thread ID, useful for logging */
 extern _Atomic uint max_thread_id;
 extern _Thread_local uint this_thread_id;
-#define THIS_THREAD_ID  (this_thread_id ?: (this_thread_id = atomic_fetch_add_explicit(&max_thread_id, 1, memory_order_acq_rel)))
+#define THIS_THREAD_ID  this_thread_id
 
 
 /* Debugging */
index ce9002057f1fd8ab8a0740baeca252486d7d1be4..1b70a61670479a3b31942a0dc0a9d597526df936 100644 (file)
@@ -17,6 +17,8 @@
 
 extern struct birdloop main_birdloop;
 
+#define MAX_THREADS 256
+
 /* Currently running birdloop */
 extern _Thread_local struct birdloop *this_birdloop;
 
index 42d78c6f648c5e99e0d55c0814c55e4ef12e09bf..6e2452c42e35fd17dc2f32eb1883d4a0c6c07751 100644 (file)
@@ -147,6 +147,7 @@ cli_opts_block:
 
 conf: THREADS expr {
     if ($2 < 1) cf_error("Number of threads must be at least one.");
+    if ($2 > MAX_THREADS) cf_error("Number of threads must not be higher than %i.", MAX_THREADS);
     new_config->thread_count = $2;
 }
 
index 75d863757366386b187af4dd3549da9084573200..7e4e2a80add076ed54a4e492fd15118574c09596 100644 (file)
@@ -24,6 +24,7 @@
 #include "lib/event.h"
 #include "lib/timer.h"
 #include "lib/socket.h"
+#include "lib/bitmap.h"
 
 #include "lib/io-loop.h"
 #include "sysdep/unix/io-loop.h"
@@ -840,10 +841,13 @@ bird_thread_busy_set(struct bird_thread *thr, int val)
   UNLOCK_DOMAIN(attrs, thr->group->domain);
 }
 
+struct hmap hmap_thread_ids;
+
 static void *
 bird_thread_main(void *arg)
 {
   struct bird_thread *thr = this_thread = arg;
+  this_thread_id = thr->id;
 
   rcu_thread_start();
 
@@ -1013,6 +1017,9 @@ bird_thread_cleanup(void *_thr)
   thr->meta->thread = NULL;
   thr->meta = NULL;
   birdloop_free(meta);
+
+  /* Return the thread ID */
+  hmap_clear(&hmap_thread_ids, thr->id);
 }
 
 static struct bird_thread *
@@ -1034,6 +1041,10 @@ bird_thread_start(struct birdloop_pickup_group *group)
   thr->meta = meta;
   thr->meta->thread = thr;
 
+  thr->id = hmap_first_zero(&hmap_thread_ids);
+  hmap_set(&hmap_thread_ids, thr->id);
+  thr->id++;
+
   wakeup_init(thr);
   ev_init_list(&thr->priority_events, NULL, "Thread direct event list");
 
@@ -1505,6 +1516,8 @@ birdloop_init(void)
   this_birdloop = &main_birdloop;
   this_thread = &main_thread;
 
+  hmap_init(&hmap_thread_ids, &root_pool, 1024);
+
   defer_init(lp_new(&root_pool));
 }
 
index f7fde411b6c817d3d0e62baeab4f294aac5f5c84..f17f87b3581da7cf134b2692a8b7617838982b8c 100644 (file)
@@ -76,6 +76,7 @@ struct birdloop
 struct bird_thread
 {
   node n;
+  uint id;
 
   struct pipe wakeup;
   event_list priority_events;
index 2e5987f97f0f5c6f5b8220768830b1edc2e39ea6..971ff8fca24bf6ed65238b2291a89b31524dfa9f 100644 (file)
@@ -41,6 +41,7 @@
 #include "conf/conf.h"
 #include "filter/filter.h"
 #include "filter/data.h"
+#include "lib/io-loop.h"
 
 #include "unix.h"
 #include "krt.h"