From 2fceca73813b0645e6c968c6a8ed74c9a564f922 Mon Sep 17 00:00:00 2001
From: Florian Fischer <florian.fischer@muhq.space>
Date: Wed, 4 Aug 2021 15:04:07 +0200
Subject: [PATCH] [Runtime] use gettid() if provided by libc

---
 emper/Runtime.cpp | 9 ++++++---
 emper/meson.build | 3 +++
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/emper/Runtime.cpp b/emper/Runtime.cpp
index ad79ad43..0ca865bd 100644
--- a/emper/Runtime.cpp
+++ b/emper/Runtime.cpp
@@ -8,6 +8,7 @@
 // Non portable.
 #include <sched.h>				// for cpu_set_t, CPU_SET, CPU_ZERO
 #include <sys/sysinfo.h>	// for get_nprocs
+#include <unistd.h>
 
 #include <cstdlib>	// for rand, srand, abort
 #include <cstring>
@@ -48,8 +49,10 @@
 #include <urcu.h>	 // for rcu_register_thread
 #endif
 
-#include <syscall.h>	// for SYS_gettid
-#include <unistd.h>		// for syscall
+#ifndef EMPER_HAS_GETTID
+#include <syscall.h>
+#define gettid() (syscall(SYS_gettid))
+#endif
 
 std::mutex Runtime::currentRuntimeMutex;
 Runtime* Runtime::currentRuntime;
@@ -249,7 +252,7 @@ auto Runtime::workerLoop(Worker* worker) -> void* {
 		workerIo->setWorkerIo(worker);
 	}
 
-	LOGD("Worker loop started by thread " << syscall(SYS_gettid));
+	LOGD("Worker loop started by thread " << gettid());
 
 	int oldType;
 	errno = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldType);
diff --git a/emper/meson.build b/emper/meson.build
index 96c3737f..5e827d84 100644
--- a/emper/meson.build
+++ b/emper/meson.build
@@ -1,3 +1,6 @@
+cpp_has_gettid = cpp_compiler.has_function('gettid', prefix: '#include<unistd.h>')
+conf_data.set('EMPER_HAS_GETTID', cpp_has_gettid)
+
 emper_asm_sources = [ 'ContextAsm.nasm' ]
 nasm = find_program('nasm')
 # mesons's nasm support currently means using generator()
-- 
GitLab