blob: cca50c191c14c76710ae678d5179d9217160ed8a [file] [log] [blame]
Mark Salyzyn12717162014-04-29 15:49:14 -07001/*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08002** Copyright (C) 2007, The Android Open Source Project
3**
Yabin Cui4a6e5a32015-01-26 19:48:54 -08004** Licensed under the Apache License, Version 2.0 (the "License");
5** you may not use this file except in compliance with the License.
6** You may obtain a copy of the License at
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08007**
Yabin Cui4a6e5a32015-01-26 19:48:54 -08008** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08009**
Yabin Cui4a6e5a32015-01-26 19:48:54 -080010** Unless required by applicable law or agreed to in writing, software
11** distributed under the License is distributed on an "AS IS" BASIS,
12** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13** See the License for the specific language governing permissions and
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080014** limitations under the License.
15*/
Mark Salyzyn12717162014-04-29 15:49:14 -070016
Tomasz Wasilczyk21a07162023-07-25 12:57:45 -070017#include <sys/types.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080018
Dan Albert7dfb61d2015-03-20 13:46:28 -070019#if defined(__APPLE__)
Tomasz Wasilczyk21a07162023-07-25 12:57:45 -070020#include <pthread.h>
Christopher Ferrisfc3576f2015-03-23 21:35:56 -070021#include <stdint.h>
Elliott Hughesdcf81842020-12-07 10:54:53 -080022#elif defined(__linux__)
Tomasz Wasilczyk21a07162023-07-25 12:57:45 -070023#include <pthread.h>
Dan Albert7dfb61d2015-03-20 13:46:28 -070024#include <syscall.h>
25#include <unistd.h>
26#elif defined(_WIN32)
Dan Albertb3a36ca2015-04-29 17:13:32 -070027#include <windows.h>
Dan Albert7dfb61d2015-03-20 13:46:28 -070028#endif
29
Hao Chen253445c2023-07-18 18:46:34 +000030#if defined(__BIONIC__) || defined(__GLIBC__) && __GLIBC_MINOR__ >= 30
Dan Albertb3a36ca2015-04-29 17:13:32 -070031// No definition needed for Android because we'll just pick up bionic's copy.
Hao Chen253445c2023-07-18 18:46:34 +000032// No definition needed for Glibc >= 2.30 because it exposes its own copy.
Elliott Hughesdcf81842020-12-07 10:54:53 -080033#else
Tomasz Wasilczyk21a07162023-07-25 12:57:45 -070034extern "C" pid_t gettid() {
Dan Albertb3a36ca2015-04-29 17:13:32 -070035#if defined(__APPLE__)
Christopher N. Hesse684b4422016-09-17 18:29:03 +020036 uint64_t tid;
37 pthread_threadid_np(NULL, &tid);
38 return tid;
Dan Albertb3a36ca2015-04-29 17:13:32 -070039#elif defined(__linux__)
40 return syscall(__NR_gettid);
41#elif defined(_WIN32)
42 return GetCurrentThreadId();
43#endif
44}
Elliott Hughes06757022020-12-02 11:21:14 -080045#endif