blob: 6ec5c91d2e864f4346459e5f7188b7e4fb9a25e9 [file] [log] [blame]
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:441/*
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 04:47:3113#include "rtc_base/thread_checker_impl.h"
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:4414
Mirko Bonadei92ea95e2017-09-15 04:47:3115#include "rtc_base/platform_thread.h"
pbos12411ef2015-11-23 22:47:5616
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:4417namespace rtc {
tommi@webrtc.orgd43bdf52015-02-03 16:29:5718
tommi@webrtc.org2eb16602015-02-07 19:17:4719ThreadCheckerImpl::ThreadCheckerImpl() : valid_thread_(CurrentThreadRef()) {
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:4420}
21
22ThreadCheckerImpl::~ThreadCheckerImpl() {
23}
24
25bool ThreadCheckerImpl::CalledOnValidThread() const {
tommi@webrtc.org2eb16602015-02-07 19:17:4726 const PlatformThreadRef current_thread = CurrentThreadRef();
tommi@webrtc.orgb5a12522015-02-06 15:39:0527 CritScope scoped_lock(&lock_);
tommi@webrtc.org04cd4662015-01-26 15:27:2928 if (!valid_thread_) // Set if previously detached.
29 valid_thread_ = current_thread;
tommi@webrtc.org2eb16602015-02-07 19:17:4730 return IsThreadRefEqual(valid_thread_, current_thread);
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:4431}
32
33void ThreadCheckerImpl::DetachFromThread() {
34 CritScope scoped_lock(&lock_);
tommi@webrtc.org04cd4662015-01-26 15:27:2935 valid_thread_ = 0;
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:4436}
37
38} // namespace rtc