blob: 5d59c99c10716a44ad925d7f449f1bcda7bd87eb [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)
tommi@webrtc.orgd43bdf52015-02-03 16:29:57 +000028#if defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
tommi@webrtc.orgb5a12522015-02-06 15:39:05 +000029 ret = pthread_mach_thread_np(pthread_self());
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000030#elif defined(WEBRTC_LINUX)
tommi@webrtc.orgb5a12522015-02-06 15:39:05 +000031 ret = syscall(__NR_gettid);
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000032#elif defined(WEBRTC_ANDROID)
tommi@webrtc.orgb5a12522015-02-06 15:39:05 +000033 ret = gettid();
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000034#else
35 // Default implementation for nacl and solaris.
tommi@webrtc.orgb5a12522015-02-06 15:39:05 +000036 ret = reinterpret_cast<pid_t>(pthread_self());
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000037#endif
38#endif // defined(WEBRTC_POSIX)
tommi@webrtc.orgb5a12522015-02-06 15:39:05 +000039 DCHECK(ret);
40 return ret;
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000041}
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000042
tommi@webrtc.org2eb16602015-02-07 19:17:47 +000043PlatformThreadRef CurrentThreadRef() {
44#if defined(WEBRTC_WIN)
45 return GetCurrentThreadId();
46#elif defined(WEBRTC_POSIX)
47 return pthread_self();
48#endif
49}
50
51bool 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
59ThreadCheckerImpl::ThreadCheckerImpl() : valid_thread_(CurrentThreadRef()) {
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000060}
61
62ThreadCheckerImpl::~ThreadCheckerImpl() {
63}
64
65bool ThreadCheckerImpl::CalledOnValidThread() const {
tommi@webrtc.org2eb16602015-02-07 19:17:47 +000066 const PlatformThreadRef current_thread = CurrentThreadRef();
tommi@webrtc.orgb5a12522015-02-06 15:39:05 +000067 CritScope scoped_lock(&lock_);
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000068 if (!valid_thread_) // Set if previously detached.
69 valid_thread_ = current_thread;
tommi@webrtc.org2eb16602015-02-07 19:17:47 +000070 return IsThreadRefEqual(valid_thread_, current_thread);
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000071}
72
73void ThreadCheckerImpl::DetachFromThread() {
74 CritScope scoped_lock(&lock_);
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000075 valid_thread_ = 0;
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000076}
77
78} // namespace rtc