libdlog: Rewrite errnous comments about pthread_atfork() usage 31/301931/2
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Mon, 27 Nov 2023 23:59:24 +0000 (00:59 +0100)
committerMichal Bloch <m.bloch@samsung.com>
Fri, 15 Dec 2023 12:45:35 +0000 (13:45 +0100)
Change-Id: I56106fddfddf00b38639b147dd80ab90e0a9f6fa

src/libdlog/log.c

index b9a3a77..023990f 100644 (file)
@@ -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;
 }