Tommi | bebc690 | 2015-05-18 09:51:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/base/platform_thread.h" |
| 12 | |
Tommi | ea14f0a | 2015-05-18 13:51:06 +0200 | [diff] [blame] | 13 | #include <string.h> |
| 14 | |
Tommi | bebc690 | 2015-05-18 09:51:42 +0200 | [diff] [blame] | 15 | #include "webrtc/base/checks.h" |
| 16 | |
| 17 | #if defined(WEBRTC_LINUX) |
Tommi | ea14f0a | 2015-05-18 13:51:06 +0200 | [diff] [blame] | 18 | #include <sys/prctl.h> |
Tommi | bebc690 | 2015-05-18 09:51:42 +0200 | [diff] [blame] | 19 | #include <sys/syscall.h> |
| 20 | #endif |
| 21 | |
| 22 | namespace rtc { |
| 23 | |
| 24 | PlatformThreadId CurrentThreadId() { |
| 25 | PlatformThreadId ret; |
| 26 | #if defined(WEBRTC_WIN) |
| 27 | ret = GetCurrentThreadId(); |
| 28 | #elif defined(WEBRTC_POSIX) |
| 29 | #if defined(WEBRTC_MAC) || defined(WEBRTC_IOS) |
| 30 | ret = pthread_mach_thread_np(pthread_self()); |
| 31 | #elif defined(WEBRTC_LINUX) |
| 32 | ret = syscall(__NR_gettid); |
| 33 | #elif defined(WEBRTC_ANDROID) |
| 34 | ret = gettid(); |
| 35 | #else |
| 36 | // Default implementation for nacl and solaris. |
| 37 | ret = reinterpret_cast<pid_t>(pthread_self()); |
| 38 | #endif |
| 39 | #endif // defined(WEBRTC_POSIX) |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame^] | 40 | RTC_DCHECK(ret); |
Tommi | bebc690 | 2015-05-18 09:51:42 +0200 | [diff] [blame] | 41 | return ret; |
| 42 | } |
| 43 | |
| 44 | PlatformThreadRef CurrentThreadRef() { |
| 45 | #if defined(WEBRTC_WIN) |
| 46 | return GetCurrentThreadId(); |
| 47 | #elif defined(WEBRTC_POSIX) |
| 48 | return pthread_self(); |
| 49 | #endif |
| 50 | } |
| 51 | |
| 52 | bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b) { |
| 53 | #if defined(WEBRTC_WIN) |
| 54 | return a == b; |
| 55 | #elif defined(WEBRTC_POSIX) |
| 56 | return pthread_equal(a, b); |
| 57 | #endif |
| 58 | } |
| 59 | |
Tommi | ea14f0a | 2015-05-18 13:51:06 +0200 | [diff] [blame] | 60 | void SetCurrentThreadName(const char* name) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame^] | 61 | RTC_DCHECK(strlen(name) < 64); |
Tommi | ea14f0a | 2015-05-18 13:51:06 +0200 | [diff] [blame] | 62 | #if defined(WEBRTC_WIN) |
| 63 | struct { |
| 64 | DWORD dwType; |
| 65 | LPCSTR szName; |
| 66 | DWORD dwThreadID; |
| 67 | DWORD dwFlags; |
| 68 | } threadname_info = {0x1000, name, static_cast<DWORD>(-1), 0}; |
| 69 | |
| 70 | __try { |
| 71 | ::RaiseException(0x406D1388, 0, sizeof(threadname_info) / sizeof(DWORD), |
| 72 | reinterpret_cast<ULONG_PTR*>(&threadname_info)); |
| 73 | } __except (EXCEPTION_EXECUTE_HANDLER) { |
| 74 | } |
| 75 | #elif defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID) |
| 76 | prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(name)); |
| 77 | #elif defined(WEBRTC_MAC) || defined(WEBRTC_IOS) |
| 78 | pthread_setname_np(name); |
| 79 | #endif |
| 80 | } |
| 81 | |
Tommi | bebc690 | 2015-05-18 09:51:42 +0200 | [diff] [blame] | 82 | } // namespace rtc |