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 | * |
Pau Espin Pedrol | 88e4058 | 2021-02-17 17:46:02 +0100 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | /*! \addtogroup thread |
| 20 | * @{ |
| 21 | * \file thread.c |
| 22 | */ |
| 23 | |
| 24 | /*! \file thread.c |
| 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | |
| 29 | /* If HAVE_GETTID, then "_GNU_SOURCE" may need to be defined to use gettid() */ |
| 30 | #if HAVE_GETTID |
| 31 | #define _GNU_SOURCE |
| 32 | #endif |
| 33 | #include <unistd.h> |
| 34 | #include <sys/types.h> |
| 35 | |
| 36 | #include <osmocom/core/thread.h> |
| 37 | |
| 38 | /*! Wrapper around Linux's gettid() to make it easily accessible on different system versions. |
| 39 | * If the gettid() API cannot be found, it will use the syscall directly if |
| 40 | * available. If no syscall is found available, then getpid() is called as |
| 41 | * fallback. See 'man 2 gettid' for further and details information. |
| 42 | * \returns This call is always successful and returns returns the thread ID of |
| 43 | * the calling thread (or the process ID of the current process if |
| 44 | * gettid() or its syscall are unavailable in the system). |
| 45 | */ |
| 46 | pid_t osmo_gettid(void) |
| 47 | { |
| 48 | #if HAVE_GETTID |
| 49 | return gettid(); |
| 50 | #elif defined(LINUX) && defined(__NR_gettid) |
| 51 | return (pid_t) syscall(__NR_gettid); |
| 52 | #else |
| 53 | #pragma message ("use pid as tid") |
| 54 | return getpid(); |
| 55 | #endif |
| 56 | } |