blob: 4f908ddeec1d2e8f61c5e40dd1b339051c95b082 [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
brians343bc112013-02-10 01:53:46 +00008#include "aos/common/type_traits.h"
Brian Silverman598800f2013-05-09 17:08:42 -07009#include "aos/common/logging/logging.h"
brians343bc112013-02-10 01:53:46 +000010
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) {
Brian Silverman8efe23e2013-07-07 23:31:37 -070023 LOG(FATAL, "mutex_grab(%p(=%" PRIu32 ")) failed because of %d: %s\n",
brians343bc112013-02-10 01:53:46 +000024 &impl_, impl_, errno, strerror(errno));
25 }
26}
27
28void Mutex::Unlock() {
29 if (mutex_unlock(&impl_) != 0) {
Brian Silverman8efe23e2013-07-07 23:31:37 -070030 LOG(FATAL, "mutex_unlock(%p(=%" PRIu32 ")) failed because of %d: %s\n",
brians343bc112013-02-10 01:53:46 +000031 &impl_, impl_, errno, strerror(errno));
32 }
33}
34
35bool Mutex::TryLock() {
36 return mutex_trylock(&impl_) == 0;
37}
38
39} // namespace aos