Brian Silverman | 7b266d9 | 2021-02-17 21:24:02 -0800 | [diff] [blame] | 1 | #include "aos/ipc_lib/event.h" |
Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 2 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 3 | #include <chrono> |
| 4 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 5 | #include "aos/type_traits/type_traits.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 6 | #include "glog/logging.h" |
Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 7 | |
| 8 | namespace aos { |
| 9 | |
| 10 | Event::Event() : impl_(0) { |
| 11 | static_assert(shm_ok<Event>::value, |
| 12 | "Event is not safe for use in shared memory."); |
| 13 | } |
| 14 | |
| 15 | void Event::Wait() { |
Brian Silverman | 408511d | 2016-09-10 16:12:02 -0400 | [diff] [blame] | 16 | while (__atomic_load_n(&impl_, __ATOMIC_SEQ_CST) == 0) { |
| 17 | const int ret = futex_wait(&impl_); |
| 18 | if (ret != 0) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 19 | CHECK_EQ(-1, ret); |
| 20 | PLOG(FATAL) << "futex_wait(" << &impl_ << ") failed"; |
Brian Silverman | 408511d | 2016-09-10 16:12:02 -0400 | [diff] [blame] | 21 | } |
| 22 | } |
Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 23 | } |
| 24 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 25 | bool Event::WaitTimeout(monotonic_clock::duration timeout) { |
| 26 | ::std::chrono::seconds sec = |
| 27 | ::std::chrono::duration_cast<::std::chrono::seconds>(timeout); |
| 28 | ::std::chrono::nanoseconds nsec = |
| 29 | ::std::chrono::duration_cast<::std::chrono::nanoseconds>(timeout - sec); |
| 30 | struct timespec timeout_timespec; |
| 31 | timeout_timespec.tv_sec = sec.count(); |
| 32 | timeout_timespec.tv_nsec = nsec.count(); |
Brian Silverman | 408511d | 2016-09-10 16:12:02 -0400 | [diff] [blame] | 33 | while (true) { |
| 34 | if (__atomic_load_n(&impl_, __ATOMIC_SEQ_CST) != 0) { |
| 35 | return true; |
| 36 | } |
| 37 | const int ret = futex_wait_timeout(&impl_, &timeout_timespec); |
| 38 | if (ret != 0) { |
| 39 | if (ret == 2) return false; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 40 | CHECK_EQ(-1, ret); |
| 41 | PLOG(FATAL) << "futex_wait(" << &impl_ << ") failed"; |
Brian Silverman | 408511d | 2016-09-10 16:12:02 -0400 | [diff] [blame] | 42 | } |
| 43 | } |
Brian Silverman | 3060894 | 2015-04-08 19:16:46 -0400 | [diff] [blame] | 44 | } |
| 45 | |
Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 46 | // We're not going to expose the number woken because that's not easily portable |
| 47 | // to condition variable-based implementations. |
| 48 | void Event::Set() { |
| 49 | if (futex_set(&impl_) == -1) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 50 | PLOG(FATAL) << "futex_set(" << &impl_ << ") failed"; |
Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
Brian Silverman | 7b266d9 | 2021-02-17 21:24:02 -0800 | [diff] [blame] | 54 | bool Event::Clear() { return !futex_unset(&impl_); } |
Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 55 | |
| 56 | } // namespace aos |