From eea697d523cbbb1daa3225d6bd0841db7a7e1bca Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Wed, 27 Nov 2024 21:56:16 +0000 Subject: [PATCH] threadpool: Simplify pool thread naming In my experience, it isn't actually useful to have the program name as part of the thread name (we don't do that for other threads that we create: pool-spawner, gmain, gdbus, ...) and the size limit of 16 means that there really isn't enough space for it. But what is somewhat useful is to have different threads have different names, so number the pool threads: pool-0, pool-1, ... --- glib/gthreadpool.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/glib/gthreadpool.c b/glib/gthreadpool.c index 9e88ededb..10fa2bceb 100644 --- a/glib/gthreadpool.c +++ b/glib/gthreadpool.c @@ -103,6 +103,8 @@ static gint max_unused_threads = 8; static gint kill_unused_threads = 0; static guint max_idle_time = 15 * 1000; +static int thread_counter = 0; + typedef struct { /* Either thread or error are set in the end. Both transfer-full. */ @@ -283,11 +285,9 @@ g_thread_pool_spawn_thread (gpointer data) SpawnThreadData *spawn_thread_data; GThread *thread = NULL; GError *error = NULL; - const gchar *prgname = g_get_prgname (); - gchar name[16] = "pool"; + gchar name[16]; - if (prgname) - g_snprintf (name, sizeof (name), "pool-%s", prgname); + g_snprintf (name, sizeof (name), "pool-%d", g_atomic_int_add (&thread_counter, 1)); g_async_queue_lock (spawn_thread_queue); /* Spawn a new thread for the given pool and wake the requesting thread @@ -434,13 +434,8 @@ g_thread_pool_start_thread (GRealThreadPool *pool, if (!success) { - const gchar *prgname = g_get_prgname (); - gchar name[16] = "pool"; GThread *thread; - if (prgname) - g_snprintf (name, sizeof (name), "pool-%s", prgname); - /* No thread was found, we have to start a new one */ if (pool->pool.exclusive) { @@ -448,6 +443,10 @@ g_thread_pool_start_thread (GRealThreadPool *pool, * we simply start new threads that inherit the scheduler settings * from the current thread. */ + char name[16]; + + g_snprintf (name, sizeof (name), "pool-%d", g_atomic_int_add (&thread_counter, 1)); + thread = g_thread_try_new (name, g_thread_pool_thread_proxy, pool, error); } else