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