#include <stdio.h>
#include <string.h>
#include <unistd.h>
+#include <pthread.h>
#include <logcommon.h>
#include <zero_copy_backend.h>
static int g_fd;
static char *g_shm_ptr[ZLOGGER_DEVICE_COUNT];
_Thread_local uint16_t blk = 0;
+_Thread_local int g_tid = 0;
extern int (*write_to_log)(log_id_t log_id, log_priority prio, const char *tag, const char *msg, struct timespec *tp_mono);
extern void (*destroy_backend)(void);
+/* Use thread local cache for tid to speed-up logging hot path */
+static inline int gettid_cached(void)
+{
+ if (!g_tid)
+ g_tid = gettid();
+ return g_tid;
+}
+
+/*
+ * fork() doesn't clear/reinitialize thread local variables,
+ * so we need to do this from pthread_atfork() hooks
+ */
+static void clear_thread_locals(void)
+{
+ g_tid = 0;
+}
+
static inline char *get_shared_memory(int dev_index)
{
if (dev_index < 0 || dev_index >= ZLOGGER_DEVICE_COUNT)
* for stdout logs though. */
uint64_t ts = tp_mono ? to_zlog_clock(tp_mono) : get_zlog_clock();
- const int tid = gettid();
+ const int tid = gettid_cached();
size_t hd_size = sizeof(struct zlogger_entry);
size_t prio_size = 1;
size_t tag_size = strnlen(tag, ZLOGGER_TAG_MAX) + 1;
if (g_fd < 0)
return -1;
+ /* properly handle thread local variables on fork() */
+ if (pthread_atfork(NULL, NULL, clear_thread_locals) != 0) {
+ close(g_fd);
+ return -1;
+ }
+
for (int i = 0; i < ZLOGGER_DEVICE_COUNT; i++)
g_shm_ptr[i] = MAP_FAILED;
return 0;
}
-