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 | |
| 46 | private: |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame^] | 47 | aos_mutex impl_; |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 48 | |
| 49 | friend class Condition; // for access to impl_ |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | // A class that locks a Mutex when constructed and unlocks it when destructed. |
| 53 | // Designed to be used as a local variable so that |
| 54 | // the mutex will be unlocked when the scope is exited. |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame^] | 55 | // This one immediately Dies if the previous owner died. This makes it a good |
| 56 | // choice for mutexes that are only used within a single process, but NOT for |
| 57 | // mutexes shared by multiple processes. For those, use IPCMutexLocker. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 58 | class MutexLocker { |
| 59 | public: |
| 60 | explicit MutexLocker(Mutex *mutex) : mutex_(mutex) { |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame^] | 61 | if (__builtin_expect(mutex_->Lock(), false)) { |
| 62 | ::aos::Die("previous owner of mutex %p died but it shouldn't be able to", |
| 63 | this); |
| 64 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 65 | } |
| 66 | ~MutexLocker() { |
| 67 | mutex_->Unlock(); |
| 68 | } |
| 69 | |
| 70 | private: |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame^] | 71 | Mutex *const mutex_; |
| 72 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 73 | DISALLOW_COPY_AND_ASSIGN(MutexLocker); |
| 74 | }; |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame^] | 75 | |
| 76 | // A version of MutexLocker which reports the previous owner dying instead of |
| 77 | // immediately LOG(FATAL)ing. |
| 78 | class IPCMutexLocker { |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 79 | public: |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame^] | 80 | explicit IPCMutexLocker(Mutex *mutex) |
| 81 | : mutex_(mutex), owner_died_(mutex_->Lock()) {} |
| 82 | ~IPCMutexLocker() { |
| 83 | if (__builtin_expect(!owner_died_checked_, false)) { |
| 84 | ::aos::Die("nobody checked if the previous owner of mutex %p died", this); |
| 85 | } |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 86 | mutex_->Unlock(); |
| 87 | } |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame^] | 88 | |
| 89 | // Whether or not the previous owner died. If this is not called at least |
| 90 | // once, the destructor will ::aos::Die. |
| 91 | __attribute__((warn_unused_result)) bool owner_died() { |
| 92 | owner_died_checked_ = true; |
| 93 | return __builtin_expect(owner_died_, false); |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | private: |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame^] | 97 | Mutex *const mutex_; |
| 98 | const bool owner_died_; |
| 99 | bool owner_died_checked_ = false; |
| 100 | |
| 101 | DISALLOW_COPY_AND_ASSIGN(IPCMutexLocker); |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 102 | }; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 103 | |
| 104 | } // namespace aos |
| 105 | |
| 106 | #endif // AOS_COMMON_MUTEX_H_ |