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/logging/logging.h" |
| 8 | #include "aos/type_traits/type_traits.h" |
| 9 | #include "aos/macros.h" |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 10 | |
| 11 | namespace aos { |
| 12 | |
| 13 | // A mutex with the same API and semantics as ::std::mutex, with the addition of |
| 14 | // methods for checking if the previous owner died and a constexpr default |
| 15 | // constructor. |
| 16 | // Definitely safe to put in SHM. |
| 17 | // This uses the pthread_mutex semantics for owner-died: once somebody dies with |
| 18 | // the lock held, anybody else who takes it will see true for owner_died() until |
| 19 | // one of them calls consistent(). It is an error to call unlock() when |
| 20 | // owner_died() returns true. |
| 21 | class stl_mutex { |
| 22 | public: |
| 23 | constexpr stl_mutex() : native_handle_() {} |
| 24 | |
| 25 | void lock() { |
| 26 | const int ret = mutex_grab(&native_handle_); |
| 27 | switch (ret) { |
| 28 | case 0: |
| 29 | break; |
| 30 | case 1: |
| 31 | owner_died_ = true; |
| 32 | break; |
| 33 | default: |
| 34 | LOG(FATAL, "mutex_grab(%p) failed with %d\n", &native_handle_, ret); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | bool try_lock() { |
| 39 | const int ret = mutex_trylock(&native_handle_); |
| 40 | switch (ret) { |
| 41 | case 0: |
| 42 | return true; |
| 43 | case 1: |
| 44 | owner_died_ = true; |
| 45 | return true; |
| 46 | case 4: |
| 47 | return false; |
| 48 | default: |
| 49 | LOG(FATAL, "mutex_trylock(%p) failed with %d\n", &native_handle_, ret); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | void unlock() { |
| 54 | CHECK(!owner_died_); |
| 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 | |
| 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. |
| 80 | class stl_recursive_mutex { |
| 81 | public: |
| 82 | constexpr stl_recursive_mutex() {} |
| 83 | |
| 84 | void lock() { |
| 85 | if (mutex_islocked(mutex_.native_handle())) { |
| 86 | CHECK(!owner_died()); |
| 87 | ++recursive_locks_; |
| 88 | } else { |
| 89 | mutex_.lock(); |
| 90 | if (mutex_.owner_died()) { |
| 91 | recursive_locks_ = 0; |
| 92 | } else { |
| 93 | CHECK_EQ(0, recursive_locks_); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | bool try_lock() { |
| 98 | if (mutex_islocked(mutex_.native_handle())) { |
| 99 | CHECK(!owner_died()); |
| 100 | ++recursive_locks_; |
| 101 | return true; |
| 102 | } else { |
| 103 | if (mutex_.try_lock()) { |
| 104 | if (mutex_.owner_died()) { |
| 105 | recursive_locks_ = 0; |
| 106 | } else { |
| 107 | CHECK_EQ(0, recursive_locks_); |
| 108 | } |
| 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. |
| 137 | typedef ::std::lock_guard<stl_mutex> mutex_lock_guard; |
| 138 | typedef ::std::lock_guard<stl_recursive_mutex> recursive_lock_guard; |
| 139 | typedef ::std::unique_lock<stl_mutex> mutex_unique_lock; |
| 140 | typedef ::std::unique_lock<stl_recursive_mutex> recursive_unique_lock; |
| 141 | |
| 142 | } // namespace aos |
| 143 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 144 | #endif // AOS_STL_MUTEX_H_ |