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;
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,
__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;
}