blob: 4753e781aef4e3b012f6f0331b300259f523ab18 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include "aos/common/mutex.h"
2
3#include <inttypes.h>
4#include <errno.h>
Brian Silvermanf665d692013-02-17 22:11:39 -08005#include <stdio.h>
6#include <string.h>
brians343bc112013-02-10 01:53:46 +00007
8#include "aos/aos_core.h"
9#include "aos/common/type_traits.h"
10
11namespace aos {
12
13Mutex::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
21void Mutex::Lock() {
22 if (mutex_grab(&impl_) != 0) {
23 LOG(FATAL, "mutex_grab(%p(=%"PRIu32")) failed because of %d: %s\n",
24 &impl_, impl_, errno, strerror(errno));
25 }
26}
27
28void Mutex::Unlock() {
29 if (mutex_unlock(&impl_) != 0) {
30 LOG(FATAL, "mutex_unlock(%p(=%"PRIu32")) failed because of %d: %s\n",
31 &impl_, impl_, errno, strerror(errno));
32 }
33}
34
35bool Mutex::TryLock() {
36 return mutex_trylock(&impl_) == 0;
37}
38
39} // namespace aos