-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcondition.h
More file actions
64 lines (49 loc) · 1.38 KB
/
Copy pathcondition.h
File metadata and controls
64 lines (49 loc) · 1.38 KB
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
/*
* Condition.h
* Copyright (C) 2021 youfa.song <vsyfar@gmail.com>
*
* Distributed under terms of the GPLv2 license.
*/
#ifndef AVE_CONDITION_H
#define AVE_CONDITION_H
#include <climits>
#include <cstdint>
#include <ctime>
#include "mutex.h"
#include "types.h"
namespace ave {
namespace base {
class Condition {
public:
Condition() { pthread_cond_init(&cond_, nullptr); }
virtual ~Condition() { pthread_cond_destroy(&cond_); }
status_t Wait(Mutex& mutex) {
return -pthread_cond_wait(&cond_, &mutex.mutex_);
}
status_t WaitRelative(Mutex& mutex, nsecs_t reltime) {
struct timespec ts{};
clock_gettime(CLOCK_MONOTONIC, &ts);
int64_t reltime_sec = reltime / 1000000000;
ts.tv_nsec += static_cast<long>(reltime % 1000000000);
if (reltime_sec < INT64_MAX && ts.tv_nsec >= 1000000000) {
ts.tv_nsec -= 1000000000;
++reltime_sec;
}
int64_t time_sec = ts.tv_sec;
if (time_sec > INT64_MAX - reltime_sec) {
time_sec = INT64_MAX;
} else {
time_sec += reltime_sec;
}
ts.tv_sec = (time_sec > LONG_MAX) ? LONG_MAX : static_cast<long>(time_sec);
return -pthread_cond_timedwait(&cond_, &mutex.mutex_, &ts);
}
void Signal() { pthread_cond_signal(&cond_); }
void Broadcast() { pthread_cond_broadcast(&cond_); }
private:
pthread_cond_t cond_{};
};
}
} // namespace base
} // namespace ave
#endif /* !AVE_CONDITION_H */