John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/mutex/mutex.h" |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 2 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 3 | #include <cinttypes> |
| 4 | #include <cstdio> |
| 5 | #include <cstring> |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 6 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | #include "glog/logging.h" |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 8 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 9 | #include "aos/type_traits/type_traits.h" |
| 10 | |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 11 | namespace aos { |
| 12 | |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 13 | // Lock and Unlock use the return values of mutex_lock/mutex_unlock |
| 14 | // to determine whether the lock/unlock succeeded. |
| 15 | |
| 16 | bool Mutex::Lock() { |
| 17 | const int ret = mutex_grab(&impl_); |
| 18 | if (ret == 0) { |
| 19 | return false; |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 20 | } else if (ret == 1) { |
| 21 | return true; |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 22 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 23 | LOG(FATAL) << "mutex_grab(" << &impl_ << "(=" << std::hex << impl_.futex |
| 24 | << ")) failed with " << ret; |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 25 | } |
| 26 | } |
| 27 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 28 | void Mutex::Unlock() { mutex_unlock(&impl_); } |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 29 | |
| 30 | Mutex::State Mutex::TryLock() { |
| 31 | const int ret = mutex_trylock(&impl_); |
| 32 | switch (ret) { |
| 33 | case 0: |
| 34 | return State::kLocked; |
Brian Silverman | 71c55c5 | 2014-08-19 14:31:59 -0400 | [diff] [blame] | 35 | case 1: |
| 36 | return State::kOwnerDied; |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 37 | case 4: |
Daniel Petti | 88a1566 | 2015-04-12 17:42:22 -0400 | [diff] [blame] | 38 | return State::kLockFailed; |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 39 | default: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 40 | LOG(FATAL) << "mutex_trylock(" << &impl_ << "(=" << std::hex |
| 41 | << impl_.futex << ")) failed with " << ret; |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 45 | bool Mutex::OwnedBySelf() const { return mutex_islocked(&impl_); } |
Brian Silverman | 1dfe48b | 2014-09-06 16:13:02 -0400 | [diff] [blame] | 46 | |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 47 | } // namespace aos |