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 | |
| 13 | #include "webrtc/base/checks.h" |
| 14 | |
| 15 | #if defined(WEBRTC_LINUX) |
| 16 | #include <sys/syscall.h> |
| 17 | #endif |
| 18 | |
| 19 | namespace rtc { |
| 20 | |
| 21 | PlatformThreadId CurrentThreadId() { |
| 22 | PlatformThreadId ret; |
| 23 | #if defined(WEBRTC_WIN) |
| 24 | ret = GetCurrentThreadId(); |
| 25 | #elif defined(WEBRTC_POSIX) |
| 26 | #if defined(WEBRTC_MAC) || defined(WEBRTC_IOS) |
| 27 | ret = pthread_mach_thread_np(pthread_self()); |
| 28 | #elif defined(WEBRTC_LINUX) |
| 29 | ret = syscall(__NR_gettid); |
| 30 | #elif defined(WEBRTC_ANDROID) |
| 31 | ret = gettid(); |
| 32 | #else |
| 33 | // Default implementation for nacl and solaris. |
| 34 | ret = reinterpret_cast<pid_t>(pthread_self()); |
| 35 | #endif |
| 36 | #endif // defined(WEBRTC_POSIX) |
| 37 | DCHECK(ret); |
| 38 | return ret; |
| 39 | } |
| 40 | |
| 41 | PlatformThreadRef CurrentThreadRef() { |
| 42 | #if defined(WEBRTC_WIN) |
| 43 | return GetCurrentThreadId(); |
| 44 | #elif defined(WEBRTC_POSIX) |
| 45 | return pthread_self(); |
| 46 | #endif |
| 47 | } |
| 48 | |
| 49 | bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b) { |
| 50 | #if defined(WEBRTC_WIN) |
| 51 | return a == b; |
| 52 | #elif defined(WEBRTC_POSIX) |
| 53 | return pthread_equal(a, b); |
| 54 | #endif |
| 55 | } |
| 56 | |
| 57 | } // namespace rtc |