-
Notifications
You must be signed in to change notification settings - Fork 355
/
ts_timestamp.h
182 lines (147 loc) · 4.39 KB
/
ts_timestamp.h
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#ifndef CDSLIB_CONTAINER_TS_TIMESTAMP_H
#define CDSLIB_CONTAINER_TS_TIMESTAMP_H
#include <atomic>
#include <cds/compiler/ts_hardwaretimestamp.h>
namespace cds { namespace container {
using namespace cds::tshardwaretimestamp;
class HardwareTimestamp
{
public:
inline void initialize(uint64_t delay, uint64_t num_threads)
{
}
inline void init_sentinel(uint64_t *result)
{
result[0] = 0;
}
inline void init_sentinel_atomic(std::atomic<uint64_t> *result)
{
result[0].store(0);
}
inline void init_top_atomic(std::atomic<uint64_t> *result)
{
result[0].store(UINT64_MAX);
}
inline void init_top(uint64_t *result)
{
result[0] = UINT64_MAX;
}
inline void load_timestamp(uint64_t *result, std::atomic<uint64_t> *source)
{
result[0] = source[0].load();
}
inline void set_timestamp(std::atomic<uint64_t> *result)
{
result[0].store(platform::get_hwptime());
}
inline void read_time(uint64_t *result)
{
result[0] = platform::get_hwptime();
}
inline bool is_later(uint64_t *timestamp1, uint64_t *timestamp2)
{
return timestamp2[0] < timestamp1[0];
}
};
class HardwareIntervalTimestamp
{
private:
uint64_t delay_;
public:
inline void initialize(uint64_t delay, uint64_t num_threads)
{
delay_ = delay;
}
inline void init_sentinel(uint64_t *result)
{
result[0] = 0;
result[1] = 0;
}
inline void init_sentinel_atomic(std::atomic<uint64_t> *result)
{
result[0].store(0);
result[1].store(0);
}
inline void init_top_atomic(std::atomic<uint64_t> *result)
{
result[0].store(UINT64_MAX);
result[1].store(UINT64_MAX);
}
inline void init_top(uint64_t *result)
{
result[0] = UINT64_MAX;
result[1] = UINT64_MAX;
}
inline void load_timestamp(uint64_t *result, std::atomic<uint64_t> *source)
{
result[0] = source[0].load();
result[1] = source[1].load();
}
inline void set_timestamp(std::atomic<uint64_t> *result)
{
result[0].store(platform::get_hwptime());
uint64_t wait = platform::get_hwtime() + delay_;
while (platform::get_hwtime() < wait)
{
}
result[1].store(platform::get_hwptime());
}
inline void read_time(uint64_t *result)
{
result[0] = platform::get_hwptime();
result[1] = result[0];
}
inline bool is_later(uint64_t *timestamp1, uint64_t *timestamp2)
{
return timestamp2[1] < timestamp1[0];
}
};
class AtomicCounterTimestamp
{
private:
std::atomic<uint64_t> *clock_;
public:
inline void initialize(uint64_t delay, uint64_t num_threads)
{
clock_ = new std::atomic<uint64_t>();
clock_->store(1);
}
inline void init_sentinel(uint64_t *result)
{
result[0] = 0;
}
inline void init_sentinel_atomic(std::atomic<uint64_t> *result)
{
result[0].store(0);
}
inline void init_top_atomic(std::atomic<uint64_t> *result)
{
result[0].store(UINT64_MAX);
}
inline void init_top(uint64_t *result)
{
result[0] = UINT64_MAX;
}
inline void load_timestamp(uint64_t *result, std::atomic<uint64_t> *source)
{
result[0] = source[0].load();
}
inline void set_timestamp(std::atomic<uint64_t> *result)
{
result[0].store(clock_->fetch_add(1));
}
inline void set_timestamp_local(uint64_t *result)
{
result[0] = clock_->fetch_add(1);
}
inline void read_time(uint64_t *result)
{
result[0] = clock_->load();
}
inline bool is_later(uint64_t *timestamp1, uint64_t *timestamp2)
{
return timestamp2[0] < timestamp1[0];
}
};
}} // namespace cds::container
#endif // #ifndef CDSLIB_CONTAINER_TS_TIMESTAMP_H