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) |
| 28 | // Pthreads doesn't have the concept of a thread ID, so we have to reach down |
| 29 | // into the kernel. |
tommi@webrtc.org | d43bdf5 | 2015-02-03 16:29:57 +0000 | [diff] [blame] | 30 | #if defined(WEBRTC_MAC) || defined(WEBRTC_IOS) |
tommi@webrtc.org | b5a1252 | 2015-02-06 15:39:05 +0000 | [diff] [blame] | 31 | ret = pthread_mach_thread_np(pthread_self()); |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 32 | #elif defined(WEBRTC_LINUX) |
tommi@webrtc.org | b5a1252 | 2015-02-06 15:39:05 +0000 | [diff] [blame] | 33 | ret = syscall(__NR_gettid); |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 34 | #elif defined(WEBRTC_ANDROID) |
tommi@webrtc.org | b5a1252 | 2015-02-06 15:39:05 +0000 | [diff] [blame] | 35 | ret = gettid(); |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 36 | #else |
| 37 | // Default implementation for nacl and solaris. |
tommi@webrtc.org | b5a1252 | 2015-02-06 15:39:05 +0000 | [diff] [blame] | 38 | ret = reinterpret_cast<pid_t>(pthread_self()); |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 39 | #endif |
| 40 | #endif // defined(WEBRTC_POSIX) |
tommi@webrtc.org | b5a1252 | 2015-02-06 15:39:05 +0000 | [diff] [blame] | 41 | DCHECK(ret); |
| 42 | return ret; |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 43 | } |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 44 | |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 45 | ThreadCheckerImpl::ThreadCheckerImpl() : valid_thread_(CurrentThreadId()) { |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | ThreadCheckerImpl::~ThreadCheckerImpl() { |
| 49 | } |
| 50 | |
| 51 | bool ThreadCheckerImpl::CalledOnValidThread() const { |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 52 | const PlatformThreadId current_thread = CurrentThreadId(); |
tommi@webrtc.org | b5a1252 | 2015-02-06 15:39:05 +0000 | [diff] [blame] | 53 | CritScope scoped_lock(&lock_); |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 54 | if (!valid_thread_) // Set if previously detached. |
| 55 | valid_thread_ = current_thread; |
tommi@webrtc.org | b5a1252 | 2015-02-06 15:39:05 +0000 | [diff] [blame] | 56 | #if defined(WEBRTC_MAC) || defined(WEBRTC_IOS) |
| 57 | // TODO(tommi): Remove this hack after we've figured out the roll issue |
| 58 | // with chromium's Mac 10.9 debug bot. |
| 59 | if (valid_thread_ != current_thread) { |
| 60 | // At the moment, this file cannot use logging from either webrtc or |
| 61 | // libjingle. :( |
tommi@webrtc.org | 13a0e18 | 2015-02-06 16:38:58 +0000 | [diff] [blame^] | 62 | fprintf(stderr, "*** WRONG THREAD *** current=%i vs valid=%i\n", |
tommi@webrtc.org | b5a1252 | 2015-02-06 15:39:05 +0000 | [diff] [blame] | 63 | current_thread, valid_thread_); |
| 64 | } |
| 65 | return true; // le sigh. |
| 66 | #else |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 67 | return valid_thread_ == current_thread; |
tommi@webrtc.org | b5a1252 | 2015-02-06 15:39:05 +0000 | [diff] [blame] | 68 | #endif |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void ThreadCheckerImpl::DetachFromThread() { |
| 72 | CritScope scoped_lock(&lock_); |
tommi@webrtc.org | 04cd466 | 2015-01-26 15:27:29 +0000 | [diff] [blame] | 73 | valid_thread_ = 0; |
henrik.lundin@webrtc.org | 1e3c5c2 | 2014-06-16 11:34:44 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | } // namespace rtc |