forked from frc971/971-Robot-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcondition.cc
58 lines (44 loc) · 1.53 KB
/
condition.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "aos/condition.h"
#include <cassert>
#include <cinttypes>
#include <ctime>
#include "absl/log/check.h"
#include "absl/log/log.h"
#include "aos/mutex/mutex.h"
#include "aos/type_traits/type_traits.h"
namespace aos {
namespace chrono = ::std::chrono;
static_assert(shm_ok<Condition>::value,
"Condition should work in shared memory");
Condition::Condition(Mutex *m) : impl_(), m_(m) {}
bool Condition::Wait() {
const int ret = condition_wait(&impl_, &m_->impl_, nullptr);
assert(__builtin_expect(ret == 0 || ret == 1, 1));
return ret == 1;
}
Condition::WaitResult Condition::WaitTimed(chrono::nanoseconds timeout) {
struct timespec end_time;
const bool do_timeout = timeout != chrono::nanoseconds(0);
if (do_timeout) {
PCHECK(clock_gettime(CLOCK_MONOTONIC, &end_time) == 0);
timeout += chrono::nanoseconds(end_time.tv_nsec);
chrono::seconds timeout_seconds =
chrono::duration_cast<chrono::seconds>(timeout);
end_time.tv_sec += timeout_seconds.count();
end_time.tv_nsec = (timeout - timeout_seconds).count();
}
const int ret =
condition_wait(&impl_, &m_->impl_, do_timeout ? &end_time : nullptr);
assert(__builtin_expect(ret == 0 || ret == 1 || ret == -1, 1));
switch (ret) {
case 0:
return WaitResult::kOk;
case 1:
return WaitResult::kOwnerDied;
default:
return WaitResult::kTimeout;
}
}
void Condition::Signal() { condition_signal(&impl_, &m_->impl_); }
void Condition::Broadcast() { condition_broadcast(&impl_, &m_->impl_); }
} // namespace aos