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