1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_QUIC_PLATFORM_IMPL_QUIC_MUTEX_IMPL_H_ 6 #define NET_QUIC_PLATFORM_IMPL_QUIC_MUTEX_IMPL_H_ 7 8 #include "base/macros.h" 9 #include "base/synchronization/lock.h" 10 #include "base/synchronization/waitable_event.h" 11 #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" 12 13 #define QUIC_EXCLUSIVE_LOCKS_REQUIRED_IMPL EXCLUSIVE_LOCKS_REQUIRED 14 #define QUIC_GUARDED_BY_IMPL GUARDED_BY 15 #define QUIC_LOCKABLE_IMPL LOCKABLE 16 #define QUIC_LOCKS_EXCLUDED_IMPL LOCKS_EXCLUDED 17 #define QUIC_SHARED_LOCKS_REQUIRED_IMPL SHARED_LOCKS_REQUIRED 18 #define QUIC_EXCLUSIVE_LOCK_FUNCTION_IMPL EXCLUSIVE_LOCK_FUNCTION 19 #define QUIC_UNLOCK_FUNCTION_IMPL UNLOCK_FUNCTION 20 #define QUIC_SHARED_LOCK_FUNCTION_IMPL SHARED_LOCK_FUNCTION 21 #define QUIC_SCOPED_LOCKABLE_IMPL SCOPED_LOCKABLE 22 #define QUIC_ASSERT_SHARED_LOCK_IMPL ASSERT_SHARED_LOCK 23 24 #ifndef EXCLUSIVE_LOCK_FUNCTION 25 #define EXCLUSIVE_LOCK_FUNCTION(...) 26 #endif 27 28 #ifndef UNLOCK_FUNCTION 29 #define UNLOCK_FUNCTION(...) 30 #endif 31 32 #ifndef SHARED_LOCK_FUNCTION 33 #define SHARED_LOCK_FUNCTION(...) 34 #endif 35 36 #ifndef ASSERT_SHARED_LOCK 37 #define ASSERT_SHARED_LOCK(...) 38 #endif 39 40 #ifndef LOCKABLE 41 #define LOCKABLE 42 #endif 43 44 #ifndef SCOPED_LOCKABLE 45 #define SCOPED_LOCKABLE 46 #endif 47 48 #ifndef GUARDED_BY 49 #define GUARDED_BY(x) 50 #endif 51 52 #ifndef SHARED_LOCKS_REQUIRED 53 #define SHARED_LOCKS_REQUIRED(...) 54 #endif 55 56 #ifndef EXCLUSIVE_LOCKS_REQUIRED 57 #define EXCLUSIVE_LOCKS_REQUIRED(...) 58 #endif 59 60 namespace quic { 61 62 // A class wrapping a non-reentrant mutex. 63 class QUIC_LOCKABLE_IMPL QUIC_EXPORT_PRIVATE QuicLockImpl { 64 public: 65 QuicLockImpl() = default; 66 67 // Block until lock_ is free, then acquire it exclusively. 68 void WriterLock() EXCLUSIVE_LOCK_FUNCTION(); 69 70 // Release lock_. Caller must hold it exclusively. 71 void WriterUnlock() UNLOCK_FUNCTION(); 72 73 // Block until lock_ is free or shared, then acquire a share of it. 74 void ReaderLock() SHARED_LOCK_FUNCTION(); 75 76 // Release lock_. Caller could hold it in shared mode. 77 void ReaderUnlock() UNLOCK_FUNCTION(); 78 79 // Not implemented. AssertReaderHeld()80 void AssertReaderHeld() const ASSERT_SHARED_LOCK() {} 81 82 private: 83 base::Lock lock_; 84 85 DISALLOW_COPY_AND_ASSIGN(QuicLockImpl); 86 }; 87 88 // A Notification allows threads to receive notification of a single occurrence 89 // of a single event. 90 class QUIC_EXPORT_PRIVATE QuicNotificationImpl { 91 public: QuicNotificationImpl()92 QuicNotificationImpl() 93 : event_(base::WaitableEvent::ResetPolicy::MANUAL, 94 base::WaitableEvent::InitialState::NOT_SIGNALED) {} 95 QuicNotificationImpl(const QuicNotificationImpl&) = delete; 96 QuicNotificationImpl& operator=(const QuicNotificationImpl&) = delete; 97 HasBeenNotified()98 bool HasBeenNotified() { return event_.IsSignaled(); } 99 Notify()100 void Notify() { event_.Signal(); } 101 WaitForNotification()102 void WaitForNotification() { event_.Wait(); } 103 104 private: 105 base::WaitableEvent event_; 106 }; 107 108 } // namespace quic 109 110 #endif // NET_QUIC_PLATFORM_IMPL_QUIC_MUTEX_IMPL_H_ 111