blob: db1b012d746a7524a23585d4ad5852fbdb8f22ea [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:
Brian Silvermandc1eb272014-08-19 14:25:59 -040023 enum class State {
24 kLocked, kUnlocked
25 };
26
brians343bc112013-02-10 01:53:46 +000027 // Creates an unlocked mutex.
28 Mutex();
Brian Silvermandc1eb272014-08-19 14:25:59 -040029 // 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.
brians343bc112013-02-10 01:53:46 +000033 ~Mutex();
Brian Silvermandc1eb272014-08-19 14:25:59 -040034
brians343bc112013-02-10 01:53:46 +000035 // Locks the mutex. If it fails, it calls LOG(FATAL).
Austin Schuhf4b194e2014-09-21 10:26:41 -070036 // Returns false.
Brian Silvermandc1eb272014-08-19 14:25:59 -040037 bool Lock() __attribute__((warn_unused_result));
brians343bc112013-02-10 01:53:46 +000038 // Unlocks the mutex. Fails like Lock.
Brian Silverman6da04272014-05-18 18:47:48 -070039 // Multiple unlocking is undefined.
brians343bc112013-02-10 01:53:46 +000040 void Unlock();
41 // Locks the mutex unless it is already locked.
Brian Silvermandc1eb272014-08-19 14:25:59 -040042 // Returns the new state of the mutex.
brians343bc112013-02-10 01:53:46 +000043 // Doesn't wait for the mutex to be unlocked if it is locked.
Brian Silvermandc1eb272014-08-19 14:25:59 -040044 State TryLock() __attribute__((warn_unused_result));
brians343bc112013-02-10 01:53:46 +000045
46 private:
Brian Silvermandc1eb272014-08-19 14:25:59 -040047 aos_mutex impl_;
Brian Silverman08661c72013-09-01 17:24:38 -070048
49 friend class Condition; // for access to impl_
brians343bc112013-02-10 01:53:46 +000050};
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 Silvermandc1eb272014-08-19 14:25:59 -040055// 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.
brians343bc112013-02-10 01:53:46 +000058class MutexLocker {
59 public:
60 explicit MutexLocker(Mutex *mutex) : mutex_(mutex) {
Brian Silvermandc1eb272014-08-19 14:25:59 -040061 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 }
brians343bc112013-02-10 01:53:46 +000065 }
66 ~MutexLocker() {
67 mutex_->Unlock();
68 }
69
70 private:
Brian Silvermandc1eb272014-08-19 14:25:59 -040071 Mutex *const mutex_;
72
brians343bc112013-02-10 01:53:46 +000073 DISALLOW_COPY_AND_ASSIGN(MutexLocker);
74};
Brian Silvermandc1eb272014-08-19 14:25:59 -040075
76// A version of MutexLocker which reports the previous owner dying instead of
77// immediately LOG(FATAL)ing.
78class IPCMutexLocker {
Brian Silvermand41b4422013-09-01 14:02:33 -070079 public:
Brian Silvermandc1eb272014-08-19 14:25:59 -040080 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 Silvermand41b4422013-09-01 14:02:33 -070086 mutex_->Unlock();
87 }
Brian Silvermandc1eb272014-08-19 14:25:59 -040088
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 Silvermand41b4422013-09-01 14:02:33 -070094 }
95
96 private:
Brian Silvermandc1eb272014-08-19 14:25:59 -040097 Mutex *const mutex_;
98 const bool owner_died_;
99 bool owner_died_checked_ = false;
100
101 DISALLOW_COPY_AND_ASSIGN(IPCMutexLocker);
Brian Silvermand41b4422013-09-01 14:02:33 -0700102};
brians343bc112013-02-10 01:53:46 +0000103
104} // namespace aos
105
106#endif // AOS_COMMON_MUTEX_H_