-
-
Notifications
You must be signed in to change notification settings - Fork 217
/
Copy pathtimer.cpp
66 lines (51 loc) · 1.41 KB
/
timer.cpp
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
59
60
61
62
63
64
65
66
/*
* Copyright (c) 2020 Roc Streaming authors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "roc_core/timer.h"
#include "roc_core/panic.h"
namespace roc {
namespace core {
Timer::Timer()
: sem_(0)
, sem_post_flag_(false)
, deadline_(0)
, next_wakeup_(0) {
}
bool Timer::try_set_deadline(nanoseconds_t new_deadline) {
if (!deadline_.try_store(new_deadline)) {
return false;
}
nanoseconds_t next_wakeup;
if (!next_wakeup_.try_load(next_wakeup)) {
next_wakeup = -1;
}
if (next_wakeup < 0 || (new_deadline >= 0 && new_deadline < next_wakeup)) {
if (sem_post_flag_.compare_exchange(false, true)) {
sem_.post();
}
}
return true;
}
void Timer::wait_deadline() {
for (;;) {
next_wakeup_.exclusive_store(-1);
const nanoseconds_t deadline = deadline_.wait_load();
if (deadline >= 0 && deadline <= timestamp(ClockMonotonic)) {
break;
}
if (deadline > 0) {
next_wakeup_.exclusive_store(deadline);
(void)sem_.timed_wait(deadline);
} else {
sem_.wait();
}
sem_post_flag_ = false;
}
next_wakeup_.exclusive_store(0);
}
} // namespace core
} // namespace roc