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