diff options
author | Paul Adenot <[email protected]> | 2022-12-13 19:30:46 +0100 |
---|---|---|
committer | Paul Adenot <[email protected]> | 2022-12-13 19:53:21 +0100 |
commit | 1353c3383431459b9b3ef469dc1ecb3ec4ec7d69 (patch) | |
tree | 95b65e257f356346d3d03c92c39140021170b8b2 /test | |
parent | e4da2d47c3499320142145f3dbc27204b2d51a59 (diff) | |
download | cubeb-1353c3383431459b9b3ef469dc1ecb3ec4ec7d69.tar.gz cubeb-1353c3383431459b9b3ef469dc1ecb3ec4ec7d69.zip |
Add a triple_buffer class that allows publishing data in a wait-free manner from a real-time thread to another thread.
It's largely inspired from the crate cubeb-coreaudio-rs uses, but ported to
C++.
Diffstat (limited to 'test')
-rw-r--r-- | test/test_triple_buffer.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/test/test_triple_buffer.cpp b/test/test_triple_buffer.cpp new file mode 100644 index 0000000..a6e0049 --- /dev/null +++ b/test/test_triple_buffer.cpp @@ -0,0 +1,67 @@ +/* + * Copyright © 2022 Mozilla Foundation + * + * This program is made available under an ISC-style license. See the + * accompanying file LICENSE for details. + */ + +/* cubeb_triple_buffer test */ +#include "gtest/gtest.h" +#if !defined(_XOPEN_SOURCE) +#define _XOPEN_SOURCE 600 +#endif +#include "cubeb/cubeb.h" +#include "cubeb_triple_buffer.h" +#include <atomic> +#include <math.h> +#include <memory> +#include <stdio.h> +#include <stdlib.h> +#include <thread> + +#include "common.h" + +TEST(cubeb, triple_buffer) +{ + struct AB { + uint64_t a; + uint64_t b; + }; + triple_buffer<AB> buffer; + + std::atomic<bool> finished = {false}; + + ASSERT_TRUE(!buffer.updated()); + + auto t = std::thread([&finished, &buffer] { + AB ab; + ab.a = 0; + ab.b = UINT64_MAX; + uint64_t counter = 0; + do { + buffer.write(ab); + ab.a++; + ab.b--; + } while (counter++ < 1e6 && ab.a <= UINT64_MAX && ab.b != 0); + finished.store(true); + }); + + AB ab; + AB old_ab; + old_ab.a = 0; + old_ab.b = UINT64_MAX; + + // Wait to have at least one value produced. + while (!buffer.updated()) { + } + + // Check that the values are increasing (resp. descreasing) monotonically. + while (!finished) { + ab = buffer.read(); + ASSERT_GE(ab.a, old_ab.a); + ASSERT_LE(ab.b, old_ab.b); + old_ab = ab; + } + + t.join(); +} |