<p>laforge <strong>submitted</strong> this change.</p><p><a href="https://gerrit.osmocom.org/c/libosmocore/+/22949">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Jenkins Builder: Verified
  fixeria: Looks good to me, but someone else must approve
  laforge: Looks good to me, approved

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">Introduce osmo_gettid() API<br><br>This API wraps conventional gettid() linux-specific API, which even in<br>Linux itself is sometimes not properly supported/announced.<br><br>This API also allows future porting to other platforms if needed, and so<br>far falls back to getpid() if no gettid(9 can be found.<br><br>Code ported from osmo-trx.git, see commit 7a07de1efd4eb7cc11c33d3ad25cb2df70aa1ef1.<br><br>Related: OS#5027<br>Change-Id: Id7534beeb22fcd50813dab76dd68818e2ff87ec2<br>---<br>M configure.ac<br>M include/Makefile.am<br>A include/osmocom/core/thread.h<br>M src/Makefile.am<br>A src/thread.c<br>5 files changed, 105 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/configure.ac b/configure.ac</span><br><span>index 2a6a180..f5af0d0 100644</span><br><span>--- a/configure.ac</span><br><span>+++ b/configure.ac</span><br><span>@@ -134,6 +134,16 @@</span><br><span> </span><br><span> CHECK_TM_INCLUDES_TM_GMTOFF</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+# Check if gettid is available (despite not being documented in glibc doc,</span><br><span style="color: hsl(120, 100%, 40%);">+# it requires __USE_GNU on some systems)</span><br><span style="color: hsl(120, 100%, 40%);">+# C compiler is used since __USE_GNU seems to be always defined for g++.</span><br><span style="color: hsl(120, 100%, 40%);">+save_CPPFLAGS=$CPPFLAGS</span><br><span style="color: hsl(120, 100%, 40%);">+AC_LANG_PUSH(C)</span><br><span style="color: hsl(120, 100%, 40%);">+CPPFLAGS="$CPPFLAGS -D_GNU_SOURCE"</span><br><span style="color: hsl(120, 100%, 40%);">+AC_CHECK_FUNCS([gettid])</span><br><span style="color: hsl(120, 100%, 40%);">+AC_LANG_POP(C)</span><br><span style="color: hsl(120, 100%, 40%);">+CPPFLAGS=$save_CPPFLAGS</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> dnl Check if We need to apply workaround for TLS bug on ARM platform for GCC < 7.3.0:</span><br><span> ARG_ENABLE_DETECT_TLS_GCC_ARM_BUG</span><br><span> </span><br><span>diff --git a/include/Makefile.am b/include/Makefile.am</span><br><span>index 8dae3d9..f0742d5 100644</span><br><span>--- a/include/Makefile.am</span><br><span>+++ b/include/Makefile.am</span><br><span>@@ -53,6 +53,7 @@</span><br><span>                        osmocom/core/strrb.h \</span><br><span>                        osmocom/core/talloc.h \</span><br><span>                        osmocom/core/tdef.h \</span><br><span style="color: hsl(120, 100%, 40%);">+                   osmocom/core/thread.h \</span><br><span>                        osmocom/core/timer.h \</span><br><span>                        osmocom/core/timer_compat.h \</span><br><span>                        osmocom/core/utils.h \</span><br><span>diff --git a/include/osmocom/core/thread.h b/include/osmocom/core/thread.h</span><br><span>new file mode 100644</span><br><span>index 0000000..40b5436</span><br><span>--- /dev/null</span><br><span>+++ b/include/osmocom/core/thread.h</span><br><span>@@ -0,0 +1,33 @@</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \file thread.h</span><br><span style="color: hsl(120, 100%, 40%);">+ *  Compatibility header with some thread related helpers</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+ * (C) 2021 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de></span><br><span style="color: hsl(120, 100%, 40%);">+ * All Rights Reserved</span><br><span style="color: hsl(120, 100%, 40%);">+ * Author: Pau Espin Pedrol <pespin@sysmocom.de></span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is free software; you can redistribute it and/or modify</span><br><span style="color: hsl(120, 100%, 40%);">+ * it under the terms of the GNU General Public License as published by</span><br><span style="color: hsl(120, 100%, 40%);">+ * the Free Software Foundation; either version 2 of the License, or</span><br><span style="color: hsl(120, 100%, 40%);">+ * (at your option) any later version.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is distributed in the hope that it will be useful,</span><br><span style="color: hsl(120, 100%, 40%);">+ * but WITHOUT ANY WARRANTY; without even the implied warranty of</span><br><span style="color: hsl(120, 100%, 40%);">+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span><br><span style="color: hsl(120, 100%, 40%);">+ * GNU General Public License for more details.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * You should have received a copy of the GNU General Public License along</span><br><span style="color: hsl(120, 100%, 40%);">+ * with this program; if not, write to the Free Software Foundation, Inc.,</span><br><span style="color: hsl(120, 100%, 40%);">+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \defgroup thread Osmocom thread helpers</span><br><span style="color: hsl(120, 100%, 40%);">+ *  @{</span><br><span style="color: hsl(120, 100%, 40%);">+ * \file thread.h */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#pragma once</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#include <sys/types.h></span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+pid_t osmo_gettid(void);</span><br><span>diff --git a/src/Makefile.am b/src/Makefile.am</span><br><span>index a2566a3..e56b57e 100644</span><br><span>--- a/src/Makefile.am</span><br><span>+++ b/src/Makefile.am</span><br><span>@@ -25,6 +25,7 @@</span><br><span>                         conv_acc.c conv_acc_generic.c sercomm.c prbs.c \</span><br><span>                     isdnhdlc.c \</span><br><span>                         tdef.c \</span><br><span style="color: hsl(120, 100%, 40%);">+                      thread.c \</span><br><span>                   sockaddr_str.c \</span><br><span>                     use_count.c \</span><br><span>                        exec.c \</span><br><span>diff --git a/src/thread.c b/src/thread.c</span><br><span>new file mode 100644</span><br><span>index 0000000..956fee7</span><br><span>--- /dev/null</span><br><span>+++ b/src/thread.c</span><br><span>@@ -0,0 +1,60 @@</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+ * (C) 2021 by sysmocom s.f.m.c. GmbH <info@sysmocom.de></span><br><span style="color: hsl(120, 100%, 40%);">+ * All Rights Reserved</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Author: Pau Espin Pedrol <pespin@sysmocom.de></span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is free software; you can redistribute it and/or modify</span><br><span style="color: hsl(120, 100%, 40%);">+ * it under the terms of the GNU General Public License as published by</span><br><span style="color: hsl(120, 100%, 40%);">+ * the Free Software Foundation; either version 2 of the License, or</span><br><span style="color: hsl(120, 100%, 40%);">+ * (at your option) any later version.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is distributed in the hope that it will be useful,</span><br><span style="color: hsl(120, 100%, 40%);">+ * but WITHOUT ANY WARRANTY; without even the implied warranty of</span><br><span style="color: hsl(120, 100%, 40%);">+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span><br><span style="color: hsl(120, 100%, 40%);">+ * GNU General Public License for more details.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * You should have received a copy of the GNU General Public License along</span><br><span style="color: hsl(120, 100%, 40%);">+ * with this program; if not, write to the Free Software Foundation, Inc.,</span><br><span style="color: hsl(120, 100%, 40%);">+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \addtogroup thread</span><br><span style="color: hsl(120, 100%, 40%);">+ *  @{</span><br><span style="color: hsl(120, 100%, 40%);">+ * \file thread.c</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! \file thread.c</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#include "config.h"</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* If HAVE_GETTID, then "_GNU_SOURCE" may need to be defined to use gettid() */</span><br><span style="color: hsl(120, 100%, 40%);">+#if HAVE_GETTID</span><br><span style="color: hsl(120, 100%, 40%);">+#define _GNU_SOURCE</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+#include <unistd.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <sys/types.h></span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/core/thread.h></span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! Wrapper around Linux's gettid() to make it easily accessible on different system versions.</span><br><span style="color: hsl(120, 100%, 40%);">+ * If the gettid() API cannot be found, it will use the syscall directly if</span><br><span style="color: hsl(120, 100%, 40%);">+ * available. If no syscall is found available, then getpid() is called as</span><br><span style="color: hsl(120, 100%, 40%);">+ * fallback. See 'man 2 gettid' for further and details information.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \returns This call is always successful and returns returns the thread ID of</span><br><span style="color: hsl(120, 100%, 40%);">+ *          the calling thread (or the process ID of the current process if</span><br><span style="color: hsl(120, 100%, 40%);">+ *          gettid() or its syscall are unavailable in the system).</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+pid_t osmo_gettid(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+#if HAVE_GETTID</span><br><span style="color: hsl(120, 100%, 40%);">+     return gettid();</span><br><span style="color: hsl(120, 100%, 40%);">+#elif defined(LINUX) && defined(__NR_gettid)</span><br><span style="color: hsl(120, 100%, 40%);">+        return (pid_t) syscall(__NR_gettid);</span><br><span style="color: hsl(120, 100%, 40%);">+#else</span><br><span style="color: hsl(120, 100%, 40%);">+   #pragma message ("use pid as tid")</span><br><span style="color: hsl(120, 100%, 40%);">+  return getpid();</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/libosmocore/+/22949">change 22949</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/c/libosmocore/+/22949"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: libosmocore </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: Id7534beeb22fcd50813dab76dd68818e2ff87ec2 </div>
<div style="display:none"> Gerrit-Change-Number: 22949 </div>
<div style="display:none"> Gerrit-PatchSet: 4 </div>
<div style="display:none"> Gerrit-Owner: pespin <pespin@sysmocom.de> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins Builder </div>
<div style="display:none"> Gerrit-Reviewer: fixeria <vyanitskiy@sysmocom.de> </div>
<div style="display:none"> Gerrit-Reviewer: laforge <laforge@osmocom.org> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>