John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #ifndef AOS_STL_MUTEX_H_ |
| 2 | #define AOS_STL_MUTEX_H_ |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 3 | |
| 4 | #include <mutex> |
| 5 | |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 6 | #include "aos/ipc_lib/aos_sync.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 7 | #include "aos/macros.h" |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 8 | #include "glog/logging.h" |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 9 | |
| 10 | namespace 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. |
| 20 | class 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 Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 33 | LOG(FATAL) << "mutex_grab(" << &native_handle_ << ") failed: " << ret; |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 34 | } |
| 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 Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 48 | LOG(FATAL) << "mutex_trylock(" << &native_handle_ |
| 49 | << ") failed: " << ret; |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | void unlock() { |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 54 | CHECK(!owner_died_); |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 55 | 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 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame^] | 64 | // Returns whether this mutex is locked by the current thread. This is very |
| 65 | // hard to use reliably, please think very carefully before using it for |
| 66 | // anything beyond probabilistic assertion checks. |
| 67 | bool is_locked() const { return mutex_islocked(&native_handle_); } |
| 68 | |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 69 | private: |
| 70 | aos_mutex native_handle_; |
| 71 | |
| 72 | bool owner_died_ = false; |
| 73 | |
| 74 | DISALLOW_COPY_AND_ASSIGN(stl_mutex); |
| 75 | }; |
| 76 | |
| 77 | // A mutex with the same API and semantics as ::std::recursive_mutex, with the |
| 78 | // addition of methods for checking if the previous owner died and a constexpr |
| 79 | // default constructor. |
| 80 | // Definitely safe to put in SHM. |
| 81 | // This uses the pthread_mutex semantics for owner-died: once somebody dies with |
| 82 | // the lock held, anybody else who takes it will see true for owner_died() until |
| 83 | // one of them calls consistent(). It is an error to call unlock() or lock() |
| 84 | // again when owner_died() returns true. |
| 85 | class stl_recursive_mutex { |
| 86 | public: |
| 87 | constexpr stl_recursive_mutex() {} |
| 88 | |
| 89 | void lock() { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame^] | 90 | if (mutex_.is_locked()) { |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 91 | CHECK(!owner_died()); |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 92 | ++recursive_locks_; |
| 93 | } else { |
| 94 | mutex_.lock(); |
| 95 | if (mutex_.owner_died()) { |
| 96 | recursive_locks_ = 0; |
| 97 | } else { |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 98 | CHECK_EQ(0, recursive_locks_); |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | } |
| 102 | bool try_lock() { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame^] | 103 | if (mutex_.is_locked()) { |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 104 | CHECK(!owner_died()); |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 105 | ++recursive_locks_; |
| 106 | return true; |
| 107 | } else { |
| 108 | if (mutex_.try_lock()) { |
| 109 | if (mutex_.owner_died()) { |
| 110 | recursive_locks_ = 0; |
| 111 | } else { |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 112 | CHECK_EQ(0, recursive_locks_); |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 113 | } |
| 114 | return true; |
| 115 | } else { |
| 116 | return false; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | void unlock() { |
| 121 | if (recursive_locks_ == 0) { |
| 122 | mutex_.unlock(); |
| 123 | } else { |
| 124 | --recursive_locks_; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | typedef stl_mutex::native_handle_type native_handle_type; |
| 129 | native_handle_type native_handle() { return mutex_.native_handle(); } |
| 130 | |
| 131 | bool owner_died() const { return mutex_.owner_died(); } |
| 132 | void consistent() { mutex_.consistent(); } |
| 133 | |
| 134 | private: |
| 135 | stl_mutex mutex_; |
| 136 | int recursive_locks_ = 0; |
| 137 | |
| 138 | DISALLOW_COPY_AND_ASSIGN(stl_recursive_mutex); |
| 139 | }; |
| 140 | |
| 141 | // Convenient typedefs for various types of locking objects. |
| 142 | typedef ::std::lock_guard<stl_mutex> mutex_lock_guard; |
| 143 | typedef ::std::lock_guard<stl_recursive_mutex> recursive_lock_guard; |
| 144 | typedef ::std::unique_lock<stl_mutex> mutex_unique_lock; |
| 145 | typedef ::std::unique_lock<stl_recursive_mutex> recursive_unique_lock; |
| 146 | |
| 147 | } // namespace aos |
| 148 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 149 | #endif // AOS_STL_MUTEX_H_ |