Pau Espin Pedrol | 88e4058 | 2021-02-17 17:46:02 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2021 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * Author: Pau Espin Pedrol <pespin@sysmocom.de> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | /*! \addtogroup thread |
| 24 | * @{ |
| 25 | * \file thread.c |
| 26 | */ |
| 27 | |
| 28 | /*! \file thread.c |
| 29 | */ |
| 30 | |
| 31 | #include "config.h" |
| 32 | |
| 33 | /* If HAVE_GETTID, then "_GNU_SOURCE" may need to be defined to use gettid() */ |
| 34 | #if HAVE_GETTID |
| 35 | #define _GNU_SOURCE |
| 36 | #endif |
| 37 | #include <unistd.h> |
| 38 | #include <sys/types.h> |
| 39 | |
| 40 | #include <osmocom/core/thread.h> |
| 41 | |
| 42 | /*! Wrapper around Linux's gettid() to make it easily accessible on different system versions. |
| 43 | * If the gettid() API cannot be found, it will use the syscall directly if |
| 44 | * available. If no syscall is found available, then getpid() is called as |
| 45 | * fallback. See 'man 2 gettid' for further and details information. |
| 46 | * \returns This call is always successful and returns returns the thread ID of |
| 47 | * the calling thread (or the process ID of the current process if |
| 48 | * gettid() or its syscall are unavailable in the system). |
| 49 | */ |
| 50 | pid_t osmo_gettid(void) |
| 51 | { |
| 52 | #if HAVE_GETTID |
| 53 | return gettid(); |
| 54 | #elif defined(LINUX) && defined(__NR_gettid) |
| 55 | return (pid_t) syscall(__NR_gettid); |
| 56 | #else |
| 57 | #pragma message ("use pid as tid") |
| 58 | return getpid(); |
| 59 | #endif |
| 60 | } |