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> |
| 5 | |
| 6 | #include "aos/aos_core.h" |
| 7 | #include "aos/common/type_traits.h" |
| 8 | |
| 9 | namespace aos { |
| 10 | |
| 11 | Mutex::Mutex() : impl_(0) { |
| 12 | static_assert(shm_ok<Mutex>::value, |
| 13 | "Mutex is not safe for use in shared memory."); |
| 14 | } |
| 15 | |
| 16 | // Lock and Unlock use the return values of mutex_lock/mutex_unlock |
| 17 | // to determine whether the lock/unlock succeeded. |
| 18 | |
| 19 | void Mutex::Lock() { |
| 20 | if (mutex_grab(&impl_) != 0) { |
| 21 | LOG(FATAL, "mutex_grab(%p(=%"PRIu32")) failed because of %d: %s\n", |
| 22 | &impl_, impl_, errno, strerror(errno)); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | void Mutex::Unlock() { |
| 27 | if (mutex_unlock(&impl_) != 0) { |
| 28 | LOG(FATAL, "mutex_unlock(%p(=%"PRIu32")) failed because of %d: %s\n", |
| 29 | &impl_, impl_, errno, strerror(errno)); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | bool Mutex::TryLock() { |
| 34 | return mutex_trylock(&impl_) == 0; |
| 35 | } |
| 36 | |
| 37 | } // namespace aos |