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