Repository navigation
Expand file tree
/
Copy pathqueue.cpp
More file actions
369 lines (341 loc) · 12.2 KB
/
Copy pathqueue.cpp
File metadata and controls
369 lines (341 loc) · 12.2 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// File/layer: core/src/queue.cpp | C++17, Qt-free
// Testing: UNIT: QueueTest (test_core); c_abi, c_abi_shared
//
// ecuio/queue.h (ADR-0053 section 4, its appendix "the pull queue as
// built"): a ring of fixed slots made at open, so the worker's copy never
// allocates. The worker copies under the queue's mutex and notifies only
// when a reader waits; a reader copies out in chunks, the mutex let go
// between them, so a large read never holds the worker up for long. A
// full ring drops its oldest entry.
#include "ecuio/queue.h"
#include "ecuio/internal.h" /* ecuio_queue_test_waiting */
#include "pull_queue.h"
#include "session.h"
#include <algorithm>
#include <chrono>
#include <condition_variable>
#include <cstring>
#include <memory>
#include <new>
#include <thread>
namespace {
/* Entries a reader copies out per hold of the mutex: about 130 KB of
* frames, 40 KB of values */
constexpr uint32_t kFrameChunk = 32;
constexpr uint32_t kValueChunk = 256;
} // namespace
struct ecuio_queue {
uint32_t kind = 0;
uint32_t capacity = 0;
std::shared_ptr<ecuio_queue_link> link;
std::unique_ptr<ecuio_frame_t[]> frames;
std::unique_ptr<ecuio_queue_value_t[]> values;
std::unique_ptr<ecuio_condition_event_t[]> events;
std::mutex m;
std::condition_variable cv;
uint32_t head = 0; /* the oldest entry's slot */
uint32_t size = 0; /* entries waiting */
uint64_t dropped = 0;
uint32_t waiting = 0; /* readers waiting for an entry */
uint32_t readers = 0; /* reads under way */
bool ended = false; /* the session has closed */
bool stream_ended = false; /* a replay has played its last frame */
bool closing = false; /* ecuio_queue_close has begun */
/* The slot the next entry goes in, the oldest dropped when full */
uint32_t take_slot()
{
if (size == capacity) {
head = (head + 1) % capacity;
--size;
++dropped;
}
return (head + size++) % capacity;
}
void wake_readers(std::unique_lock<std::mutex>& lock)
{
const bool wake = waiting > 0;
lock.unlock();
if (wake) {
cv.notify_all();
}
}
};
namespace {
void copy_frame(ecuio_frame_t& to, const ecuio_frame_t& from)
{
const uint32_t length =
(std::min)(uint32_t{from.data_length}, ECUIO_FRAME_DATA_MAX);
to.timestamp_us = from.timestamp_us;
to.arbitration_id = from.arbitration_id;
to.channel_id = from.channel_id;
to.data_length = static_cast<uint16_t>(length);
std::memcpy(to.data, from.data, length);
to.flags = from.flags;
to.reserved = from.reserved;
}
void copy_value(ecuio_queue_value_t& to, const ecuio_value_batch_t& batch,
const ecuio_value_t& v)
{
to.timestamp_us = batch.timestamp_us;
to.arbitration_id = batch.arbitration_id;
to.signal_id = v.signal_id;
to.kind = v.kind;
to.flags = 0;
to.value = v.value;
to.reserved = 0;
uint32_t length = 0;
if (v.text != nullptr) {
length = v.text_len;
if (length > ECUIO_QUEUE_TEXT_MAX - 1) {
length = ECUIO_QUEUE_TEXT_MAX - 1;
to.flags |= ECUIO_QUEUE_VALUE_TEXT_CUT;
}
std::memcpy(to.text, v.text, length);
}
to.text_len = length;
/* The NUL and past it zero, so no older entry's text shows */
std::memset(to.text + length, 0, ECUIO_QUEUE_TEXT_MAX - length);
}
/* One read, either kind: waits for the first entry, then copies out in
* chunks through `copy(slot, out index)` */
template <typename Copy>
ecuio_status_t read_queue(ecuio_queue_t* q, uint32_t capacity, uint32_t chunk,
uint32_t* count, uint64_t* dropped,
uint32_t timeout_ms, Copy copy)
{
*count = 0;
std::unique_lock<std::mutex> lock(q->m);
++q->readers;
if (q->size == 0 && timeout_ms > 0 && !q->ended && !q->closing &&
!q->stream_ended) {
const auto until = std::chrono::steady_clock::now() +
std::chrono::milliseconds(timeout_ms);
++q->waiting;
q->cv.wait_until(lock, until, [q] {
return q->size > 0 || q->ended || q->closing || q->stream_ended;
});
--q->waiting;
}
uint32_t taken = 0;
while (taken < capacity && q->size > 0 && !q->closing) {
const uint32_t n = (std::min)({chunk, capacity - taken, q->size});
for (uint32_t i = 0; i < n; ++i) {
copy(q->head, taken + i);
q->head = (q->head + 1) % q->capacity;
--q->size;
}
taken += n;
if (taken < capacity && q->size > 0) {
/* Let the worker in between chunks */
lock.unlock();
lock.lock();
}
}
*count = taken;
*dropped = q->dropped;
const ecuio_status_t rc = taken > 0 ? ECUIO_OK
: q->stream_ended ? ECUIO_ERR_STREAM_ENDED
: q->ended || q->closing
? ECUIO_ERR_SESSION_CLOSED
: ECUIO_OK;
--q->readers;
/* The last read out of a closing queue wakes the close while holding the
* mutex, so the close can't free the queue before the release below */
if (q->closing && q->readers == 0) {
q->cv.notify_all();
}
return rc;
}
} // namespace
void ecuio_queue_put_frame(ecuio_queue_t* q, const ecuio_frame_t* frame)
{
std::unique_lock<std::mutex> lock(q->m);
copy_frame(q->frames[q->take_slot()], *frame);
q->wake_readers(lock);
}
void ecuio_queue_put_values(ecuio_queue_t* q, const ecuio_value_batch_t* batch)
{
std::unique_lock<std::mutex> lock(q->m);
for (uint32_t i = 0; i < batch->count; ++i) {
copy_value(q->values[q->take_slot()], *batch, batch->values[i]);
}
q->wake_readers(lock);
}
void ecuio_queue_detach(ecuio_queue_link& link)
{
std::lock_guard<std::mutex> g(link.m);
ecuio_queue_t* q = link.active.exchange(nullptr, std::memory_order_seq_cst);
while (link.pins.load(std::memory_order_acquire) != 0) {
std::this_thread::yield();
}
if (q == nullptr) {
return;
}
{
std::lock_guard<std::mutex> lock(q->m);
q->ended = true;
}
q->cv.notify_all();
}
void ecuio_queue_end_all(ecuio_session_t* session)
{
for (uint32_t i = 0; i < session->queue_count; ++i) {
ecuio_queue_link& link = *session->queues[i];
ecuio_queue_t* q = ecuio_queue_pin(link);
if (q == nullptr) {
continue;
}
{
std::lock_guard<std::mutex> lock(q->m);
q->stream_ended = true;
}
q->cv.notify_all(); /* pinned: the queue can't be freed yet */
ecuio_queue_unpin(link);
}
}
void ecuio_queue_put_conditions(ecuio_queue_t* q,
const ecuio_condition_event_t* events,
uint32_t count)
{
std::unique_lock<std::mutex> lock(q->m);
for (uint32_t i = 0; i < count; ++i) {
q->events[q->take_slot()] = events[i];
}
q->wake_readers(lock);
}
extern "C" {
ecuio_status_t ecuio_queue_open(ecuio_session_t* session, uint32_t kind,
uint32_t capacity, ecuio_queue_t** out)
{
if (out != nullptr) {
*out = nullptr;
}
if (session == nullptr || out == nullptr) {
return ECUIO_ERR_NULL_ARGUMENT;
}
if (session->closed.load()) {
return ECUIO_ERR_SESSION_CLOSED;
}
const uint32_t ceiling =
kind == ECUIO_QUEUE_FRAMES ? ECUIO_QUEUE_MAX_FRAMES
: kind == ECUIO_QUEUE_VALUES ? ECUIO_QUEUE_MAX_VALUES
: kind == ECUIO_QUEUE_CONDITIONS ? ECUIO_QUEUE_MAX_CONDITIONS
: 0;
if (ceiling == 0 || capacity == 0 || capacity > ceiling ||
session->started ||
session->queue_count >= ECUIO_QUEUE_MAX_PER_SESSION) {
return ECUIO_ERR_INVALID_ARGUMENT;
}
std::unique_ptr<ecuio_queue_t> q(new (std::nothrow) ecuio_queue_t);
if (q == nullptr) {
return ECUIO_ERR_OUT_OF_MEMORY;
}
q->kind = kind;
q->capacity = capacity;
if (kind == ECUIO_QUEUE_FRAMES) {
q->frames.reset(new (std::nothrow) ecuio_frame_t[capacity]);
} else if (kind == ECUIO_QUEUE_VALUES) {
q->values.reset(new (std::nothrow) ecuio_queue_value_t[capacity]);
} else {
q->events.reset(new (std::nothrow) ecuio_condition_event_t[capacity]);
}
if (q->frames == nullptr && q->values == nullptr && q->events == nullptr) {
return ECUIO_ERR_OUT_OF_MEMORY;
}
try {
q->link = std::make_shared<ecuio_queue_link>();
} catch (const std::bad_alloc&) {
return ECUIO_ERR_OUT_OF_MEMORY;
}
q->link->kind = kind;
q->link->active.store(q.get());
session->queues[session->queue_count++] = q->link;
if (kind == ECUIO_QUEUE_VALUES) {
++session->value_queues;
}
*out = q.release();
return ECUIO_OK;
}
ecuio_status_t ecuio_queue_read_frames(ecuio_queue_t* queue, ecuio_frame_t* out,
uint32_t capacity, uint32_t* count,
uint64_t* dropped, uint32_t timeout_ms)
{
if (queue == nullptr || out == nullptr || count == nullptr ||
dropped == nullptr) {
return ECUIO_ERR_NULL_ARGUMENT;
}
if (capacity == 0 || queue->kind != ECUIO_QUEUE_FRAMES) {
return ECUIO_ERR_INVALID_ARGUMENT;
}
return read_queue(queue, capacity, kFrameChunk, count, dropped, timeout_ms,
[queue, out](uint32_t slot, uint32_t at) {
const ecuio_frame_t& from = queue->frames[slot];
copy_frame(out[at], from);
std::memset(out[at].data + from.data_length, 0,
ECUIO_FRAME_DATA_MAX - from.data_length);
});
}
ecuio_status_t ecuio_queue_read_values(ecuio_queue_t* queue,
ecuio_queue_value_t* out,
uint32_t capacity, uint32_t* count,
uint64_t* dropped, uint32_t timeout_ms)
{
if (queue == nullptr || out == nullptr || count == nullptr ||
dropped == nullptr) {
return ECUIO_ERR_NULL_ARGUMENT;
}
if (capacity == 0 || queue->kind != ECUIO_QUEUE_VALUES) {
return ECUIO_ERR_INVALID_ARGUMENT;
}
return read_queue(queue, capacity, kValueChunk, count, dropped, timeout_ms,
[queue, out](uint32_t slot, uint32_t at) {
out[at] = queue->values[slot];
});
}
ecuio_status_t ecuio_queue_read_conditions(ecuio_queue_t* queue,
ecuio_condition_event_t* out,
uint32_t capacity, uint32_t* count,
uint64_t* dropped,
uint32_t timeout_ms)
{
if (queue == nullptr || out == nullptr || count == nullptr ||
dropped == nullptr) {
return ECUIO_ERR_NULL_ARGUMENT;
}
if (capacity == 0 || queue->kind != ECUIO_QUEUE_CONDITIONS) {
return ECUIO_ERR_INVALID_ARGUMENT;
}
return read_queue(queue, capacity, kValueChunk, count, dropped, timeout_ms,
[queue, out](uint32_t slot, uint32_t at) {
out[at] = queue->events[slot];
});
}
uint32_t ecuio_queue_test_waiting(ecuio_queue_t* queue)
{
std::lock_guard<std::mutex> lock(queue->m);
return queue->waiting;
}
void ecuio_queue_close(ecuio_queue_t* queue)
{
if (queue == nullptr) {
return;
}
/* Off the session first: once the pins are gone the worker can't
* reach it */
{
std::lock_guard<std::mutex> g(queue->link->m);
queue->link->active.exchange(nullptr, std::memory_order_seq_cst);
while (queue->link->pins.load(std::memory_order_acquire) != 0) {
std::this_thread::yield();
}
}
/* Then the reads under way: each returns, the last one wakes us */
{
std::unique_lock<std::mutex> lock(queue->m);
queue->closing = true;
queue->cv.notify_all();
queue->cv.wait(lock, [queue] { return queue->readers == 0; });
}
delete queue;
}
} // extern "C"