blob: 4bd0759a4cb5399644e9b7839835b60b490ca091 [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
Austin Schuhf4b194e2014-09-21 10:26:41 -070020bool Mutex::Lock() {
brians343bc112013-02-10 01:53:46 +000021 if (mutex_grab(&impl_) != 0) {
Brian Silverman01be0002014-05-10 15:44:38 -070022 PLOG(FATAL, "mutex_grab(%p(=%" PRIu32 ")) failed", &impl_, impl_);
Austin Schuhf4b194e2014-09-21 10:26:41 -070023 } else {
24 return false;
brians343bc112013-02-10 01:53:46 +000025 }
26}
27
28void Mutex::Unlock() {
Brian Silvermanaf221b82013-09-01 13:57:50 -070029 mutex_unlock(&impl_);
brians343bc112013-02-10 01:53:46 +000030}
31
32bool Mutex::TryLock() {
33 return mutex_trylock(&impl_) == 0;
34}
35
36} // namespace aos