brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #include "aos/common/mutex.h" |
| 2 | |
| 3 | #include <inttypes.h> |
| 4 | #include <errno.h> |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 5 | #include <stdio.h> |
| 6 | #include <string.h> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 7 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 8 | #include "aos/common/type_traits.h" |
Brian Silverman | 598800f | 2013-05-09 17:08:42 -0700 | [diff] [blame] | 9 | #include "aos/common/logging/logging.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 10 | |
| 11 | namespace aos { |
| 12 | |
| 13 | Mutex::Mutex() : impl_(0) { |
| 14 | static_assert(shm_ok<Mutex>::value, |
| 15 | "Mutex is not safe for use in shared memory."); |
| 16 | } |
| 17 | |
| 18 | // Lock and Unlock use the return values of mutex_lock/mutex_unlock |
| 19 | // to determine whether the lock/unlock succeeded. |
| 20 | |
| 21 | void Mutex::Lock() { |
| 22 | if (mutex_grab(&impl_) != 0) { |
Brian Silverman | 8efe23e | 2013-07-07 23:31:37 -0700 | [diff] [blame] | 23 | LOG(FATAL, "mutex_grab(%p(=%" PRIu32 ")) failed because of %d: %s\n", |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 24 | &impl_, impl_, errno, strerror(errno)); |
| 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 |