blob: ae32d391d622ea47110f39dfb8f4e0582fc2c2ff [file] [log] [blame]
Brian Silvermanf5f34902015-03-29 17:57:59 -04001#include "aos/common/event.h"
2
3#include "aos/common/type_traits.h"
4#include "aos/common/logging/logging.h"
5
6namespace aos {
7
8Event::Event() : impl_(0) {
9 static_assert(shm_ok<Event>::value,
10 "Event is not safe for use in shared memory.");
11}
12
13void Event::Wait() {
14 int ret;
15 do {
16 ret = futex_wait(&impl_);
17 } while (ret == 1);
18 if (ret == 0) return;
19 CHECK_EQ(-1, ret);
20 PLOG(FATAL, "futex_wait(%p) failed", &impl_);
21}
22
Brian Silverman30608942015-04-08 19:16:46 -040023bool Event::WaitTimeout(const ::aos::time::Time &timeout) {
24 const auto timeout_timespec = timeout.ToTimespec();
25 int ret;
26 do {
27 ret = futex_wait_timeout(&impl_, &timeout_timespec);
28 } while (ret == 1);
29 if (ret == 0) return true;
30 if (ret == 2) return false;
31 CHECK_EQ(-1, ret);
32 PLOG(FATAL, "futex_wait(%p) failed", &impl_);
33}
34
Brian Silvermanf5f34902015-03-29 17:57:59 -040035// We're not going to expose the number woken because that's not easily portable
36// to condition variable-based implementations.
37void Event::Set() {
38 if (futex_set(&impl_) == -1) {
39 PLOG(FATAL, "futex_set(%p) failed", &impl_);
40 }
41}
42
43bool Event::Clear() {
44 return !futex_unset(&impl_);
45}
46
47} // namespace aos