forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared_mutex.h
50 lines (38 loc) · 1.2 KB
/
shared_mutex.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_FML_SYNCHRONIZATION_SHARED_MUTEX_H_
#define FLUTTER_FML_SYNCHRONIZATION_SHARED_MUTEX_H_
namespace fml {
// Interface for a reader/writer lock.
class SharedMutex {
public:
static SharedMutex* Create();
virtual ~SharedMutex() = default;
virtual void Lock() = 0;
virtual void LockShared() = 0;
virtual void Unlock() = 0;
virtual void UnlockShared() = 0;
};
// RAII wrapper that does a shared acquire of a SharedMutex.
class SharedLock {
public:
explicit SharedLock(SharedMutex& shared_mutex) : shared_mutex_(shared_mutex) {
shared_mutex_.LockShared();
}
~SharedLock() { shared_mutex_.UnlockShared(); }
private:
SharedMutex& shared_mutex_;
};
// RAII wrapper that does an exclusive acquire of a SharedMutex.
class UniqueLock {
public:
explicit UniqueLock(SharedMutex& shared_mutex) : shared_mutex_(shared_mutex) {
shared_mutex_.Lock();
}
~UniqueLock() { shared_mutex_.Unlock(); }
private:
SharedMutex& shared_mutex_;
};
} // namespace fml
#endif // FLUTTER_FML_SYNCHRONIZATION_SHARED_MUTEX_H_