]> 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>
Mon, 10 Mar 2025 10:51:28 +0000 (11:51 +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/io-loop.c
sysdep/unix/io-loop.h
sysdep/unix/main.c

index 5a004d9a21fd13a15a06b9c4af6769414a88e42f..db3a83574503f115257dd69b21e35b1064b7ef35 100644 (file)
@@ -234,7 +234,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 d19d3fe5ab6258cc05a6ab667dce5d88068f5e52..8c2a6cfbdcba42a491bbae69f664da38669f9462 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 82f9782d5dc5dc384f382d09206c638a5b7d9e5f..deda2b40c07adacc8279b10a4e1c0ae0433eb793 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"
@@ -937,10 +938,13 @@ bird_thread_busy_set(struct thread_group_private *group, int val)
   ASSERT_DIE(group->thread_busy_count <= group->thread_count);
 }
 
+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();
 
@@ -1161,21 +1165,9 @@ bird_thread_cleanup(void *_thr)
   if (leftover_loops)
     /* Happens only with the last thread */
     bird_thread_group_done(gpub, leftover_loops);
-}
 
-static void bird_thread_start(thread_group *);
-static void
-bird_thread_start_event(void *_data)
-{
-  thread_group *group = _data;
-  bird_thread_start(group);
-}
-
-static void
-bird_thread_start_indirect(struct thread_group_private *group)
-{
-  group->thread_dropper.event.hook = bird_thread_start_event;
-  ev_send(&global_event_list, &group->thread_dropper.event);
+  /* Return the thread ID */
+  hmap_clear(&hmap_thread_ids, thr->id);
 }
 
 static void
@@ -1205,6 +1197,10 @@ bird_thread_start(thread_group *gpub)
       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");
 
@@ -1234,6 +1230,20 @@ bird_thread_start(thread_group *gpub)
   }
 }
 
+static void
+bird_thread_start_event(void *_data)
+{
+  thread_group *group = _data;
+  bird_thread_start(group);
+}
+
+static void
+bird_thread_start_indirect(struct thread_group_private *group)
+{
+  group->thread_dropper.event.hook = bird_thread_start_event;
+  ev_send(&global_event_list, &group->thread_dropper.event);
+}
+
 static void
 bird_thread_dropper_free(void *data)
 {
@@ -1780,6 +1790,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 6681f4d6d591370a3606a3a3020887222dbf9dc7..51418b90f65362e0166b5d4d45666ab39524c072 100644 (file)
@@ -90,6 +90,7 @@ struct bird_thread
 #define TLIST_WANT_WALK
 #define TLIST_WANT_ADD_TAIL
   TLIST_DEFAULT_NODE;
+  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"