Repo created
This commit is contained in:
parent
81b91f4139
commit
f8c34fa5ee
22732 changed files with 4815320 additions and 2 deletions
|
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
* Copyright 2020 The WebRTC Project Authors. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#ifndef RTC_BASE_SYNCHRONIZATION_MUTEX_H_
|
||||
#define RTC_BASE_SYNCHRONIZATION_MUTEX_H_
|
||||
|
||||
#include <atomic>
|
||||
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/base/const_init.h"
|
||||
#include "rtc_base/checks.h"
|
||||
#include "rtc_base/thread_annotations.h"
|
||||
|
||||
#if defined(WEBRTC_ABSL_MUTEX)
|
||||
#include "rtc_base/synchronization/mutex_abseil.h" // nogncheck
|
||||
#elif defined(WEBRTC_WIN)
|
||||
#include "rtc_base/synchronization/mutex_critical_section.h"
|
||||
#elif defined(WEBRTC_POSIX)
|
||||
#include "rtc_base/synchronization/mutex_pthread.h"
|
||||
#else
|
||||
#error Unsupported platform.
|
||||
#endif
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
// The Mutex guarantees exclusive access and aims to follow Abseil semantics
|
||||
// (i.e. non-reentrant etc).
|
||||
class RTC_LOCKABLE Mutex final {
|
||||
public:
|
||||
Mutex() = default;
|
||||
Mutex(const Mutex&) = delete;
|
||||
Mutex& operator=(const Mutex&) = delete;
|
||||
|
||||
void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION() { impl_.Lock(); }
|
||||
ABSL_MUST_USE_RESULT bool TryLock() RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true) {
|
||||
return impl_.TryLock();
|
||||
}
|
||||
// Return immediately if this thread holds the mutex, or RTC_DCHECK_IS_ON==0.
|
||||
// Otherwise, may report an error (typically by crashing with a diagnostic),
|
||||
// or may return immediately.
|
||||
void AssertHeld() const RTC_ASSERT_EXCLUSIVE_LOCK() { impl_.AssertHeld(); }
|
||||
void Unlock() RTC_UNLOCK_FUNCTION() { impl_.Unlock(); }
|
||||
|
||||
private:
|
||||
MutexImpl impl_;
|
||||
};
|
||||
|
||||
// MutexLock, for serializing execution through a scope.
|
||||
class RTC_SCOPED_LOCKABLE MutexLock final {
|
||||
public:
|
||||
MutexLock(const MutexLock&) = delete;
|
||||
MutexLock& operator=(const MutexLock&) = delete;
|
||||
|
||||
explicit MutexLock(Mutex* mutex) RTC_EXCLUSIVE_LOCK_FUNCTION(mutex)
|
||||
: mutex_(mutex) {
|
||||
mutex->Lock();
|
||||
}
|
||||
~MutexLock() RTC_UNLOCK_FUNCTION() { mutex_->Unlock(); }
|
||||
|
||||
private:
|
||||
Mutex* mutex_;
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // RTC_BASE_SYNCHRONIZATION_MUTEX_H_
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* Copyright 2020 The WebRTC Project Authors. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#ifndef RTC_BASE_SYNCHRONIZATION_MUTEX_ABSEIL_H_
|
||||
#define RTC_BASE_SYNCHRONIZATION_MUTEX_ABSEIL_H_
|
||||
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "rtc_base/thread_annotations.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
class RTC_LOCKABLE MutexImpl final {
|
||||
public:
|
||||
MutexImpl() = default;
|
||||
MutexImpl(const MutexImpl&) = delete;
|
||||
MutexImpl& operator=(const MutexImpl&) = delete;
|
||||
|
||||
void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION() { mutex_.Lock(); }
|
||||
ABSL_MUST_USE_RESULT bool TryLock() RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true) {
|
||||
return mutex_.TryLock();
|
||||
}
|
||||
void AssertHeld() const RTC_ASSERT_EXCLUSIVE_LOCK() {
|
||||
#if RTC_DCHECK_IS_ON
|
||||
mutex_.AssertHeld();
|
||||
#endif
|
||||
}
|
||||
void Unlock() RTC_UNLOCK_FUNCTION() { mutex_.Unlock(); }
|
||||
|
||||
private:
|
||||
absl::Mutex mutex_;
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // RTC_BASE_SYNCHRONIZATION_MUTEX_ABSEIL_H_
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* Copyright 2020 The WebRTC Project Authors. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#ifndef RTC_BASE_SYNCHRONIZATION_MUTEX_CRITICAL_SECTION_H_
|
||||
#define RTC_BASE_SYNCHRONIZATION_MUTEX_CRITICAL_SECTION_H_
|
||||
|
||||
#if defined(WEBRTC_WIN)
|
||||
// clang-format off
|
||||
// clang formating would change include order.
|
||||
|
||||
// Include winsock2.h before including <windows.h> to maintain consistency with
|
||||
// win32.h. To include win32.h directly, it must be broken out into its own
|
||||
// build target.
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
#include <sal.h> // must come after windows headers.
|
||||
// clang-format on
|
||||
|
||||
#include "absl/base/attributes.h"
|
||||
#include "rtc_base/thread_annotations.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
class RTC_LOCKABLE MutexImpl final {
|
||||
public:
|
||||
MutexImpl() { InitializeCriticalSection(&critical_section_); }
|
||||
MutexImpl(const MutexImpl&) = delete;
|
||||
MutexImpl& operator=(const MutexImpl&) = delete;
|
||||
~MutexImpl() { DeleteCriticalSection(&critical_section_); }
|
||||
|
||||
void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION() {
|
||||
EnterCriticalSection(&critical_section_);
|
||||
}
|
||||
ABSL_MUST_USE_RESULT bool TryLock() RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true) {
|
||||
return TryEnterCriticalSection(&critical_section_) != FALSE;
|
||||
}
|
||||
void AssertHeld() const RTC_ASSERT_EXCLUSIVE_LOCK() {}
|
||||
void Unlock() RTC_UNLOCK_FUNCTION() {
|
||||
LeaveCriticalSection(&critical_section_);
|
||||
}
|
||||
|
||||
private:
|
||||
CRITICAL_SECTION critical_section_;
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // #if defined(WEBRTC_WIN)
|
||||
#endif // RTC_BASE_SYNCHRONIZATION_MUTEX_CRITICAL_SECTION_H_
|
||||
|
|
@ -0,0 +1,101 @@
|
|||
/*
|
||||
* Copyright 2020 The WebRTC Project Authors. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#ifndef RTC_BASE_SYNCHRONIZATION_MUTEX_PTHREAD_H_
|
||||
#define RTC_BASE_SYNCHRONIZATION_MUTEX_PTHREAD_H_
|
||||
|
||||
#if defined(WEBRTC_POSIX)
|
||||
|
||||
#include <pthread.h>
|
||||
#if defined(WEBRTC_MAC)
|
||||
#include <pthread_spis.h>
|
||||
#endif
|
||||
|
||||
#include "absl/base/attributes.h"
|
||||
#include "rtc_base/system/no_unique_address.h"
|
||||
#include "rtc_base/thread_annotations.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
class RTC_LOCKABLE MutexImpl final {
|
||||
public:
|
||||
MutexImpl() {
|
||||
pthread_mutexattr_t mutex_attribute;
|
||||
pthread_mutexattr_init(&mutex_attribute);
|
||||
#if defined(WEBRTC_MAC)
|
||||
pthread_mutexattr_setpolicy_np(&mutex_attribute,
|
||||
_PTHREAD_MUTEX_POLICY_FIRSTFIT);
|
||||
#endif
|
||||
pthread_mutex_init(&mutex_, &mutex_attribute);
|
||||
pthread_mutexattr_destroy(&mutex_attribute);
|
||||
}
|
||||
MutexImpl(const MutexImpl&) = delete;
|
||||
MutexImpl& operator=(const MutexImpl&) = delete;
|
||||
~MutexImpl() { pthread_mutex_destroy(&mutex_); }
|
||||
|
||||
void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION() {
|
||||
pthread_mutex_lock(&mutex_);
|
||||
owner_.SetOwner();
|
||||
}
|
||||
ABSL_MUST_USE_RESULT bool TryLock() RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true) {
|
||||
if (pthread_mutex_trylock(&mutex_) != 0) {
|
||||
return false;
|
||||
}
|
||||
owner_.SetOwner();
|
||||
return true;
|
||||
}
|
||||
void AssertHeld() const RTC_ASSERT_EXCLUSIVE_LOCK() { owner_.AssertOwned(); }
|
||||
void Unlock() RTC_UNLOCK_FUNCTION() {
|
||||
owner_.ClearOwner();
|
||||
pthread_mutex_unlock(&mutex_);
|
||||
}
|
||||
|
||||
private:
|
||||
class OwnerRecord {
|
||||
public:
|
||||
#if !RTC_DCHECK_IS_ON
|
||||
void SetOwner() {}
|
||||
void ClearOwner() {}
|
||||
void AssertOwned() const {}
|
||||
#else
|
||||
void SetOwner() {
|
||||
latest_owner_ = pthread_self();
|
||||
is_owned_ = true;
|
||||
}
|
||||
void ClearOwner() { is_owned_ = false; }
|
||||
void AssertOwned() const {
|
||||
RTC_CHECK(is_owned_);
|
||||
RTC_CHECK(pthread_equal(latest_owner_, pthread_self()));
|
||||
}
|
||||
|
||||
private:
|
||||
// Use two separate primitive types, rather than absl::optional, since the
|
||||
// data race described below might invalidate absl::optional invariants.
|
||||
bool is_owned_ = false;
|
||||
pthread_t latest_owner_ = pthread_self();
|
||||
#endif
|
||||
};
|
||||
|
||||
pthread_mutex_t mutex_;
|
||||
// This record is modified only with the mutex held, and hence, calls to
|
||||
// AssertHeld where mutex is held are race-free and will always succeed.
|
||||
//
|
||||
// The failure case is more subtle: If AssertHeld is called from some thread
|
||||
// not holding the mutex, and RTC_DCHECK_IS_ON==1, we have a data race. It is
|
||||
// highly likely that the calling thread will see `is_owned_` false or
|
||||
// `latest_owner_` different from itself, and crash. But it may fail to crash,
|
||||
// and invoke some other undefined behavior (still, this race can happen only
|
||||
// when RTC_DCHECK_IS_ON==1).
|
||||
RTC_NO_UNIQUE_ADDRESS OwnerRecord owner_;
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
#endif // #if defined(WEBRTC_POSIX)
|
||||
#endif // RTC_BASE_SYNCHRONIZATION_MUTEX_PTHREAD_H_
|
||||
|
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
* Copyright 2019 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
#include "rtc_base/synchronization/sequence_checker_internal.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "rtc_base/checks.h"
|
||||
#include "rtc_base/strings/string_builder.h"
|
||||
|
||||
namespace webrtc {
|
||||
namespace webrtc_sequence_checker_internal {
|
||||
|
||||
SequenceCheckerImpl::SequenceCheckerImpl(bool attach_to_current_thread)
|
||||
: attached_(attach_to_current_thread),
|
||||
valid_thread_(rtc::CurrentThreadRef()),
|
||||
valid_queue_(TaskQueueBase::Current()) {}
|
||||
|
||||
SequenceCheckerImpl::SequenceCheckerImpl(TaskQueueBase* attached_queue)
|
||||
: attached_(attached_queue != nullptr),
|
||||
valid_thread_(rtc::PlatformThreadRef()),
|
||||
valid_queue_(attached_queue) {}
|
||||
|
||||
bool SequenceCheckerImpl::IsCurrent() const {
|
||||
const TaskQueueBase* const current_queue = TaskQueueBase::Current();
|
||||
const rtc::PlatformThreadRef current_thread = rtc::CurrentThreadRef();
|
||||
MutexLock scoped_lock(&lock_);
|
||||
if (!attached_) { // Previously detached.
|
||||
attached_ = true;
|
||||
valid_thread_ = current_thread;
|
||||
valid_queue_ = current_queue;
|
||||
return true;
|
||||
}
|
||||
if (valid_queue_) {
|
||||
return valid_queue_ == current_queue;
|
||||
}
|
||||
return rtc::IsThreadRefEqual(valid_thread_, current_thread);
|
||||
}
|
||||
|
||||
void SequenceCheckerImpl::Detach() {
|
||||
MutexLock scoped_lock(&lock_);
|
||||
attached_ = false;
|
||||
// We don't need to touch the other members here, they will be
|
||||
// reset on the next call to IsCurrent().
|
||||
}
|
||||
|
||||
#if RTC_DCHECK_IS_ON
|
||||
std::string SequenceCheckerImpl::ExpectationToString() const {
|
||||
const TaskQueueBase* const current_queue = TaskQueueBase::Current();
|
||||
const rtc::PlatformThreadRef current_thread = rtc::CurrentThreadRef();
|
||||
MutexLock scoped_lock(&lock_);
|
||||
if (!attached_)
|
||||
return "Checker currently not attached.";
|
||||
|
||||
// The format of the string is meant to compliment the one we have inside of
|
||||
// FatalLog() (checks.cc). Example:
|
||||
//
|
||||
// # Expected: TQ: 0x0 SysQ: 0x7fff69541330 Thread: 0x11dcf6dc0
|
||||
// # Actual: TQ: 0x7fa8f0604190 SysQ: 0x7fa8f0604a30 Thread: 0x700006f1a000
|
||||
// TaskQueue doesn't match
|
||||
|
||||
rtc::StringBuilder message;
|
||||
message.AppendFormat(
|
||||
"# Expected: TQ: %p Thread: %p\n"
|
||||
"# Actual: TQ: %p Thread: %p\n",
|
||||
valid_queue_, reinterpret_cast<const void*>(valid_thread_), current_queue,
|
||||
reinterpret_cast<const void*>(current_thread));
|
||||
|
||||
if ((valid_queue_ || current_queue) && valid_queue_ != current_queue) {
|
||||
message << "TaskQueue doesn't match\n";
|
||||
} else if (!rtc::IsThreadRefEqual(valid_thread_, current_thread)) {
|
||||
message << "Threads don't match\n";
|
||||
}
|
||||
|
||||
return message.Release();
|
||||
}
|
||||
#endif // RTC_DCHECK_IS_ON
|
||||
|
||||
} // namespace webrtc_sequence_checker_internal
|
||||
} // namespace webrtc
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
* Copyright 2020 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
#ifndef RTC_BASE_SYNCHRONIZATION_SEQUENCE_CHECKER_INTERNAL_H_
|
||||
#define RTC_BASE_SYNCHRONIZATION_SEQUENCE_CHECKER_INTERNAL_H_
|
||||
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include "api/task_queue/task_queue_base.h"
|
||||
#include "rtc_base/platform_thread_types.h"
|
||||
#include "rtc_base/synchronization/mutex.h"
|
||||
#include "rtc_base/system/rtc_export.h"
|
||||
#include "rtc_base/thread_annotations.h"
|
||||
|
||||
namespace webrtc {
|
||||
namespace webrtc_sequence_checker_internal {
|
||||
|
||||
// Real implementation of SequenceChecker, for use in debug mode, or
|
||||
// for temporary use in release mode (e.g. to RTC_CHECK on a threading issue
|
||||
// seen only in the wild).
|
||||
//
|
||||
// Note: You should almost always use the SequenceChecker class to get the
|
||||
// right version for your build configuration.
|
||||
class RTC_EXPORT SequenceCheckerImpl {
|
||||
public:
|
||||
explicit SequenceCheckerImpl(bool attach_to_current_thread);
|
||||
explicit SequenceCheckerImpl(TaskQueueBase* attached_queue);
|
||||
~SequenceCheckerImpl() = default;
|
||||
|
||||
bool IsCurrent() const;
|
||||
// Changes the task queue or thread that is checked for in IsCurrent. This can
|
||||
// be useful when an object may be created on one task queue / thread and then
|
||||
// used exclusively on another thread.
|
||||
void Detach();
|
||||
|
||||
// Returns a string that is formatted to match with the error string printed
|
||||
// by RTC_CHECK() when a condition is not met.
|
||||
// This is used in conjunction with the RTC_DCHECK_RUN_ON() macro.
|
||||
std::string ExpectationToString() const;
|
||||
|
||||
private:
|
||||
mutable Mutex lock_;
|
||||
// These are mutable so that IsCurrent can set them.
|
||||
mutable bool attached_ RTC_GUARDED_BY(lock_);
|
||||
mutable rtc::PlatformThreadRef valid_thread_ RTC_GUARDED_BY(lock_);
|
||||
mutable const TaskQueueBase* valid_queue_ RTC_GUARDED_BY(lock_);
|
||||
};
|
||||
|
||||
// Do nothing implementation, for use in release mode.
|
||||
//
|
||||
// Note: You should almost always use the SequenceChecker class to get the
|
||||
// right version for your build configuration.
|
||||
class SequenceCheckerDoNothing {
|
||||
public:
|
||||
explicit SequenceCheckerDoNothing(bool attach_to_current_thread) {}
|
||||
explicit SequenceCheckerDoNothing(TaskQueueBase* attached_queue) {}
|
||||
bool IsCurrent() const { return true; }
|
||||
void Detach() {}
|
||||
};
|
||||
|
||||
template <typename ThreadLikeObject>
|
||||
std::enable_if_t<std::is_base_of_v<SequenceCheckerImpl, ThreadLikeObject>,
|
||||
std::string>
|
||||
ExpectationToString(const ThreadLikeObject* checker) {
|
||||
#if RTC_DCHECK_IS_ON
|
||||
return checker->ExpectationToString();
|
||||
#else
|
||||
return std::string();
|
||||
#endif
|
||||
}
|
||||
|
||||
// Catch-all implementation for types other than explicitly supported above.
|
||||
template <typename ThreadLikeObject>
|
||||
std::enable_if_t<!std::is_base_of_v<SequenceCheckerImpl, ThreadLikeObject>,
|
||||
std::string>
|
||||
ExpectationToString(const ThreadLikeObject*) {
|
||||
return std::string();
|
||||
}
|
||||
|
||||
} // namespace webrtc_sequence_checker_internal
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // RTC_BASE_SYNCHRONIZATION_SEQUENCE_CHECKER_INTERNAL_H_
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Copyright 2020 The WebRTC Project Authors. All rights reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include "rtc_base/synchronization/yield.h"
|
||||
|
||||
#if defined(WEBRTC_WIN)
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <sched.h>
|
||||
#include <time.h>
|
||||
#endif
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
void YieldCurrentThread() {
|
||||
// TODO(bugs.webrtc.org/11634): use dedicated OS functionality instead of
|
||||
// sleep for yielding.
|
||||
#if defined(WEBRTC_WIN)
|
||||
::Sleep(0);
|
||||
#elif defined(WEBRTC_MAC) && defined(RTC_USE_NATIVE_MUTEX_ON_MAC) && \
|
||||
!RTC_USE_NATIVE_MUTEX_ON_MAC
|
||||
sched_yield();
|
||||
#else
|
||||
static const struct timespec ts_null = {0};
|
||||
nanosleep(&ts_null, nullptr);
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
* Copyright 2020 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
#ifndef RTC_BASE_SYNCHRONIZATION_YIELD_H_
|
||||
#define RTC_BASE_SYNCHRONIZATION_YIELD_H_
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
// Request rescheduling of threads.
|
||||
void YieldCurrentThread();
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // RTC_BASE_SYNCHRONIZATION_YIELD_H_
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* Copyright 2019 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
#include "rtc_base/synchronization/yield_policy.h"
|
||||
|
||||
#include "absl/base/attributes.h"
|
||||
#include "absl/base/config.h"
|
||||
#include "rtc_base/checks.h"
|
||||
#if !defined(ABSL_HAVE_THREAD_LOCAL) && defined(WEBRTC_POSIX)
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
namespace rtc {
|
||||
namespace {
|
||||
|
||||
#if defined(ABSL_HAVE_THREAD_LOCAL)
|
||||
|
||||
ABSL_CONST_INIT thread_local YieldInterface* current_yield_policy = nullptr;
|
||||
|
||||
YieldInterface* GetCurrentYieldPolicy() {
|
||||
return current_yield_policy;
|
||||
}
|
||||
|
||||
void SetCurrentYieldPolicy(YieldInterface* ptr) {
|
||||
current_yield_policy = ptr;
|
||||
}
|
||||
|
||||
#elif defined(WEBRTC_POSIX)
|
||||
|
||||
// Emscripten does not support the C++11 thread_local keyword but does support
|
||||
// the pthread thread-local storage API.
|
||||
// https://github.com/emscripten-core/emscripten/issues/3502
|
||||
|
||||
ABSL_CONST_INIT pthread_key_t g_current_yield_policy_tls = 0;
|
||||
|
||||
void InitializeTls() {
|
||||
RTC_CHECK_EQ(pthread_key_create(&g_current_yield_policy_tls, nullptr), 0);
|
||||
}
|
||||
|
||||
pthread_key_t GetCurrentYieldPolicyTls() {
|
||||
static pthread_once_t init_once = PTHREAD_ONCE_INIT;
|
||||
RTC_CHECK_EQ(pthread_once(&init_once, &InitializeTls), 0);
|
||||
return g_current_yield_policy_tls;
|
||||
}
|
||||
|
||||
YieldInterface* GetCurrentYieldPolicy() {
|
||||
return static_cast<YieldInterface*>(
|
||||
pthread_getspecific(GetCurrentYieldPolicyTls()));
|
||||
}
|
||||
|
||||
void SetCurrentYieldPolicy(YieldInterface* ptr) {
|
||||
pthread_setspecific(GetCurrentYieldPolicyTls(), ptr);
|
||||
}
|
||||
|
||||
#else
|
||||
#error Unsupported platform
|
||||
#endif
|
||||
|
||||
} // namespace
|
||||
|
||||
ScopedYieldPolicy::ScopedYieldPolicy(YieldInterface* policy)
|
||||
: previous_(GetCurrentYieldPolicy()) {
|
||||
SetCurrentYieldPolicy(policy);
|
||||
}
|
||||
|
||||
ScopedYieldPolicy::~ScopedYieldPolicy() {
|
||||
SetCurrentYieldPolicy(previous_);
|
||||
}
|
||||
|
||||
void ScopedYieldPolicy::YieldExecution() {
|
||||
YieldInterface* current = GetCurrentYieldPolicy();
|
||||
if (current)
|
||||
current->YieldExecution();
|
||||
}
|
||||
|
||||
} // namespace rtc
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Copyright 2019 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
#ifndef RTC_BASE_SYNCHRONIZATION_YIELD_POLICY_H_
|
||||
#define RTC_BASE_SYNCHRONIZATION_YIELD_POLICY_H_
|
||||
|
||||
namespace rtc {
|
||||
class YieldInterface {
|
||||
public:
|
||||
virtual ~YieldInterface() = default;
|
||||
virtual void YieldExecution() = 0;
|
||||
};
|
||||
|
||||
// Sets the current thread-local yield policy while it's in scope and reverts
|
||||
// to the previous policy when it leaves the scope.
|
||||
class ScopedYieldPolicy final {
|
||||
public:
|
||||
explicit ScopedYieldPolicy(YieldInterface* policy);
|
||||
ScopedYieldPolicy(const ScopedYieldPolicy&) = delete;
|
||||
ScopedYieldPolicy& operator=(const ScopedYieldPolicy&) = delete;
|
||||
~ScopedYieldPolicy();
|
||||
// Will yield as specified by the currently active thread-local yield policy
|
||||
// (which by default is a no-op).
|
||||
static void YieldExecution();
|
||||
|
||||
private:
|
||||
YieldInterface* const previous_;
|
||||
};
|
||||
|
||||
} // namespace rtc
|
||||
|
||||
#endif // RTC_BASE_SYNCHRONIZATION_YIELD_POLICY_H_
|
||||
Loading…
Add table
Add a link
Reference in a new issue