Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame^] | 1 | #include "aos/common/condition.h" |
| 2 | |
| 3 | #include <inttypes.h> |
| 4 | |
| 5 | #include "aos/common/type_traits.h" |
| 6 | |
| 7 | namespace aos { |
| 8 | |
| 9 | static_assert(shm_ok<Condition>::value, "Condition should work" |
| 10 | " in shared memory"); |
| 11 | |
| 12 | Condition::Condition() : impl_(0) {} |
| 13 | |
| 14 | bool Condition::Wait() { |
| 15 | switch (condition_wait(&impl_)) { |
| 16 | case 1: |
| 17 | return false; |
| 18 | case 0: |
| 19 | return true; |
| 20 | default: |
| 21 | if (errno != EINTR) { |
| 22 | LOG(FATAL, "condition_wait(%p(=%"PRIu32")) failed because of %d: %s\n", |
| 23 | &impl_, impl_, errno, strerror(errno)); |
| 24 | } |
| 25 | return false; |
| 26 | } |
| 27 | } |
| 28 | bool Condition::WaitNext() { |
| 29 | switch (condition_wait_force(&impl_)) { |
| 30 | case 1: |
| 31 | return false; |
| 32 | case 0: |
| 33 | return true; |
| 34 | default: |
| 35 | if (errno != EINTR) { |
| 36 | LOG(FATAL, "condition_wait_force(%p(=%"PRIu32")) failed" |
| 37 | " because of %d: %s\n", &impl_, impl_, errno, strerror(errno)); |
| 38 | } |
| 39 | return false; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | void Condition::Set() { |
| 44 | if (condition_set(&impl_) == -1) { |
| 45 | LOG(FATAL, "condition_set(%p(=%"PRIu32")) failed because of %d: %s\n", |
| 46 | &impl_, impl_, errno, strerror(errno)); |
| 47 | } |
| 48 | } |
| 49 | void Condition::Unset() { |
| 50 | // can not fail |
| 51 | condition_unset(&impl_); |
| 52 | } |
| 53 | |
| 54 | } // namespace aos |