Removed Common
Change-Id: I01ea8f07220375c2ad9bc0092281d4f27c642303
diff --git a/aos/mutex/mutex.cc b/aos/mutex/mutex.cc
new file mode 100644
index 0000000..a35f0cd
--- /dev/null
+++ b/aos/mutex/mutex.cc
@@ -0,0 +1,50 @@
+#include "aos/mutex/mutex.h"
+
+#include <inttypes.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "aos/type_traits/type_traits.h"
+#include "aos/logging/logging.h"
+
+namespace aos {
+
+// Lock and Unlock use the return values of mutex_lock/mutex_unlock
+// to determine whether the lock/unlock succeeded.
+
+bool Mutex::Lock() {
+ const int ret = mutex_grab(&impl_);
+ if (ret == 0) {
+ return false;
+ } else if (ret == 1) {
+ return true;
+ } else {
+ LOG(FATAL, "mutex_grab(%p(=%" PRIu32 ")) failed with %d\n",
+ &impl_, impl_.futex, ret);
+ }
+}
+
+void Mutex::Unlock() {
+ mutex_unlock(&impl_);
+}
+
+Mutex::State Mutex::TryLock() {
+ const int ret = mutex_trylock(&impl_);
+ switch (ret) {
+ case 0:
+ return State::kLocked;
+ case 1:
+ return State::kOwnerDied;
+ case 4:
+ return State::kLockFailed;
+ default:
+ LOG(FATAL, "mutex_trylock(%p(=%" PRIu32 ")) failed with %d\n",
+ &impl_, impl_.futex, ret);
+ }
+}
+
+bool Mutex::OwnedBySelf() const {
+ return mutex_islocked(&impl_);
+}
+
+} // namespace aos