blob: 15247735db2a86f761fc406eed8da1b2f54d2778 [file] [log] [blame]
Brian Silvermand41b4422013-09-01 14:02:33 -07001#include "aos/common/condition.h"
2
3#include <inttypes.h>
4
5#include "aos/common/type_traits.h"
6
7namespace aos {
8
9static_assert(shm_ok<Condition>::value, "Condition should work"
10 " in shared memory");
11
12Condition::Condition() : impl_(0) {}
13
14bool 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}
28bool 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
43void 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}
49void Condition::Unset() {
50 // can not fail
51 condition_unset(&impl_);
52}
53
54} // namespace aos