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() { |
| 24 | #if defined(WEBRTC_WIN) |
| 25 | return GetCurrentThreadId(); |
| 26 | #elif defined(WEBRTC_POSIX) |
| 27 | // Pthreads doesn't have the concept of a thread ID, so we have to reach down |
| 28 | // into the kernel. |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 29 | #if defined(WEBRTC_MAC) || defined(WEBRTC_IOS) |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 30 | return pthread_mach_thread_np(pthread_self()); |
| 31 | #elif defined(WEBRTC_LINUX) |
| 32 | return syscall(__NR_gettid); |
| 33 | #elif defined(WEBRTC_ANDROID) |
| 34 | return gettid(); |
| 35 | #else |
| 36 | // Default implementation for nacl and solaris. |
| 37 | return reinterpret_cast<pid_t>(pthread_self()); |
| 38 | #endif |
| 39 | #endif // defined(WEBRTC_POSIX) |
| 40 | } |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 41 | |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 42 | ThreadCheckerImpl::ThreadCheckerImpl() : valid_thread_(CurrentThreadId()) { |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | ThreadCheckerImpl::~ThreadCheckerImpl() { |
| 46 | } |
| 47 | |
| 48 | bool ThreadCheckerImpl::CalledOnValidThread() const { |
| 49 | CritScope scoped_lock(&lock_); |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 50 | const PlatformThreadId current_thread = CurrentThreadId(); |
| 51 | if (!valid_thread_) // Set if previously detached. |
| 52 | valid_thread_ = current_thread; |
| 53 | return valid_thread_ == current_thread; |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void ThreadCheckerImpl::DetachFromThread() { |
| 57 | CritScope scoped_lock(&lock_); |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 58 | valid_thread_ = 0; |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | } // namespace rtc |