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