2019-06-04 10:14:50 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2004 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.
|
|
|
|
*/
|
|
|
|
|
2020-08-14 16:58:22 +00:00
|
|
|
#ifndef RTC_BASE_DEPRECATED_RECURSIVE_CRITICAL_SECTION_H_
|
|
|
|
#define RTC_BASE_DEPRECATED_RECURSIVE_CRITICAL_SECTION_H_
|
2019-06-04 10:14:50 +00:00
|
|
|
|
2020-08-14 16:58:22 +00:00
|
|
|
#include "rtc_base/constructor_magic.h"
|
2019-06-04 10:14:50 +00:00
|
|
|
#include "rtc_base/platform_thread_types.h"
|
|
|
|
#include "rtc_base/thread_annotations.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
|
|
|
|
#endif // defined(WEBRTC_WIN)
|
|
|
|
|
|
|
|
#if defined(WEBRTC_POSIX)
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// See notes in the 'Performance' unit test for the effects of this flag.
|
2020-08-14 16:58:22 +00:00
|
|
|
#define RTC_USE_NATIVE_MUTEX_ON_MAC 1
|
2019-06-04 10:14:50 +00:00
|
|
|
|
2020-08-14 16:58:22 +00:00
|
|
|
#if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC
|
2019-06-04 10:14:50 +00:00
|
|
|
#include <dispatch/dispatch.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace rtc {
|
|
|
|
|
2020-08-14 16:58:22 +00:00
|
|
|
// NOTE: This class is deprecated. Please use webrtc::Mutex instead!
|
|
|
|
// Search using https://www.google.com/?q=recursive+lock+considered+harmful
|
|
|
|
// to find the reasons.
|
|
|
|
//
|
2019-06-04 10:14:50 +00:00
|
|
|
// Locking methods (Enter, TryEnter, Leave)are const to permit protecting
|
2020-08-14 16:58:22 +00:00
|
|
|
// members inside a const context without requiring mutable
|
|
|
|
// RecursiveCriticalSections everywhere. RecursiveCriticalSection is
|
|
|
|
// reentrant lock.
|
|
|
|
class RTC_LOCKABLE RecursiveCriticalSection {
|
2019-06-04 10:14:50 +00:00
|
|
|
public:
|
2020-08-14 16:58:22 +00:00
|
|
|
RecursiveCriticalSection();
|
|
|
|
~RecursiveCriticalSection();
|
2019-06-04 10:14:50 +00:00
|
|
|
|
|
|
|
void Enter() const RTC_EXCLUSIVE_LOCK_FUNCTION();
|
|
|
|
bool TryEnter() const RTC_EXCLUSIVE_TRYLOCK_FUNCTION(true);
|
|
|
|
void Leave() const RTC_UNLOCK_FUNCTION();
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Use only for RTC_DCHECKing.
|
|
|
|
bool CurrentThreadIsOwner() const;
|
|
|
|
|
|
|
|
#if defined(WEBRTC_WIN)
|
|
|
|
mutable CRITICAL_SECTION crit_;
|
|
|
|
#elif defined(WEBRTC_POSIX)
|
2020-08-14 16:58:22 +00:00
|
|
|
#if defined(WEBRTC_MAC) && !RTC_USE_NATIVE_MUTEX_ON_MAC
|
2019-06-04 10:14:50 +00:00
|
|
|
// Number of times the lock has been locked + number of threads waiting.
|
|
|
|
// TODO(tommi): We could use this number and subtract the recursion count
|
|
|
|
// to find places where we have multiple threads contending on the same lock.
|
|
|
|
mutable volatile int lock_queue_;
|
|
|
|
// |recursion_| represents the recursion count + 1 for the thread that owns
|
|
|
|
// the lock. Only modified by the thread that owns the lock.
|
|
|
|
mutable int recursion_;
|
|
|
|
// Used to signal a single waiting thread when the lock becomes available.
|
|
|
|
mutable dispatch_semaphore_t semaphore_;
|
|
|
|
// The thread that currently holds the lock. Required to handle recursion.
|
|
|
|
mutable PlatformThreadRef owning_thread_;
|
|
|
|
#else
|
|
|
|
mutable pthread_mutex_t mutex_;
|
|
|
|
#endif
|
|
|
|
mutable PlatformThreadRef thread_; // Only used by RTC_DCHECKs.
|
|
|
|
mutable int recursion_count_; // Only used by RTC_DCHECKs.
|
|
|
|
#else // !defined(WEBRTC_WIN) && !defined(WEBRTC_POSIX)
|
|
|
|
#error Unsupported platform.
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
// CritScope, for serializing execution through a scope.
|
|
|
|
class RTC_SCOPED_LOCKABLE CritScope {
|
|
|
|
public:
|
2020-08-14 16:58:22 +00:00
|
|
|
explicit CritScope(const RecursiveCriticalSection* cs)
|
|
|
|
RTC_EXCLUSIVE_LOCK_FUNCTION(cs);
|
2019-06-04 10:14:50 +00:00
|
|
|
~CritScope() RTC_UNLOCK_FUNCTION();
|
|
|
|
|
|
|
|
private:
|
2020-08-14 16:58:22 +00:00
|
|
|
const RecursiveCriticalSection* const cs_;
|
2019-06-04 10:14:50 +00:00
|
|
|
RTC_DISALLOW_COPY_AND_ASSIGN(CritScope);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace rtc
|
|
|
|
|
2020-08-14 16:58:22 +00:00
|
|
|
#endif // RTC_BASE_DEPRECATED_RECURSIVE_CRITICAL_SECTION_H_
|