• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Flutter 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 FLUTTER_FML_SYNCHRONIZATION_SHARED_MUTEX_H_
6 #define FLUTTER_FML_SYNCHRONIZATION_SHARED_MUTEX_H_
7 
8 namespace fml {
9 
10 // Interface for a reader/writer lock.
11 class SharedMutex {
12  public:
13   static SharedMutex* Create();
14   virtual ~SharedMutex() = default;
15 
16   virtual void Lock() = 0;
17   virtual void LockShared() = 0;
18   virtual void Unlock() = 0;
19   virtual void UnlockShared() = 0;
20 };
21 
22 // RAII wrapper that does a shared acquire of a SharedMutex.
23 class SharedLock {
24  public:
SharedLock(SharedMutex & shared_mutex)25   explicit SharedLock(SharedMutex& shared_mutex) : shared_mutex_(shared_mutex) {
26     shared_mutex_.LockShared();
27   }
28 
~SharedLock()29   ~SharedLock() { shared_mutex_.UnlockShared(); }
30 
31  private:
32   SharedMutex& shared_mutex_;
33 };
34 
35 // RAII wrapper that does an exclusive acquire of a SharedMutex.
36 class UniqueLock {
37  public:
UniqueLock(SharedMutex & shared_mutex)38   explicit UniqueLock(SharedMutex& shared_mutex) : shared_mutex_(shared_mutex) {
39     shared_mutex_.Lock();
40   }
41 
~UniqueLock()42   ~UniqueLock() { shared_mutex_.Unlock(); }
43 
44  private:
45   SharedMutex& shared_mutex_;
46 };
47 
48 }  // namespace fml
49 
50 #endif  // FLUTTER_FML_SYNCHRONIZATION_SHARED_MUTEX_H_
51