blob: e95b45ee31dc9e03ea0191d9bb1155728c10a634 [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
23// We're not going to expose the number woken because that's not easily portable
24// to condition variable-based implementations.
25void Event::Set() {
26 if (futex_set(&impl_) == -1) {
27 PLOG(FATAL, "futex_set(%p) failed", &impl_);
28 }
29}
30
31bool Event::Clear() {
32 return !futex_unset(&impl_);
33}
34
35} // namespace aos