brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #include "aos/common/mutex.h" |
| 2 | |
| 3 | #include <inttypes.h> |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 4 | #include <stdio.h> |
| 5 | #include <string.h> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 6 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 7 | #include "aos/common/type_traits.h" |
Brian Silverman | 598800f | 2013-05-09 17:08:42 -0700 | [diff] [blame] | 8 | #include "aos/common/logging/logging.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 9 | |
| 10 | namespace aos { |
| 11 | |
| 12 | Mutex::Mutex() : impl_(0) { |
| 13 | static_assert(shm_ok<Mutex>::value, |
| 14 | "Mutex is not safe for use in shared memory."); |
| 15 | } |
| 16 | |
| 17 | // Lock and Unlock use the return values of mutex_lock/mutex_unlock |
| 18 | // to determine whether the lock/unlock succeeded. |
| 19 | |
Austin Schuh | f4b194e | 2014-09-21 10:26:41 -0700 | [diff] [blame^] | 20 | bool Mutex::Lock() { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 21 | if (mutex_grab(&impl_) != 0) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 22 | PLOG(FATAL, "mutex_grab(%p(=%" PRIu32 ")) failed", &impl_, impl_); |
Austin Schuh | f4b194e | 2014-09-21 10:26:41 -0700 | [diff] [blame^] | 23 | } else { |
| 24 | return false; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 25 | } |
| 26 | } |
| 27 | |
| 28 | void Mutex::Unlock() { |
Brian Silverman | af221b8 | 2013-09-01 13:57:50 -0700 | [diff] [blame] | 29 | mutex_unlock(&impl_); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | bool Mutex::TryLock() { |
| 33 | return mutex_trylock(&impl_) == 0; |
| 34 | } |
| 35 | |
| 36 | } // namespace aos |