1 /*
2  *  Copyright 2020 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 #include "rtc_base/synchronization/mutex.h"
12 
13 #include "rtc_base/checks.h"
14 #include "rtc_base/synchronization/yield.h"
15 
16 namespace webrtc {
17 
18 #if !defined(WEBRTC_ABSL_MUTEX)
Lock()19 void GlobalMutex::Lock() {
20   while (mutex_locked_.exchange(1)) {
21     YieldCurrentThread();
22   }
23 }
24 
Unlock()25 void GlobalMutex::Unlock() {
26   int old = mutex_locked_.exchange(0);
27   RTC_DCHECK_EQ(old, 1) << "Unlock called without calling Lock first";
28 }
29 
GlobalMutexLock(GlobalMutex * mutex)30 GlobalMutexLock::GlobalMutexLock(GlobalMutex* mutex) : mutex_(mutex) {
31   mutex_->Lock();
32 }
33 
~GlobalMutexLock()34 GlobalMutexLock::~GlobalMutexLock() {
35   mutex_->Unlock();
36 }
37 #endif  // #if !defined(WEBRTC_ABSL_MUTEX)
38 
39 }  // namespace webrtc
40