1 /*
2 ** Copyright (C) 2007, The Android Open Source Project
3 **
4 ** 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
7 **
8 **     http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** 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
14 ** limitations under the License.
15 */
16 
17 #include <cutils/threads.h>
18 
19 #if defined(__APPLE__)
20 #include <stdint.h>
21 #elif defined(__linux__)
22 #include <syscall.h>
23 #include <unistd.h>
24 #elif defined(_WIN32)
25 #include <windows.h>
26 #endif
27 
28 #if defined(__BIONIC__) || defined(__GLIBC__) && __GLIBC_MINOR__ >= 32
29 // No definition needed for Android because we'll just pick up bionic's copy.
30 // No definition needed for Glibc >= 2.32 because it exposes its own copy.
31 #else
gettid()32 pid_t gettid() {
33 #if defined(__APPLE__)
34   uint64_t tid;
35   pthread_threadid_np(NULL, &tid);
36   return tid;
37 #elif defined(__linux__)
38   return syscall(__NR_gettid);
39 #elif defined(_WIN32)
40   return GetCurrentThreadId();
41 #endif
42 }
43 #endif
44