blob: 6ec5c91d2e864f4346459e5f7188b7e4fb9a25e9 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/thread_checker_impl.h"
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000014
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/platform_thread.h"
pbos12411ef2015-11-23 14:47:56 -080016
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000017namespace rtc {
tommi@webrtc.orgd43bdf52015-02-03 16:29:57 +000018
tommi@webrtc.org2eb16602015-02-07 19:17:47 +000019ThreadCheckerImpl::ThreadCheckerImpl() : valid_thread_(CurrentThreadRef()) {
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000020}
21
22ThreadCheckerImpl::~ThreadCheckerImpl() {
23}
24
25bool ThreadCheckerImpl::CalledOnValidThread() const {
tommi@webrtc.org2eb16602015-02-07 19:17:47 +000026 const PlatformThreadRef current_thread = CurrentThreadRef();
tommi@webrtc.orgb5a12522015-02-06 15:39:05 +000027 CritScope scoped_lock(&lock_);
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000028 if (!valid_thread_) // Set if previously detached.
29 valid_thread_ = current_thread;
tommi@webrtc.org2eb16602015-02-07 19:17:47 +000030 return IsThreadRefEqual(valid_thread_, current_thread);
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000031}
32
33void ThreadCheckerImpl::DetachFromThread() {
34 CritScope scoped_lock(&lock_);
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000035 valid_thread_ = 0;
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000036}
37
38} // namespace rtc