xref: /aosp_15_r20/external/cronet/net/third_party/quiche/overrides/quiche_platform_impl/quiche_mutex_impl.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2016 The Chromium Authors
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_THIRD_PARTY_QUICHE_OVERRIDES_QUICHE_PLATFORM_IMPL_QUICHE_MUTEX_IMPL_H_
6 #define NET_THIRD_PARTY_QUICHE_OVERRIDES_QUICHE_PLATFORM_IMPL_QUICHE_MUTEX_IMPL_H_
7 
8 #include "base/synchronization/lock.h"
9 #include "base/synchronization/waitable_event.h"
10 #include "net/third_party/quiche/src/quiche/common/platform/api/quiche_export.h"
11 
12 #define QUICHE_EXCLUSIVE_LOCKS_REQUIRED_IMPL EXCLUSIVE_LOCKS_REQUIRED
13 #define QUICHE_GUARDED_BY_IMPL GUARDED_BY
14 #define QUICHE_LOCKABLE_IMPL LOCKABLE
15 #define QUICHE_LOCKS_EXCLUDED_IMPL LOCKS_EXCLUDED
16 #define QUICHE_SHARED_LOCKS_REQUIRED_IMPL SHARED_LOCKS_REQUIRED
17 #define QUICHE_EXCLUSIVE_LOCK_FUNCTION_IMPL EXCLUSIVE_LOCK_FUNCTION
18 #define QUICHE_UNLOCK_FUNCTION_IMPL UNLOCK_FUNCTION
19 #define QUICHE_SHARED_LOCK_FUNCTION_IMPL SHARED_LOCK_FUNCTION
20 #define QUICHE_SCOPED_LOCKABLE_IMPL SCOPED_LOCKABLE
21 #define QUICHE_ASSERT_SHARED_LOCK_IMPL ASSERT_SHARED_LOCK
22 
23 #ifndef EXCLUSIVE_LOCK_FUNCTION
24 #define EXCLUSIVE_LOCK_FUNCTION(...)
25 #endif
26 
27 #ifndef UNLOCK_FUNCTION
28 #define UNLOCK_FUNCTION(...)
29 #endif
30 
31 #ifndef SHARED_LOCK_FUNCTION
32 #define SHARED_LOCK_FUNCTION(...)
33 #endif
34 
35 #ifndef ASSERT_SHARED_LOCK
36 #define ASSERT_SHARED_LOCK(...)
37 #endif
38 
39 #ifndef LOCKABLE
40 #define LOCKABLE
41 #endif
42 
43 #ifndef SCOPED_LOCKABLE
44 #define SCOPED_LOCKABLE
45 #endif
46 
47 #ifndef GUARDED_BY
48 #define GUARDED_BY(x)
49 #endif
50 
51 #ifndef SHARED_LOCKS_REQUIRED
52 #define SHARED_LOCKS_REQUIRED(...)
53 #endif
54 
55 #ifndef EXCLUSIVE_LOCKS_REQUIRED
56 #define EXCLUSIVE_LOCKS_REQUIRED(...)
57 #endif
58 
59 namespace quiche {
60 
61 // A class wrapping a non-reentrant mutex.
62 class QUICHE_LOCKABLE_IMPL QUICHE_EXPORT QuicheLockImpl {
63  public:
64   QuicheLockImpl() = default;
65 
66   QuicheLockImpl(const QuicheLockImpl&) = delete;
67   QuicheLockImpl& operator=(const QuicheLockImpl&) = delete;
68 
69   // Block until lock_ is free, then acquire it exclusively.
70   void WriterLock() EXCLUSIVE_LOCK_FUNCTION();
71 
72   // Release lock_. Caller must hold it exclusively.
73   void WriterUnlock() UNLOCK_FUNCTION();
74 
75   // Block until lock_ is free or shared, then acquire a share of it.
76   void ReaderLock() SHARED_LOCK_FUNCTION();
77 
78   // Release lock_. Caller could hold it in shared mode.
79   void ReaderUnlock() UNLOCK_FUNCTION();
80 
81   // Not implemented.
AssertReaderHeld()82   void AssertReaderHeld() const ASSERT_SHARED_LOCK() {}
83 
84  private:
85   base::Lock lock_;
86 };
87 
88 // A Notification allows threads to receive notification of a single occurrence
89 // of a single event.
90 class QUICHE_EXPORT QuicheNotificationImpl {
91  public:
QuicheNotificationImpl()92   QuicheNotificationImpl()
93       : event_(base::WaitableEvent::ResetPolicy::MANUAL,
94                base::WaitableEvent::InitialState::NOT_SIGNALED) {}
95   QuicheNotificationImpl(const QuicheNotificationImpl&) = delete;
96   QuicheNotificationImpl& operator=(const QuicheNotificationImpl&) = 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 quiche
109 
110 #endif  // NET_THIRD_PARTY_QUICHE_OVERRIDES_QUICHE_PLATFORM_IMPL_QUICHE_MUTEX_IMPL_H_
111