Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 1 | #include "aos/common/event.h" |
| 2 | |
| 3 | #include "aos/common/type_traits.h" |
| 4 | #include "aos/common/logging/logging.h" |
| 5 | |
| 6 | namespace aos { |
| 7 | |
| 8 | Event::Event() : impl_(0) { |
| 9 | static_assert(shm_ok<Event>::value, |
| 10 | "Event is not safe for use in shared memory."); |
| 11 | } |
| 12 | |
| 13 | void 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 Silverman | 3060894 | 2015-04-08 19:16:46 -0400 | [diff] [blame] | 23 | bool 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 Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 35 | // We're not going to expose the number woken because that's not easily portable |
| 36 | // to condition variable-based implementations. |
| 37 | void Event::Set() { |
| 38 | if (futex_set(&impl_) == -1) { |
| 39 | PLOG(FATAL, "futex_set(%p) failed", &impl_); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | bool Event::Clear() { |
| 44 | return !futex_unset(&impl_); |
| 45 | } |
| 46 | |
| 47 | } // namespace aos |