blob: 9ff2a97c1e0ed9ff2448c95d0a9e5a5fab0c0c61 [file] [log] [blame]
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +00001/*
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.org04cd4662015-01-26 15:27:29 +000015#include "webrtc/base/checks.h"
16
17#if defined(WEBRTC_LINUX)
18#include <sys/syscall.h>
19#endif
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000020
21namespace rtc {
tommi@webrtc.orgd43bdf52015-02-03 16:29:57 +000022
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000023PlatformThreadId CurrentThreadId() {
tommi@webrtc.orgb5a12522015-02-06 15:39:05 +000024 PlatformThreadId ret;
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000025#if defined(WEBRTC_WIN)
tommi@webrtc.orgb5a12522015-02-06 15:39:05 +000026 ret = GetCurrentThreadId();
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000027#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.orgd43bdf52015-02-03 16:29:57 +000030#if defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
tommi@webrtc.orgb5a12522015-02-06 15:39:05 +000031 ret = pthread_mach_thread_np(pthread_self());
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000032#elif defined(WEBRTC_LINUX)
tommi@webrtc.orgb5a12522015-02-06 15:39:05 +000033 ret = syscall(__NR_gettid);
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000034#elif defined(WEBRTC_ANDROID)
tommi@webrtc.orgb5a12522015-02-06 15:39:05 +000035 ret = gettid();
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000036#else
37 // Default implementation for nacl and solaris.
tommi@webrtc.orgb5a12522015-02-06 15:39:05 +000038 ret = reinterpret_cast<pid_t>(pthread_self());
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000039#endif
40#endif // defined(WEBRTC_POSIX)
tommi@webrtc.orgb5a12522015-02-06 15:39:05 +000041 DCHECK(ret);
42 return ret;
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000043}
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000044
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000045ThreadCheckerImpl::ThreadCheckerImpl() : valid_thread_(CurrentThreadId()) {
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000046}
47
48ThreadCheckerImpl::~ThreadCheckerImpl() {
49}
50
51bool ThreadCheckerImpl::CalledOnValidThread() const {
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000052 const PlatformThreadId current_thread = CurrentThreadId();
tommi@webrtc.orgb5a12522015-02-06 15:39:05 +000053 CritScope scoped_lock(&lock_);
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000054 if (!valid_thread_) // Set if previously detached.
55 valid_thread_ = current_thread;
tommi@webrtc.orgb5a12522015-02-06 15:39:05 +000056#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. :(
62 printf("*** WRONG THREAD *** current=%i vs valid=%i\n",
63 current_thread, valid_thread_);
64 }
65 return true; // le sigh.
66#else
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000067 return valid_thread_ == current_thread;
tommi@webrtc.orgb5a12522015-02-06 15:39:05 +000068#endif
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000069}
70
71void ThreadCheckerImpl::DetachFromThread() {
72 CritScope scoped_lock(&lock_);
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000073 valid_thread_ = 0;
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000074}
75
76} // namespace rtc