aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPau Espin Pedrol <pespin@sysmocom.de>2021-02-17 17:46:02 +0100
committerPau Espin Pedrol <pespin@sysmocom.de>2021-02-17 18:24:17 +0100
commit88e4058fc4ab8c4215327154f4c32f0652bb3e6b (patch)
tree3354bebf50d0ce71259d6a9b3ab2fb119b316d5a /src
parentca770ae77e38b6a634282e19d7b6e16a28db39f3 (diff)
Introduce osmo_gettid() API
This API wraps conventional gettid() linux-specific API, which even in Linux itself is sometimes not properly supported/announced. This API also allows future porting to other platforms if needed, and so far falls back to getpid() if no gettid(9 can be found. Code ported from osmo-trx.git, see commit 7a07de1efd4eb7cc11c33d3ad25cb2df70aa1ef1. Related: OS#5027 Change-Id: Id7534beeb22fcd50813dab76dd68818e2ff87ec2
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am1
-rw-r--r--src/thread.c60
2 files changed, 61 insertions, 0 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index a2566a33..e56b57e2 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -25,6 +25,7 @@ libosmocore_la_SOURCES = context.c timer.c timer_gettimeofday.c timer_clockgetti
conv_acc.c conv_acc_generic.c sercomm.c prbs.c \
isdnhdlc.c \
tdef.c \
+ thread.c \
sockaddr_str.c \
use_count.c \
exec.c \
diff --git a/src/thread.c b/src/thread.c
new file mode 100644
index 00000000..956fee76
--- /dev/null
+++ b/src/thread.c
@@ -0,0 +1,60 @@
+/*
+ * (C) 2021 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
+ * All Rights Reserved
+ *
+ * Author: Pau Espin Pedrol <pespin@sysmocom.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+/*! \addtogroup thread
+ * @{
+ * \file thread.c
+ */
+
+/*! \file thread.c
+ */
+
+#include "config.h"
+
+/* If HAVE_GETTID, then "_GNU_SOURCE" may need to be defined to use gettid() */
+#if HAVE_GETTID
+#define _GNU_SOURCE
+#endif
+#include <unistd.h>
+#include <sys/types.h>
+
+#include <osmocom/core/thread.h>
+
+/*! Wrapper around Linux's gettid() to make it easily accessible on different system versions.
+ * If the gettid() API cannot be found, it will use the syscall directly if
+ * available. If no syscall is found available, then getpid() is called as
+ * fallback. See 'man 2 gettid' for further and details information.
+ * \returns This call is always successful and returns returns the thread ID of
+ * the calling thread (or the process ID of the current process if
+ * gettid() or its syscall are unavailable in the system).
+ */
+pid_t osmo_gettid(void)
+{
+#if HAVE_GETTID
+ return gettid();
+#elif defined(LINUX) && defined(__NR_gettid)
+ return (pid_t) syscall(__NR_gettid);
+#else
+ #pragma message ("use pid as tid")
+ return getpid();
+#endif
+}