John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/condition.h" |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 2 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 3 | #include <cassert> |
| 4 | #include <cinttypes> |
| 5 | #include <ctime> |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 6 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 7 | #include "absl/log/check.h" |
| 8 | #include "absl/log/log.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 9 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 10 | #include "aos/mutex/mutex.h" |
Austin Schuh | 0ad2b6f | 2019-06-09 21:27:07 -0700 | [diff] [blame] | 11 | #include "aos/type_traits/type_traits.h" |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 12 | |
| 13 | namespace aos { |
| 14 | |
Austin Schuh | 0ad2b6f | 2019-06-09 21:27:07 -0700 | [diff] [blame] | 15 | namespace chrono = ::std::chrono; |
| 16 | |
Brian Silverman | 7f365e3 | 2014-01-01 17:59:01 -0800 | [diff] [blame] | 17 | static_assert(shm_ok<Condition>::value, |
| 18 | "Condition should work in shared memory"); |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 19 | |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 20 | Condition::Condition(Mutex *m) : impl_(), m_(m) {} |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 21 | |
Austin Schuh | f4b194e | 2014-09-21 10:26:41 -0700 | [diff] [blame] | 22 | bool Condition::Wait() { |
Austin Schuh | 0ad2b6f | 2019-06-09 21:27:07 -0700 | [diff] [blame] | 23 | const int ret = condition_wait(&impl_, &m_->impl_, nullptr); |
Brian Silverman | dc1eb27 | 2014-08-19 14:25:59 -0400 | [diff] [blame] | 24 | assert(__builtin_expect(ret == 0 || ret == 1, 1)); |
| 25 | return ret == 1; |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Austin Schuh | 0ad2b6f | 2019-06-09 21:27:07 -0700 | [diff] [blame] | 28 | Condition::WaitResult Condition::WaitTimed(chrono::nanoseconds timeout) { |
| 29 | struct timespec end_time; |
| 30 | const bool do_timeout = timeout != chrono::nanoseconds(0); |
| 31 | |
| 32 | if (do_timeout) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 33 | PCHECK(clock_gettime(CLOCK_MONOTONIC, &end_time) == 0); |
Austin Schuh | 0ad2b6f | 2019-06-09 21:27:07 -0700 | [diff] [blame] | 34 | timeout += chrono::nanoseconds(end_time.tv_nsec); |
| 35 | chrono::seconds timeout_seconds = |
| 36 | chrono::duration_cast<chrono::seconds>(timeout); |
| 37 | end_time.tv_sec += timeout_seconds.count(); |
| 38 | end_time.tv_nsec = (timeout - timeout_seconds).count(); |
| 39 | } |
| 40 | |
| 41 | const int ret = |
| 42 | condition_wait(&impl_, &m_->impl_, do_timeout ? &end_time : nullptr); |
| 43 | assert(__builtin_expect(ret == 0 || ret == 1 || ret == -1, 1)); |
| 44 | switch (ret) { |
| 45 | case 0: |
| 46 | return WaitResult::kOk; |
| 47 | case 1: |
| 48 | return WaitResult::kOwnerDied; |
| 49 | default: |
| 50 | return WaitResult::kTimeout; |
| 51 | } |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 52 | } |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 53 | |
Austin Schuh | 0ad2b6f | 2019-06-09 21:27:07 -0700 | [diff] [blame] | 54 | void Condition::Signal() { condition_signal(&impl_, &m_->impl_); } |
| 55 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 56 | void Condition::Broadcast() { condition_broadcast(&impl_, &m_->impl_); } |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 57 | |
| 58 | } // namespace aos |