From 5a5d45e9542cd7ebd3d72c276f7d97dafbb833c0 Mon Sep 17 00:00:00 2001 From: Karol Lewandowski Date: Tue, 28 Nov 2023 00:59:24 +0100 Subject: [PATCH 1/1] libdlog: Rewrite errnous comments about pthread_atfork() usage Change-Id: I56106fddfddf00b38639b147dd80ab90e0a9f6fa --- src/libdlog/log.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/libdlog/log.c b/src/libdlog/log.c index b9a3a77..023990f 100644 --- a/src/libdlog/log.c +++ b/src/libdlog/log.c @@ -103,8 +103,9 @@ static int fatal_assert; static int limiter_apply_to_all_buffers; static _Atomic log_priority priority_filter_level = DLOG_VERBOSE; -/* Cache pid and tid to avoid up to two syscalls per log. The update function - * is registered to happen when new threads are made (including a full fork). */ +/* Cache pid and tid to avoid up to two syscalls per log. + * PID is reset at fork() via pthread_atfork(). + * TID is reset by being a thread local var. */ static int32_t cached_pid = 0; _Thread_local int32_t cached_tid = 0; @@ -118,10 +119,10 @@ static inline int32_t get_cached_tid() return (cached_tid = cached_tid ?: gettid()); } -static void update_thread_local_ids(void) +static void update_cached_pid() { - cached_pid = 0; - cached_tid = 0; + cached_pid = getpid(); + // tid is reset passively (to 0) by virtue of being thread-local } /* Here, static_config is the original config from /etc/dlog.conf{,.d} which can be overriden, @@ -277,9 +278,7 @@ bool __configure(void) __configure_deduplicate(&both_config); __configure_limiter(&static_config, &both_config); - /* The fork handler also runs when a new thread is made (not just - * when the whole process forks), so we can cache both pid and tid. */ - pthread_atfork(NULL, NULL, update_thread_local_ids); + pthread_atfork(NULL, NULL, update_cached_pid); return true; } -- 2.7.4