blob: a9e87c67a01eca3fa9fb8116f4d5b09c900841d0 [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
Yves Gerey665174f2018-06-19 15:03:05 +020017ThreadCheckerImpl::ThreadCheckerImpl() : valid_thread_(CurrentThreadRef()) {}
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000018
Yves Gerey665174f2018-06-19 15:03:05 +020019ThreadCheckerImpl::~ThreadCheckerImpl() {}
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000020
21bool ThreadCheckerImpl::CalledOnValidThread() const {
tommi@webrtc.org2eb16602015-02-07 19:17:47 +000022 const PlatformThreadRef current_thread = CurrentThreadRef();
tommi@webrtc.orgb5a12522015-02-06 15:39:05 +000023 CritScope scoped_lock(&lock_);
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000024 if (!valid_thread_) // Set if previously detached.
25 valid_thread_ = current_thread;
tommi@webrtc.org2eb16602015-02-07 19:17:47 +000026 return IsThreadRefEqual(valid_thread_, current_thread);
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000027}
28
29void ThreadCheckerImpl::DetachFromThread() {
30 CritScope scoped_lock(&lock_);
tommi@webrtc.org04cd4662015-01-26 15:27:29 +000031 valid_thread_ = 0;
henrik.lundin@webrtc.org1e3c5c22014-06-16 11:34:44 +000032}
33
34} // namespace rtc