summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
Diffstat (limited to 'base')
-rw-r--r--base/Android.bp1
-rw-r--r--base/include/android-base/threads.h28
-rw-r--r--base/logging.cpp41
-rw-r--r--base/threads.cpp48
4 files changed, 81 insertions, 37 deletions
diff --git a/base/Android.bp b/base/Android.bp
index 5d70d47bd..7b0ba11d0 100644
--- a/base/Android.bp
+++ b/base/Android.bp
@@ -50,6 +50,7 @@ cc_defaults {
50 "quick_exit.cpp", 50 "quick_exit.cpp",
51 "stringprintf.cpp", 51 "stringprintf.cpp",
52 "strings.cpp", 52 "strings.cpp",
53 "threads.cpp",
53 "test_utils.cpp", 54 "test_utils.cpp",
54 ], 55 ],
55 56
diff --git a/base/include/android-base/threads.h b/base/include/android-base/threads.h
new file mode 100644
index 000000000..85e65ba65
--- /dev/null
+++ b/base/include/android-base/threads.h
@@ -0,0 +1,28 @@
1/*
2 * Copyright (C) 2018 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#ifndef ANDROID_BASE_THREADS_H
18#define ANDROID_BASE_THREADS_H
19
20#include <stdint.h>
21
22namespace android {
23namespace base {
24uint64_t GetThreadId();
25}
26} // namespace android
27
28#endif
diff --git a/base/logging.cpp b/base/logging.cpp
index a31feefab..30d7f8d57 100644
--- a/base/logging.cpp
+++ b/base/logging.cpp
@@ -21,6 +21,7 @@
21#include "android-base/logging.h" 21#include "android-base/logging.h"
22 22
23#include <fcntl.h> 23#include <fcntl.h>
24#include <inttypes.h>
24#include <libgen.h> 25#include <libgen.h>
25#include <time.h> 26#include <time.h>
26 27
@@ -54,41 +55,7 @@
54 55
55#include <android-base/macros.h> 56#include <android-base/macros.h>
56#include <android-base/strings.h> 57#include <android-base/strings.h>
57 58#include <android-base/threads.h>
58// For gettid.
59#if defined(__APPLE__)
60#include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED
61#include <stdint.h>
62#include <stdlib.h>
63#include <sys/syscall.h>
64#include <sys/time.h>
65#include <unistd.h>
66#elif defined(__linux__) && !defined(__ANDROID__)
67#include <syscall.h>
68#include <unistd.h>
69#elif defined(_WIN32)
70#include <windows.h>
71#endif
72
73#if defined(_WIN32)
74typedef uint32_t thread_id;
75#else
76typedef pid_t thread_id;
77#endif
78
79static thread_id GetThreadId() {
80#if defined(__BIONIC__)
81 return gettid();
82#elif defined(__APPLE__)
83 uint64_t tid;
84 pthread_threadid_np(NULL, &tid);
85 return tid;
86#elif defined(__linux__)
87 return syscall(__NR_gettid);
88#elif defined(_WIN32)
89 return GetCurrentThreadId();
90#endif
91}
92 59
93namespace { 60namespace {
94#if defined(__GLIBC__) 61#if defined(__GLIBC__)
@@ -223,8 +190,8 @@ void StderrLogger(LogId, LogSeverity severity, const char* tag, const char* file
223 static_assert(arraysize(log_characters) - 1 == FATAL + 1, 190 static_assert(arraysize(log_characters) - 1 == FATAL + 1,
224 "Mismatch in size of log_characters and values in LogSeverity"); 191 "Mismatch in size of log_characters and values in LogSeverity");
225 char severity_char = log_characters[severity]; 192 char severity_char = log_characters[severity];
226 fprintf(stderr, "%s %c %s %5d %5d %s:%u] %s\n", tag ? tag : "nullptr", severity_char, timestamp, 193 fprintf(stderr, "%s %c %s %5d %5" PRIu64 " %s:%u] %s\n", tag ? tag : "nullptr", severity_char,
227 getpid(), GetThreadId(), file, line, message); 194 timestamp, getpid(), GetThreadId(), file, line, message);
228} 195}
229 196
230void DefaultAborter(const char* abort_message) { 197void DefaultAborter(const char* abort_message) {
diff --git a/base/threads.cpp b/base/threads.cpp
new file mode 100644
index 000000000..a71382bc5
--- /dev/null
+++ b/base/threads.cpp
@@ -0,0 +1,48 @@
1/*
2 * Copyright (C) 2018 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 <android-base/threads.h>
18
19#include <stdint.h>
20#include <unistd.h>
21
22#if defined(__APPLE__)
23#include <pthread.h>
24#elif defined(__linux__) && !defined(__ANDROID__)
25#include <syscall.h>
26#elif defined(_WIN32)
27#include <windows.h>
28#endif
29
30namespace android {
31namespace base {
32
33uint64_t GetThreadId() {
34#if defined(__BIONIC__)
35 return gettid();
36#elif defined(__APPLE__)
37 uint64_t tid;
38 pthread_threadid_np(NULL, &tid);
39 return tid;
40#elif defined(__linux__)
41 return syscall(__NR_gettid);
42#elif defined(_WIN32)
43 return GetCurrentThreadId();
44#endif
45}
46
47} // namespace base
48} // namespace android