blob: 86a5988335270c36c3f979433db38a5a4b5969c2 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#ifndef AOS_STL_MUTEX_H_
2#define AOS_STL_MUTEX_H_
Brian Silvermanb073f242014-09-08 16:29:57 -04003
4#include <mutex>
5
John Park398c74a2018-10-20 21:17:39 -07006#include "aos/ipc_lib/aos_sync.h"
John Park33858a32018-09-28 23:05:48 -07007#include "aos/macros.h"
Austin Schuha0c41ba2020-09-10 22:59:14 -07008#include "glog/logging.h"
Brian Silvermanb073f242014-09-08 16:29:57 -04009
10namespace aos {
11
12// A mutex with the same API and semantics as ::std::mutex, with the addition of
13// methods for checking if the previous owner died and a constexpr default
14// constructor.
15// Definitely safe to put in SHM.
16// This uses the pthread_mutex semantics for owner-died: once somebody dies with
17// the lock held, anybody else who takes it will see true for owner_died() until
18// one of them calls consistent(). It is an error to call unlock() when
19// owner_died() returns true.
20class stl_mutex {
21 public:
22 constexpr stl_mutex() : native_handle_() {}
23
24 void lock() {
25 const int ret = mutex_grab(&native_handle_);
26 switch (ret) {
27 case 0:
28 break;
29 case 1:
30 owner_died_ = true;
31 break;
32 default:
Austin Schuha0c41ba2020-09-10 22:59:14 -070033 LOG(FATAL) << "mutex_grab(" << &native_handle_ << ") failed: " << ret;
Brian Silvermanb073f242014-09-08 16:29:57 -040034 }
35 }
36
37 bool try_lock() {
38 const int ret = mutex_trylock(&native_handle_);
39 switch (ret) {
40 case 0:
41 return true;
42 case 1:
43 owner_died_ = true;
44 return true;
45 case 4:
46 return false;
47 default:
Austin Schuha0c41ba2020-09-10 22:59:14 -070048 LOG(FATAL) << "mutex_trylock(" << &native_handle_
49 << ") failed: " << ret;
Brian Silvermanb073f242014-09-08 16:29:57 -040050 }
51 }
52
53 void unlock() {
Austin Schuha0c41ba2020-09-10 22:59:14 -070054 CHECK(!owner_died_);
Brian Silvermanb073f242014-09-08 16:29:57 -040055 mutex_unlock(&native_handle_);
56 }
57
58 typedef aos_mutex *native_handle_type;
59 native_handle_type native_handle() { return &native_handle_; }
60
61 bool owner_died() const { return owner_died_; }
62 void consistent() { owner_died_ = false; }
63
64 private:
65 aos_mutex native_handle_;
66
67 bool owner_died_ = false;
68
69 DISALLOW_COPY_AND_ASSIGN(stl_mutex);
70};
71
72// A mutex with the same API and semantics as ::std::recursive_mutex, with the
73// addition of methods for checking if the previous owner died and a constexpr
74// default constructor.
75// Definitely safe to put in SHM.
76// This uses the pthread_mutex semantics for owner-died: once somebody dies with
77// the lock held, anybody else who takes it will see true for owner_died() until
78// one of them calls consistent(). It is an error to call unlock() or lock()
79// again when owner_died() returns true.
80class stl_recursive_mutex {
81 public:
82 constexpr stl_recursive_mutex() {}
83
84 void lock() {
85 if (mutex_islocked(mutex_.native_handle())) {
Austin Schuha0c41ba2020-09-10 22:59:14 -070086 CHECK(!owner_died());
Brian Silvermanb073f242014-09-08 16:29:57 -040087 ++recursive_locks_;
88 } else {
89 mutex_.lock();
90 if (mutex_.owner_died()) {
91 recursive_locks_ = 0;
92 } else {
Austin Schuha0c41ba2020-09-10 22:59:14 -070093 CHECK_EQ(0, recursive_locks_);
Brian Silvermanb073f242014-09-08 16:29:57 -040094 }
95 }
96 }
97 bool try_lock() {
98 if (mutex_islocked(mutex_.native_handle())) {
Austin Schuha0c41ba2020-09-10 22:59:14 -070099 CHECK(!owner_died());
Brian Silvermanb073f242014-09-08 16:29:57 -0400100 ++recursive_locks_;
101 return true;
102 } else {
103 if (mutex_.try_lock()) {
104 if (mutex_.owner_died()) {
105 recursive_locks_ = 0;
106 } else {
Austin Schuha0c41ba2020-09-10 22:59:14 -0700107 CHECK_EQ(0, recursive_locks_);
Brian Silvermanb073f242014-09-08 16:29:57 -0400108 }
109 return true;
110 } else {
111 return false;
112 }
113 }
114 }
115 void unlock() {
116 if (recursive_locks_ == 0) {
117 mutex_.unlock();
118 } else {
119 --recursive_locks_;
120 }
121 }
122
123 typedef stl_mutex::native_handle_type native_handle_type;
124 native_handle_type native_handle() { return mutex_.native_handle(); }
125
126 bool owner_died() const { return mutex_.owner_died(); }
127 void consistent() { mutex_.consistent(); }
128
129 private:
130 stl_mutex mutex_;
131 int recursive_locks_ = 0;
132
133 DISALLOW_COPY_AND_ASSIGN(stl_recursive_mutex);
134};
135
136// Convenient typedefs for various types of locking objects.
137typedef ::std::lock_guard<stl_mutex> mutex_lock_guard;
138typedef ::std::lock_guard<stl_recursive_mutex> recursive_lock_guard;
139typedef ::std::unique_lock<stl_mutex> mutex_unique_lock;
140typedef ::std::unique_lock<stl_recursive_mutex> recursive_unique_lock;
141
142} // namespace aos
143
John Park33858a32018-09-28 23:05:48 -0700144#endif // AOS_STL_MUTEX_H_