MySQL 9.0.0
Source Code Documentation
my_thread_os_id.h
Go to the documentation of this file.
1/* Copyright (c) 2015, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24/**
25 @file include/my_thread_os_id.h
26 Portable wrapper for gettid().
27*/
28
29#ifndef MY_THREAD_OS_ID_INCLUDED
30#define MY_THREAD_OS_ID_INCLUDED
31
32#include "my_config.h"
33#include "my_macros.h"
34#include "my_thread.h"
35#ifndef _WIN32
36#include <sys/syscall.h>
37#include <unistd.h>
38#endif
39
40#ifdef HAVE_PTHREAD_GETTHREADID_NP
41#include <pthread_np.h> /* pthread_getthreadid_np() */
42#endif /* HAVE_PTHREAD_GETTHREADID_NP */
43
44#ifdef HAVE_PTHREAD_THREADID_NP
45#include <pthread.h>
46#endif /* HAVE_PTHREAD_THREADID_NP */
47
48typedef unsigned long long my_thread_os_id_t;
49
50/**
51 Return the operating system thread id.
52 With Linux, threads have:
53 - an internal id, @c pthread_self(), visible in process
54 - an external id, @c gettid(), visible in the operating system,
55 for example with perf in linux.
56 This helper returns the underling operating system thread id.
57*/
59#ifdef HAVE_PTHREAD_THREADID_NP
60 /*
61 macOS.
62
63 Be careful to use this version first, and to not use SYS_gettid on macOS,
64 as SYS_gettid has a different meaning compared to linux gettid().
65 */
66 uint64_t tid64;
67 pthread_threadid_np(nullptr, &tid64);
68 return (pid_t)tid64;
69#else
70#ifdef HAVE_SYS_GETTID
71 /*
72 Linux.
73 See man gettid
74 See GLIBC Bug 6399 - gettid() should have a wrapper
75 https://sourceware.org/bugzilla/show_bug.cgi?id=6399
76 */
77 return syscall(SYS_gettid);
78#else
79#ifdef _WIN32
80 /* Windows */
81 return GetCurrentThreadId();
82#else
83#ifdef HAVE_PTHREAD_GETTHREADID_NP
84 /* FreeBSD 10.2 */
85 return pthread_getthreadid_np();
86#else
87#ifdef HAVE_INTEGER_PTHREAD_SELF
88 /* Unknown platform, fallback. */
89 return pthread_self();
90#else
91 /* Feature not available. */
92 return 0;
93#endif /* HAVE_INTEGER_PTHREAD_SELF */
94#endif /* HAVE_PTHREAD_GETTHREADID_NP */
95#endif /* _WIN32 */
96#endif /* HAVE_SYS_GETTID */
97#endif /* HAVE_SYS_THREAD_SELFID */
98}
99
100#endif /* MY_THREAD_OS_ID_INCLUDED */
Some common macros.
Defines to make different thread packages compatible.
unsigned long long my_thread_os_id_t
Definition: my_thread_os_id.h:48
static my_thread_os_id_t my_thread_os_id()
Return the operating system thread id.
Definition: my_thread_os_id.h:58