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