Flutter Impeller
thread.h
Go to the documentation of this file.
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 #pragma once
6 
7 #include <memory>
8 #include <mutex>
9 #include <thread>
10 
11 #include "flutter/fml/macros.h"
12 #include "flutter/fml/synchronization/shared_mutex.h"
14 
15 namespace impeller {
16 
17 class IPLR_CAPABILITY("mutex") Mutex {
18  public:
19  Mutex() = default;
20 
21  ~Mutex() = default;
22 
23  void Lock() IPLR_ACQUIRE() { mutex_.lock(); }
24 
25  void Unlock() IPLR_RELEASE() { mutex_.unlock(); }
26 
27  private:
28  std::mutex mutex_;
29 
30  FML_DISALLOW_COPY_ASSIGN_AND_MOVE(Mutex);
31 };
32 
33 class IPLR_CAPABILITY("mutex") RWMutex {
34  public:
35  RWMutex()
36  : mutex_(std::unique_ptr<fml::SharedMutex>(fml::SharedMutex::Create())) {}
37 
38  ~RWMutex() = default;
39 
40  void LockWriter() IPLR_ACQUIRE() { mutex_->Lock(); }
41 
42  void UnlockWriter() IPLR_RELEASE() { mutex_->Unlock(); }
43 
44  void LockReader() IPLR_ACQUIRE_SHARED() { mutex_->LockShared(); }
45 
46  void UnlockReader() IPLR_RELEASE_SHARED() { mutex_->UnlockShared(); }
47 
48  private:
49  std::unique_ptr<fml::SharedMutex> mutex_;
50 
51  FML_DISALLOW_COPY_ASSIGN_AND_MOVE(RWMutex);
52 };
53 
55  public:
56  explicit Lock(Mutex& mutex) IPLR_ACQUIRE(mutex) : mutex_(mutex) {
57  mutex_.Lock();
58  }
59 
60  ~Lock() IPLR_RELEASE() { mutex_.Unlock(); }
61 
62  private:
63  Mutex& mutex_;
64 
65  FML_DISALLOW_COPY_ASSIGN_AND_MOVE(Lock);
66 };
67 
69  public:
70  explicit ReaderLock(RWMutex& mutex) IPLR_ACQUIRE_SHARED(mutex)
71  : mutex_(mutex) {
72  mutex_.LockReader();
73  }
74 
75  ~ReaderLock() IPLR_RELEASE() { mutex_.UnlockReader(); }
76 
77  private:
78  RWMutex& mutex_;
79 
80  FML_DISALLOW_COPY_ASSIGN_AND_MOVE(ReaderLock);
81 };
82 
84  public:
85  explicit WriterLock(RWMutex& mutex) IPLR_ACQUIRE(mutex) : mutex_(mutex) {
86  mutex_.LockWriter();
87  }
88 
89  ~WriterLock() IPLR_RELEASE() { mutex_.UnlockWriter(); }
90 
91  private:
92  RWMutex& mutex_;
93 
94  FML_DISALLOW_COPY_ASSIGN_AND_MOVE(WriterLock);
95 };
96 
97 } // namespace impeller
impeller::Lock::Lock
Lock(Mutex &mutex) IPLR_ACQUIRE(mutex)
Definition: thread.h:56
impeller::ReaderLock::~ReaderLock
~ReaderLock() IPLR_RELEASE()
Definition: thread.h:75
impeller::Lock::~Lock
~Lock() IPLR_RELEASE()
Definition: thread.h:60
IPLR_ACQUIRE_SHARED
#define IPLR_ACQUIRE_SHARED(...)
Definition: thread_safety.h:38
IPLR_SCOPED_CAPABILITY
#define IPLR_SCOPED_CAPABILITY
Definition: thread_safety.h:15
impeller::WriterLock
Definition: thread.h:83
impeller::Lock
Definition: thread.h:54
impeller::WriterLock::WriterLock
WriterLock(RWMutex &mutex) IPLR_ACQUIRE(mutex)
Definition: thread.h:85
impeller::ReaderLock::ReaderLock
ReaderLock(RWMutex &mutex) IPLR_ACQUIRE_SHARED(mutex)
Definition: thread.h:70
IPLR_ACQUIRE
#define IPLR_ACQUIRE(...)
Definition: thread_safety.h:35
IPLR_RELEASE
#define IPLR_RELEASE(...)
Definition: thread_safety.h:41
impeller::ReaderLock
Definition: thread.h:68
thread_safety.h
impeller::WriterLock::~WriterLock
~WriterLock() IPLR_RELEASE()
Definition: thread.h:89
impeller::IPLR_CAPABILITY
class IPLR_CAPABILITY("mutex") Mutex
Definition: thread.h:17
IPLR_RELEASE_SHARED
#define IPLR_RELEASE_SHARED(...)
Definition: thread_safety.h:44
std
Definition: comparable.h:98
impeller
Definition: aiks_context.cc:10