henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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 | // Borrowed from Chromium's src/base/threading/thread_checker_impl.cc. |
| 12 | |
| 13 | #include "webrtc/base/thread_checker_impl.h" |
| 14 | |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 15 | #include "webrtc/base/checks.h" |
| 16 | |
| 17 | #if defined(WEBRTC_LINUX) |
| 18 | #include <sys/syscall.h> |
| 19 | #endif |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 20 | |
| 21 | namespace rtc { |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 22 | |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 23 | PlatformThreadId CurrentThreadId() { |
tommi@webrtc.org | b5a1252 | 2015-02-06 15:39:05 +0000 | [diff] [blame] | 24 | PlatformThreadId ret; |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 25 | #if defined(WEBRTC_WIN) |
tommi@webrtc.org | b5a1252 | 2015-02-06 15:39:05 +0000 | [diff] [blame] | 26 | ret = GetCurrentThreadId(); |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 27 | #elif defined(WEBRTC_POSIX) |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 28 | #if defined(WEBRTC_MAC) || defined(WEBRTC_IOS) |
tommi@webrtc.org | b5a1252 | 2015-02-06 15:39:05 +0000 | [diff] [blame] | 29 | ret = pthread_mach_thread_np(pthread_self()); |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 30 | #elif defined(WEBRTC_LINUX) |
tommi@webrtc.org | b5a1252 | 2015-02-06 15:39:05 +0000 | [diff] [blame] | 31 | ret = syscall(__NR_gettid); |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 32 | #elif defined(WEBRTC_ANDROID) |
tommi@webrtc.org | b5a1252 | 2015-02-06 15:39:05 +0000 | [diff] [blame] | 33 | ret = gettid(); |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 34 | #else |
| 35 | // Default implementation for nacl and solaris. |
tommi@webrtc.org | b5a1252 | 2015-02-06 15:39:05 +0000 | [diff] [blame] | 36 | ret = reinterpret_cast<pid_t>(pthread_self()); |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 37 | #endif |
| 38 | #endif // defined(WEBRTC_POSIX) |
tommi@webrtc.org | b5a1252 | 2015-02-06 15:39:05 +0000 | [diff] [blame] | 39 | DCHECK(ret); |
| 40 | return ret; |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 41 | } |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 42 | |
tommi@webrtc.org | 2eb1660 | 2015-02-07 19:17:47 +0000 | [diff] [blame^] | 43 | PlatformThreadRef CurrentThreadRef() { |
| 44 | #if defined(WEBRTC_WIN) |
| 45 | return GetCurrentThreadId(); |
| 46 | #elif defined(WEBRTC_POSIX) |
| 47 | return pthread_self(); |
| 48 | #endif |
| 49 | } |
| 50 | |
| 51 | bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b) { |
| 52 | #if defined(WEBRTC_WIN) |
| 53 | return a == b; |
| 54 | #elif defined(WEBRTC_POSIX) |
| 55 | return pthread_equal(a, b); |
| 56 | #endif |
| 57 | } |
| 58 | |
| 59 | ThreadCheckerImpl::ThreadCheckerImpl() : valid_thread_(CurrentThreadRef()) { |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | ThreadCheckerImpl::~ThreadCheckerImpl() { |
| 63 | } |
| 64 | |
| 65 | bool ThreadCheckerImpl::CalledOnValidThread() const { |
tommi@webrtc.org | 2eb1660 | 2015-02-07 19:17:47 +0000 | [diff] [blame^] | 66 | const PlatformThreadRef current_thread = CurrentThreadRef(); |
tommi@webrtc.org | b5a1252 | 2015-02-06 15:39:05 +0000 | [diff] [blame] | 67 | CritScope scoped_lock(&lock_); |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 68 | if (!valid_thread_) // Set if previously detached. |
| 69 | valid_thread_ = current_thread; |
tommi@webrtc.org | 2eb1660 | 2015-02-07 19:17:47 +0000 | [diff] [blame^] | 70 | return IsThreadRefEqual(valid_thread_, current_thread); |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void ThreadCheckerImpl::DetachFromThread() { |
| 74 | CritScope scoped_lock(&lock_); |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 75 | valid_thread_ = 0; |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | } // namespace rtc |