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