John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #ifndef AOS_MUTEX_H_ |
| 2 | #define AOS_MUTEX_H_ |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 3 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 4 | #include "aos/macros.h" |
| 5 | #include "aos/type_traits/type_traits.h" |
| 6 | #include "aos/die.h" |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 7 | #include "aos/ipc_lib/aos_sync.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 8 | |
| 9 | namespace aos { |
| 10 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 11 | class Condition; |
| 12 | |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 13 | // An abstraction of a mutex that is easy to implement for environments other |
| 14 | // than Linux too. |
| 15 | // If there are multiple threads or processes contending for the mutex, |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 16 | // higher priority ones will succeed in locking first, |
| 17 | // and tasks of equal priorities have the same chance of getting the lock. |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 18 | // To deal with priority inversion, the linux implementation does priority |
| 19 | // inheritance. |
| 20 | // Before destroying a mutex, it is important to make sure it isn't locked. |
| 21 | // Otherwise, the destructor will LOG(FATAL). |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 22 | class Mutex { |
| 23 | public: |
Daniel Petti | 88a1566 | 2015-04-12 17:42:22 -0400 | [diff] [blame] | 24 | // States that signify the result of TryLock. |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 25 | enum class State { |
Daniel Petti | 88a1566 | 2015-04-12 17:42:22 -0400 | [diff] [blame] | 26 | // The mutex was acquired successfully. |
| 27 | kLocked, |
| 28 | // TryLock tried to grab the mutex and failed. |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 29 | kLockFailed, |
| 30 | // The previous owner of the mutex died. |
| 31 | kOwnerDied, |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 32 | }; |
| 33 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 34 | // Creates an unlocked mutex. |
Austin Schuh | ceb0e77 | 2018-01-03 21:10:57 -0800 | [diff] [blame] | 35 | Mutex() : impl_() { |
| 36 | static_assert(shm_ok<Mutex>::value, |
| 37 | "Mutex is not safe for use in shared memory."); |
| 38 | } |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 39 | // Verifies that it isn't locked. |
| 40 | // |
| 41 | // This is important because freeing a locked mutex means there is freed |
| 42 | // memory in the middle of the robust list, which breaks things horribly. |
Austin Schuh | ceb0e77 | 2018-01-03 21:10:57 -0800 | [diff] [blame] | 43 | ~Mutex() = default; |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 44 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 45 | // Locks the mutex. If it fails, it calls LOG(FATAL). |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 46 | // Returns true if the previous owner died instead of unlocking nicely. |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 47 | bool Lock() __attribute__((warn_unused_result)); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 48 | // Unlocks the mutex. Fails like Lock. |
Brian Silverman | 6da0427 | 2014-05-18 18:47:48 -0700 | [diff] [blame] | 49 | // Multiple unlocking is undefined. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 50 | void Unlock(); |
| 51 | // Locks the mutex unless it is already locked. |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 52 | // Returns the new state of the mutex. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 53 | // Doesn't wait for the mutex to be unlocked if it is locked. |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 54 | State TryLock() __attribute__((warn_unused_result)); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 55 | |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 56 | // Returns true iff the current task has this mutex locked. |
| 57 | // This is mainly for IPCRecursiveMutexLocker to use. |
| 58 | bool OwnedBySelf() const; |
| 59 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 60 | private: |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 61 | aos_mutex impl_; |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 62 | |
| 63 | friend class Condition; // for access to impl_ |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | // A class that locks a Mutex when constructed and unlocks it when destructed. |
| 67 | // Designed to be used as a local variable so that |
| 68 | // the mutex will be unlocked when the scope is exited. |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 69 | // This one immediately Dies if the previous owner died. This makes it a good |
| 70 | // choice for mutexes that are only used within a single process, but NOT for |
| 71 | // mutexes shared by multiple processes. For those, use IPCMutexLocker. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 72 | class MutexLocker { |
| 73 | public: |
| 74 | explicit MutexLocker(Mutex *mutex) : mutex_(mutex) { |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 75 | if (__builtin_expect(mutex_->Lock(), false)) { |
| 76 | ::aos::Die("previous owner of mutex %p died but it shouldn't be able to", |
| 77 | this); |
| 78 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 79 | } |
| 80 | ~MutexLocker() { |
| 81 | mutex_->Unlock(); |
| 82 | } |
| 83 | |
| 84 | private: |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 85 | Mutex *const mutex_; |
| 86 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 87 | DISALLOW_COPY_AND_ASSIGN(MutexLocker); |
| 88 | }; |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 89 | |
| 90 | // A version of MutexLocker which reports the previous owner dying instead of |
| 91 | // immediately LOG(FATAL)ing. |
| 92 | class IPCMutexLocker { |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 93 | public: |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 94 | explicit IPCMutexLocker(Mutex *mutex) |
| 95 | : mutex_(mutex), owner_died_(mutex_->Lock()) {} |
| 96 | ~IPCMutexLocker() { |
| 97 | if (__builtin_expect(!owner_died_checked_, false)) { |
| 98 | ::aos::Die("nobody checked if the previous owner of mutex %p died", this); |
| 99 | } |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 100 | mutex_->Unlock(); |
| 101 | } |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 102 | |
| 103 | // Whether or not the previous owner died. If this is not called at least |
| 104 | // once, the destructor will ::aos::Die. |
| 105 | __attribute__((warn_unused_result)) bool owner_died() { |
| 106 | owner_died_checked_ = true; |
| 107 | return __builtin_expect(owner_died_, false); |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | private: |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 111 | Mutex *const mutex_; |
| 112 | const bool owner_died_; |
| 113 | bool owner_died_checked_ = false; |
| 114 | |
| 115 | DISALLOW_COPY_AND_ASSIGN(IPCMutexLocker); |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 116 | }; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 117 | |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 118 | // A version of IPCMutexLocker which only locks (and unlocks) the mutex if the |
| 119 | // current task does not already hold it. |
| 120 | class IPCRecursiveMutexLocker { |
| 121 | public: |
| 122 | explicit IPCRecursiveMutexLocker(Mutex *mutex) |
| 123 | : mutex_(mutex), |
| 124 | locked_(!mutex_->OwnedBySelf()), |
| 125 | owner_died_(locked_ ? mutex_->Lock() : false) {} |
| 126 | ~IPCRecursiveMutexLocker() { |
| 127 | if (__builtin_expect(!owner_died_checked_, false)) { |
| 128 | ::aos::Die("nobody checked if the previous owner of mutex %p died", this); |
| 129 | } |
| 130 | if (locked_) mutex_->Unlock(); |
| 131 | } |
| 132 | |
| 133 | // Whether or not the previous owner died. If this is not called at least |
| 134 | // once, the destructor will ::aos::Die. |
| 135 | __attribute__((warn_unused_result)) bool owner_died() { |
| 136 | owner_died_checked_ = true; |
| 137 | return __builtin_expect(owner_died_, false); |
| 138 | } |
| 139 | |
| 140 | private: |
| 141 | Mutex *const mutex_; |
| 142 | const bool locked_, owner_died_; |
| 143 | bool owner_died_checked_ = false; |
| 144 | |
| 145 | DISALLOW_COPY_AND_ASSIGN(IPCRecursiveMutexLocker); |
| 146 | }; |
| 147 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 148 | } // namespace aos |
| 149 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 150 | #endif // AOS_MUTEX_H_ |