blob: 850f2ac3cf8eb69f0b67437026f792e4ae73bde9 [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
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000015namespace rtc {
tommi@webrtc.orgd43bdf52015-02-03 16:29:57 +000016
tommi@webrtc.org2eb16602015-02-07 19:17:47 +000017ThreadCheckerImpl::ThreadCheckerImpl() : valid_thread_(CurrentThreadRef()) {
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000018}
19
20ThreadCheckerImpl::~ThreadCheckerImpl() {
21}
22
23bool ThreadCheckerImpl::CalledOnValidThread() const {
tommi@webrtc.org2eb16602015-02-07 19:17:47 +000024 const PlatformThreadRef current_thread = CurrentThreadRef();
tommi@webrtc.orgb5a12522015-02-06 15:39:05 +000025 CritScope scoped_lock(&lock_);
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000026 if (!valid_thread_) // Set if previously detached.
27 valid_thread_ = current_thread;
tommi@webrtc.org2eb16602015-02-07 19:17:47 +000028 return IsThreadRefEqual(valid_thread_, current_thread);
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000029}
30
31void ThreadCheckerImpl::DetachFromThread() {
32 CritScope scoped_lock(&lock_);
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000033 valid_thread_ = 0;
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000034}
35
36} // namespace rtc