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
|
#include <QtTest>
#include <QThread>
#include <algorithm>
#include <array>
#include <chrono>
#include <condition_variable>
#include <cstring>
#include <memory>
#include <mutex>
#include <vector>
#include "FastDecode.hpp"
#include "Detector/Detector.hpp"
#include "commons.h"
#include "wsjtx_config.h"
namespace
{
dec_data_t storage {};
constexpr int capacity = 360000;
constexpr int committed = 7168;
class Rendezvous
{
public:
bool arrive ()
{
std::unique_lock<std::mutex> lock {mutex_};
++arrivals_;
changed_.notify_all ();
return changed_.wait_for (lock, std::chrono::seconds {5},
[this] { return arrivals_ == 2; });
}
private:
std::mutex mutex_;
std::condition_variable changed_;
int arrivals_ = 0;
};
struct DecoderProbe
{
std::mutex mutex;
std::condition_variable changed;
bool entered = false;
bool released = false;
bool timedOut = false;
bool validCount = false;
bool offCallerThread = false;
QThread * callerThread = QThread::currentThread ();
std::vector<short> samples;
std::array<int, 15> arguments;
double period = 0;
QByteArray myCall, hisCall;
std::array<fortran_charlen_t, 3> lengths;
Rendezvous * sampleAccessStart = nullptr;
bool waitForEntry ()
{
std::unique_lock<std::mutex> lock {mutex};
return changed.wait_for (lock, std::chrono::seconds {5},
[this] { return entered; });
}
void release ()
{
std::lock_guard<std::mutex> lock {mutex};
released = true;
changed.notify_all ();
}
};
DecoderProbe * activeProbe = nullptr;
std::array<DecoderProbe *, 2> overlappingProbes {{nullptr, nullptr}};
struct Inputs
{
std::array<int, 15> arguments {};
double period = 15;
std::array<char, 8000> messages {};
std::array<char, 12> myCall, hisCall;
Inputs ()
{
arguments[0] = 123000;
arguments[1] = committed;
arguments[3] = 1;
arguments[8] = 2;
arguments[9] = 104;
arguments[10] = 1500;
arguments[11] = 100;
std::memcpy (myCall.data (), "K1ABC ", 12);
std::memcpy (hisCall.data (), "W9XYZ ", 12);
}
QFuture<FastDecodeResult> submit (short const * samples = storage.d2)
{
return startFastDecode (samples, arguments.data (), period,
myCall.data (), hisCall.data ());
}
};
}
dec_data_t& dec_data = storage;
// Substitute only the decoder at its ABI boundary. The production helper
// still copies the source and launches the real QtConcurrent job. Delaying
// input consumption models an ordinarily scheduled-but-not-yet-running job.
extern "C" void fast_decode_ (short samples[], int arguments[], double * period,
char messages[], char myCall[], char hisCall[],
fortran_charlen_t messageLength,
fortran_charlen_t myCallLength,
fortran_charlen_t hisCallLength)
{
auto * selected = activeProbe;
if (!selected && arguments[0] == 111000) selected = overlappingProbes[0];
if (!selected && arguments[0] == 222000) selected = overlappingProbes[1];
auto& probe = *selected;
{
std::unique_lock<std::mutex> lock {probe.mutex};
probe.entered = true;
probe.changed.notify_all ();
probe.timedOut = !probe.changed.wait_for (lock, std::chrono::seconds {5},
[&] { return probe.released; });
}
if (probe.timedOut) return; // A harness timeout must not introduce an unordered read.
if (probe.sampleAccessStart && !probe.sampleAccessStart->arrive ())
{
probe.timedOut = true;
return;
}
probe.offCallerThread = QThread::currentThread () != probe.callerThread;
probe.validCount = arguments[1] >= 0 && arguments[1] <= capacity;
if (probe.validCount) probe.samples.assign (samples, samples + arguments[1]);
std::copy_n (arguments, 15, probe.arguments.begin ());
probe.period = *period;
probe.lengths = {{messageLength, myCallLength, hisCallLength}};
probe.myCall = QByteArray (myCall, 12);
probe.hisCall = QByteArray (hisCall, 12);
// Preserve the existing ABI's output behavior, including in/out arguments.
std::memcpy (messages, "PROBE", 6);
arguments[12] = 31;
arguments[13] = 248;
}
class TestFastDecodeOwnership : public QObject
{
Q_OBJECT
private Q_SLOTS:
void captures_submission_inputs ()
{
std::fill_n (storage.d2, committed, short (1234));
Inputs input;
auto const expectedArguments = input.arguments;
DecoderProbe probe;
activeProbe = &probe;
auto future = input.submit ();
bool const entered = probe.waitForEntry ();
// All caller storage remains alive. The release orders these mutations
// before the probe reads, so an assertion failure is an ownership defect,
// not undefined behavior deliberately introduced by the test.
std::fill_n (storage.d2, committed, short (5678));
std::memcpy (input.myCall.data (), "N0NEW ", 12);
std::memcpy (input.hisCall.data (), "G4NEW ", 12);
input.period = 30;
input.arguments[10] = 2500;
probe.release ();
future.waitForFinished ();
activeProbe = nullptr;
QVERIFY (entered);
QVERIFY (!probe.timedOut);
QVERIFY (probe.offCallerThread);
QVERIFY (probe.validCount);
QCOMPARE (probe.lengths[0], fortran_charlen_t (8000));
QCOMPARE (probe.lengths[1], fortran_charlen_t (12));
QCOMPARE (probe.lengths[2], fortran_charlen_t (12));
auto const result = future.result ();
QCOMPARE (QByteArray (result.messages.data ()), QByteArray ("PROBE"));
QCOMPARE (result.arguments[12], 31);
QCOMPARE (result.arguments[13], 248);
QCOMPARE (input.arguments[12], 0); // worker never writes caller storage
QVERIFY (probe.arguments == expectedArguments);
QCOMPARE (probe.samples.size (), std::size_t (committed));
QVERIFY (std::all_of (probe.samples.begin (), probe.samples.end (),
[] (short x) { return x == 1234; }));
QCOMPARE (probe.myCall, QByteArray ("K1ABC ", 12));
QCOMPARE (probe.hisCall, QByteArray ("W9XYZ ", 12));
QCOMPARE (probe.period, 15.0);
}
void capture_does_not_race_live_append ()
{
Detector detector {12000, 15.0, 1};
QVERIFY (detector.initialize (QIODevice::WriteOnly, AudioDevice::Mono));
AudioStreamDescriptor descriptor;
descriptor.sample_rate_hz = 12000;
descriptor.sample_encoding = AudioStreamDescriptor::SampleEncoding::SignedInteger;
descriptor.sample_size_bits = 16;
descriptor.byte_order = AudioStreamDescriptor::ByteOrder::LittleEndian;
descriptor.channel_count = 1;
descriptor.channel_layout = AudioStreamDescriptor::ChannelLayout::Mono;
descriptor.clock_domain = AudioStreamDescriptor::ClockDomain::SystemClock;
descriptor.timing_evidence = AudioStreamDescriptor::TimingEvidence::CaptureTimeAnchored;
descriptor.capture_anchor_utc_ms = 90000;
detector.setStreamDescriptor (descriptor);
ReceiveAudioConsumer consumer;
auto connection = connect (&detector, &Detector::audioBlock, this,
[&] (ReceiveAudio audio) { QVERIFY (consumer.accept (audio, storage)); });
std::vector<short> initial (committed, 1234);
QCOMPARE (detector.write (reinterpret_cast<char const *> (initial.data ()),
initial.size () * sizeof (short)), qint64 (2 * committed));
disconnect (connection);
std::mutex mutex;
std::condition_variable changed;
bool ready = false, released = false, writerStarted = false;
qint64 written = -1;
auto * callerThread = QThread::currentThread ();
std::unique_ptr<QThread> writer {QThread::create ([&] {
{
std::unique_lock<std::mutex> lock {mutex};
ready = true;
changed.notify_all ();
writerStarted = changed.wait_for (lock, std::chrono::seconds {5},
[&] { return released; });
}
if (writerStarted)
{
std::vector<short> next (3584, 5678);
written = detector.write (reinterpret_cast<char const *> (next.data ()),
next.size () * sizeof (short));
}
detector.moveToThread (callerThread);
})};
detector.moveToThread (writer.get ());
writer->start ();
bool writerReady;
{
std::unique_lock<std::mutex> lock {mutex};
writerReady = changed.wait_for (lock, std::chrono::seconds {5}, [&] { return ready; });
released = true;
changed.notify_all ();
}
// Both the production capture and the append execute after releasing the
// gate. No completion handshake orders one sample access before the other.
Inputs input;
DecoderProbe probe;
probe.released = true;
activeProbe = &probe;
auto future = input.submit ();
future.waitForFinished ();
writer->wait ();
activeProbe = nullptr;
QVERIFY (writerReady);
QVERIFY (writerStarted);
QCOMPARE (written, qint64 (2 * 3584));
QCOMPARE (dec_data.params.kin, committed);
QVERIFY (probe.validCount);
QCOMPARE (probe.samples.size (), std::size_t (committed));
QVERIFY (std::all_of (probe.samples.begin (), probe.samples.end (),
[] (short x) { return x == 1234; }));
// Producer appends cannot mutate the decoder's accepted snapshot.
}
void overlapping_jobs_retain_submission_identity ()
{
std::fill_n (storage.d2, committed, short (1234));
std::vector<short> secondSamples (committed, short (5678));
Inputs first, second;
first.arguments[0] = 111000;
second.arguments[0] = 222000;
DecoderProbe firstProbe, secondProbe;
secondProbe.released = true;
overlappingProbes = {{&firstProbe, &secondProbe}};
auto firstFuture = first.submit ();
bool const firstEntered = firstProbe.waitForEntry ();
// Submission performs the production staging copy synchronously. The
// first decoder is already pending but has not consumed any samples.
auto secondFuture = second.submit (secondSamples.data ());
firstProbe.release ();
firstFuture.waitForFinished ();
secondFuture.waitForFinished ();
overlappingProbes = {{nullptr, nullptr}};
QVERIFY (firstEntered);
QVERIFY (!firstProbe.timedOut);
QVERIFY (!secondProbe.timedOut);
QVERIFY (firstProbe.entered);
QVERIFY (secondProbe.entered);
QCOMPARE (firstProbe.samples.size (), std::size_t (committed));
QCOMPARE (secondProbe.samples.size (), std::size_t (committed));
QVERIFY (std::all_of (firstProbe.samples.begin (), firstProbe.samples.end (),
[] (short x) { return x == 1234; }));
QVERIFY (std::all_of (secondProbe.samples.begin (), secondProbe.samples.end (),
[] (short x) { return x == 5678; }));
}
void overlapping_submission_does_not_race_decoder ()
{
std::fill_n (storage.d2, committed, short (1234));
std::vector<short> secondSamples (committed, short (5678));
Inputs first, second;
first.arguments[0] = 111000;
second.arguments[0] = 222000;
DecoderProbe firstProbe, secondProbe;
firstProbe.released = true;
secondProbe.released = true;
Rendezvous sampleAccessStart;
firstProbe.sampleAccessStart = &sampleAccessStart;
overlappingProbes = {{&firstProbe, &secondProbe}};
auto firstFuture = first.submit ();
QFuture<FastDecodeResult> secondFuture;
std::unique_ptr<QThread> submitter {QThread::create ([&] {
if (sampleAccessStart.arrive ())
secondFuture = second.submit (secondSamples.data ());
})};
submitter->start ();
submitter->wait ();
firstFuture.waitForFinished ();
secondFuture.waitForFinished ();
overlappingProbes = {{nullptr, nullptr}};
QVERIFY (!firstProbe.timedOut);
QVERIFY (!secondProbe.timedOut);
QVERIFY (firstProbe.entered);
QVERIFY (secondProbe.entered);
// Under TSan, the production staging memcpy conflicts with the first
// decoder's read. No data-value assertion relies on that C++ race.
}
};
QTEST_GUILESS_MAIN (TestFastDecodeOwnership)
#include "test_fast_decode_ownership.moc"
|