add an implementation of python-style events
This is another synchronization primitive. It will be used in a couple
of extra tests coming right after this.
Change-Id: I536f04f0b5f4f87a0c0a4c8e531c526c7a56bb49
diff --git a/aos/linux_code/ipc_lib/event.cc b/aos/linux_code/ipc_lib/event.cc
new file mode 100644
index 0000000..e95b45e
--- /dev/null
+++ b/aos/linux_code/ipc_lib/event.cc
@@ -0,0 +1,35 @@
+#include "aos/common/event.h"
+
+#include "aos/common/type_traits.h"
+#include "aos/common/logging/logging.h"
+
+namespace aos {
+
+Event::Event() : impl_(0) {
+ static_assert(shm_ok<Event>::value,
+ "Event is not safe for use in shared memory.");
+}
+
+void Event::Wait() {
+ int ret;
+ do {
+ ret = futex_wait(&impl_);
+ } while (ret == 1);
+ if (ret == 0) return;
+ CHECK_EQ(-1, ret);
+ PLOG(FATAL, "futex_wait(%p) failed", &impl_);
+}
+
+// We're not going to expose the number woken because that's not easily portable
+// to condition variable-based implementations.
+void Event::Set() {
+ if (futex_set(&impl_) == -1) {
+ PLOG(FATAL, "futex_set(%p) failed", &impl_);
+ }
+}
+
+bool Event::Clear() {
+ return !futex_unset(&impl_);
+}
+
+} // namespace aos