blob: ca35b1069e0857dafef61f472788f40fdd78a506 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#ifndef AOS_COMMON_MUTEX_H_
2#define AOS_COMMON_MUTEX_H_
3
brians343bc112013-02-10 01:53:46 +00004#include "aos/common/macros.h"
Brian Silverman14fd0fb2014-01-14 21:42:01 -08005#include "aos/linux_code/ipc_lib/aos_sync.h"
Brian Silvermandc1eb272014-08-19 14:25:59 -04006#include "aos/common/die.h"
brians343bc112013-02-10 01:53:46 +00007
8namespace aos {
9
Brian Silverman08661c72013-09-01 17:24:38 -070010class Condition;
11
Brian Silvermandc1eb272014-08-19 14:25:59 -040012// 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,
brians343bc112013-02-10 01:53:46 +000015// higher priority ones will succeed in locking first,
16// and tasks of equal priorities have the same chance of getting the lock.
Brian Silvermandc1eb272014-08-19 14:25:59 -040017// 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).
brians343bc112013-02-10 01:53:46 +000021class Mutex {
22 public:
Daniel Petti88a15662015-04-12 17:42:22 -040023 // States that signify the result of TryLock.
Brian Silvermandc1eb272014-08-19 14:25:59 -040024 enum class State {
Daniel Petti88a15662015-04-12 17:42:22 -040025 // The mutex was acquired successfully.
26 kLocked,
27 // TryLock tried to grab the mutex and failed.
28 kLockFailed
Brian Silvermandc1eb272014-08-19 14:25:59 -040029 };
30
brians343bc112013-02-10 01:53:46 +000031 // Creates an unlocked mutex.
32 Mutex();
Brian Silvermandc1eb272014-08-19 14:25:59 -040033 // 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.
brians343bc112013-02-10 01:53:46 +000037 ~Mutex();
Brian Silvermandc1eb272014-08-19 14:25:59 -040038
brians343bc112013-02-10 01:53:46 +000039 // Locks the mutex. If it fails, it calls LOG(FATAL).
Austin Schuhf4b194e2014-09-21 10:26:41 -070040 // Returns false.
Brian Silvermandc1eb272014-08-19 14:25:59 -040041 bool Lock() __attribute__((warn_unused_result));
brians343bc112013-02-10 01:53:46 +000042 // Unlocks the mutex. Fails like Lock.
Brian Silverman6da04272014-05-18 18:47:48 -070043 // Multiple unlocking is undefined.
brians343bc112013-02-10 01:53:46 +000044 void Unlock();
45 // Locks the mutex unless it is already locked.
Brian Silvermandc1eb272014-08-19 14:25:59 -040046 // Returns the new state of the mutex.
brians343bc112013-02-10 01:53:46 +000047 // Doesn't wait for the mutex to be unlocked if it is locked.
Brian Silvermandc1eb272014-08-19 14:25:59 -040048 State TryLock() __attribute__((warn_unused_result));
brians343bc112013-02-10 01:53:46 +000049
Brian Silverman1dfe48b2014-09-06 16:13:02 -040050 // Returns true iff the current task has this mutex locked.
51 // This is mainly for IPCRecursiveMutexLocker to use.
52 bool OwnedBySelf() const;
53
brians343bc112013-02-10 01:53:46 +000054 private:
Brian Silvermandc1eb272014-08-19 14:25:59 -040055 aos_mutex impl_;
Brian Silverman08661c72013-09-01 17:24:38 -070056
57 friend class Condition; // for access to impl_
brians343bc112013-02-10 01:53:46 +000058};
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 Silvermandc1eb272014-08-19 14:25:59 -040063// 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.
brians343bc112013-02-10 01:53:46 +000066class MutexLocker {
67 public:
68 explicit MutexLocker(Mutex *mutex) : mutex_(mutex) {
Brian Silvermandc1eb272014-08-19 14:25:59 -040069 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 }
brians343bc112013-02-10 01:53:46 +000073 }
74 ~MutexLocker() {
75 mutex_->Unlock();
76 }
77
78 private:
Brian Silvermandc1eb272014-08-19 14:25:59 -040079 Mutex *const mutex_;
80
brians343bc112013-02-10 01:53:46 +000081 DISALLOW_COPY_AND_ASSIGN(MutexLocker);
82};
Brian Silvermandc1eb272014-08-19 14:25:59 -040083
84// A version of MutexLocker which reports the previous owner dying instead of
85// immediately LOG(FATAL)ing.
86class IPCMutexLocker {
Brian Silvermand41b4422013-09-01 14:02:33 -070087 public:
Brian Silvermandc1eb272014-08-19 14:25:59 -040088 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 Silvermand41b4422013-09-01 14:02:33 -070094 mutex_->Unlock();
95 }
Brian Silvermandc1eb272014-08-19 14:25:59 -040096
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 Silvermand41b4422013-09-01 14:02:33 -0700102 }
103
104 private:
Brian Silvermandc1eb272014-08-19 14:25:59 -0400105 Mutex *const mutex_;
106 const bool owner_died_;
107 bool owner_died_checked_ = false;
108
109 DISALLOW_COPY_AND_ASSIGN(IPCMutexLocker);
Brian Silvermand41b4422013-09-01 14:02:33 -0700110};
brians343bc112013-02-10 01:53:46 +0000111
Brian Silverman1dfe48b2014-09-06 16:13:02 -0400112// A version of IPCMutexLocker which only locks (and unlocks) the mutex if the
113// current task does not already hold it.
114class 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
brians343bc112013-02-10 01:53:46 +0000142} // namespace aos
143
144#endif // AOS_COMMON_MUTEX_H_