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