-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfifo.h
212 lines (142 loc) · 3.5 KB
/
fifo.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// (C) 2022 by folkert van heusden <[email protected]>, released under Apache License v2.0
#pragma once
#include <optional>
#include <pthread.h>
#include <stdint.h>
#include "fifo_stats.h"
#include "stats.h"
template <typename T>
class fifo
{
private:
T *data { nullptr };
const int n_elements { 0 };
int read_pointer { 0 };
int write_pointer { 0 };
bool full { false };
bool interrupted { false };
pthread_mutex_t lock;
/* cond_push is signalled when a new item is pushed
* cond_pull is signalled when an item is removed
*/
pthread_cond_t cond_push;
pthread_cond_t cond_pull;
fifo_stats *fs { nullptr };
uint64_t n_in { 0 };
uint64_t *fifo_n_in_stats { nullptr };
uint64_t *fifo_full_count { nullptr };
public:
fifo(stats *const s, const std::string & name, const int n_elements) : n_elements(n_elements)
{
data = new T[n_elements];
fifo_n_in_stats = s->register_stat(name + "_n_in");
fifo_full_count = s->register_stat(name + "_full");
fs = new fifo_stats(n_elements);
s->register_fifo_stats(name, fs);
pthread_mutex_init(&lock, NULL);
pthread_cond_init(&cond_push, NULL);
pthread_cond_init(&cond_pull, NULL);
}
~fifo()
{
delete fs;
delete [] data;
}
void interrupt()
{
pthread_mutex_lock(&lock);
interrupted = true;
pthread_cond_broadcast(&cond_push);
pthread_mutex_unlock(&lock);
}
void put(const T & element)
{
pthread_mutex_lock(&lock);
if (full)
stats_inc_counter(fifo_full_count);
while(full)
pthread_cond_wait(&cond_pull, &lock);
data[write_pointer] = element;
write_pointer++;
write_pointer %= n_elements;
n_in++;
fs->count(n_in);
stats_set(fifo_n_in_stats, n_in);
full = write_pointer == read_pointer;
pthread_cond_signal(&cond_push);
pthread_mutex_unlock(&lock);
}
bool try_put(const T & element)
{
bool have_put = false;
pthread_mutex_lock(&lock);
if (full)
stats_inc_counter(fifo_full_count);
else {
data[write_pointer] = element;
write_pointer++;
write_pointer %= n_elements;
n_in++;
fs->count(n_in);
stats_set(fifo_n_in_stats, n_in);
full = write_pointer == read_pointer;
have_put = true;
pthread_cond_signal(&cond_push);
}
pthread_mutex_unlock(&lock);
return have_put;
}
std::optional<T> get(const int ms)
{
struct timespec ts { 0, 0 };
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_nsec += (ms % 1000) * 1000000ll;
ts.tv_sec += ms / 1000;
if (ts.tv_nsec >= 1000000000ll) {
ts.tv_nsec -= 1000000000ll;
ts.tv_sec++;
}
pthread_mutex_lock(&lock);
bool ok = true;
while(read_pointer == write_pointer && !full && !interrupted) {
if (pthread_cond_timedwait(&cond_push, &lock, &ts)) {
ok = false;
break;
}
}
if (interrupted) {
pthread_mutex_unlock(&lock);
return { };
}
if (ok) {
T copy = data[read_pointer];
read_pointer++;
read_pointer %= n_elements;
n_in--;
full = 0;
pthread_cond_signal(&cond_pull);
pthread_mutex_unlock(&lock);
return copy;
}
pthread_mutex_unlock(&lock);
return { };
}
std::optional<T> get()
{
pthread_mutex_lock(&lock);
while(read_pointer == write_pointer && !full && !interrupted)
pthread_cond_wait(&cond_push, &lock);
if (interrupted) {
pthread_mutex_unlock(&lock);
return { };
}
T copy = data[read_pointer];
read_pointer++;
read_pointer %= n_elements;
n_in--;
full = 0;
pthread_cond_signal(&cond_pull);
pthread_mutex_unlock(&lock);
return copy;
}
};