blob: d1f0ef2959d79b267c96b8b083cae8b8078e389d [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#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
9namespace aos {
10
11Mutex::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
19void 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
26void 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
33bool Mutex::TryLock() {
34 return mutex_trylock(&impl_) == 0;
35}
36
37} // namespace aos