blob: 8c9820476ef3f8956957926dfc629090ad06c565 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include "aos/common/mutex.h"
2
3#include <inttypes.h>
Brian Silvermanf665d692013-02-17 22:11:39 -08004#include <stdio.h>
5#include <string.h>
brians343bc112013-02-10 01:53:46 +00006
brians343bc112013-02-10 01:53:46 +00007#include "aos/common/type_traits.h"
Brian Silverman598800f2013-05-09 17:08:42 -07008#include "aos/common/logging/logging.h"
brians343bc112013-02-10 01:53:46 +00009
10namespace aos {
11
12Mutex::Mutex() : impl_(0) {
13 static_assert(shm_ok<Mutex>::value,
14 "Mutex is not safe for use in shared memory.");
15}
16
17// Lock and Unlock use the return values of mutex_lock/mutex_unlock
18// to determine whether the lock/unlock succeeded.
19
20void Mutex::Lock() {
21 if (mutex_grab(&impl_) != 0) {
Brian Silverman01be0002014-05-10 15:44:38 -070022 PLOG(FATAL, "mutex_grab(%p(=%" PRIu32 ")) failed", &impl_, impl_);
brians343bc112013-02-10 01:53:46 +000023 }
24}
25
26void Mutex::Unlock() {
Brian Silvermanaf221b82013-09-01 13:57:50 -070027 mutex_unlock(&impl_);
brians343bc112013-02-10 01:53:46 +000028}
29
30bool Mutex::TryLock() {
31 return mutex_trylock(&impl_) == 0;
32}
33
34} // namespace aos