8000 Merge develop to main by cliffg-softwarelibre · Pull Request #17 · connectivecpp/wait-queue · GitHub
[go: up one dir, main page]

Skip to content

Merge develop to main #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading 8000
Diff view
Diff view
93 changes: 47 additions & 46 deletions include/queue/wait_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
*
* In particular, @c wait_queue:
*
* - Has been tested with Martin Moene's @c ring_span library for the internal container.
* A @c ring_span is traditionally known as a "ring buffer" or "circular buffer". This
* implies that the @c wait_queue can be used in environments where dynamic memory
* management (heap) is not allowed or is problematic. In particular, no heap memory is
* directly allocated within the @c wait_queue object.
* - Has been tested with Martin Moene's @c ring_span library for the internal container,
* as well as Justin Masiulis' @c circular_buffer library. A "ring buffer" or
* "circular buffer" uses a fixed size container and implies that the @c wait_queue can
* be used in environments where dynamic memory management (heap) is not allowed or is
* problematic. In particular, no heap memory will be directly allocated within the
* @c wait_queue object. A @c ring_span is a view on a container object instead of
* directly owning the container, so there are differences in construction and
* container management.
*
* - Does not throw or catch exceptions anywhere in its code base. If a value being pushed
* on to the queue throws an exception, it can be caught by the pushing code (or higher
Expand Down Expand Up @@ -102,13 +105,13 @@
* @code
* const int sz = 20;
* int buf[sz];
* chops::wait_queue<int, nonstd::ring_span<int> > wq(buf+0, buf+sz);
* chops::wait_queue<int, nonstd::ring_span<int> >
* wq { nonstd::ring_span<int> { buf+0, buf+sz } };
* // push and pop same as code with default container
* @endcode
*
* The container type must support the following (depending on which
* methods are called): default construction, construction from a
* begin and end iterator, construction with an initial size,
* methods are called): default construction, construction with an initial size,
* @c push_back (preferably overloaded for both copy and move),
* @c emplace_back (with a template parameter pack), @c front, @c pop_front,
* @c empty, and @c size. The container must also have a @c size_type
Expand All @@ -134,8 +137,9 @@
* @c circular_buffer then the default constructor for @c wait_queue cannot be used
* (since it would result in a container with an empty capacity).
*
* Thanks go to Lou Langholtz for adding DBC (Design by Contract) assertions.
*
* @authors Cliff Green, Anthony Williams
* @authors Cliff Green, Lou Langholtz, Anthony Williams
*
* @copyright (c) 2017-2024 by Cliff Green
*
Expand Down Expand Up @@ -186,9 +190,9 @@ class wait_queue {
* anything, so a different @c wait_queue constructor must be used if
* instantiated with a @c boost @c circular_buffer.
*
* @post @c empty returns true.
* @post @c empty returns @c true.
* @post @c size returns 0.
* @post @c stop_requested return false.
* @post @c stop_requested returns @c false.
*/
wait_queue()
// noexcept(std::is_nothrow_constructible<Container>::value)
Expand All @@ -204,7 +208,7 @@ class wait_queue {
* @param stop_tok A @c std::stop_token which can be used to shutdown @c wait_queue
* processing.
*
* @post @c empty returns true.
* @post @c empty returns @c true.
* @post @c size returns 0.
*/
wait_queue(std::stop_token stop_tok)
Expand All @@ -215,55 +219,52 @@ class wait_queue {
}

/**
* @brief Construct a @c wait_queue with an iterator range for the container.
* @brief Construct a @c wait_queue by moving in an already constructed
* container.
*
* Construct the container (or container view) with an iterator range. Whether
* element copies are performed depends on the container type. Most container
* types copy initial elements as defined by the range and the initial size is
* set accordingly. A @c ring_span, however, uses the range distance to define
* a capacity and sets the initial size to zero.
* This constructor allows a container view to be used for the @c wait_queue
* container. Typically a container view is initialized with an underlying
* object, for example a statically allocated array. This allows @c wait_queue
* to be used where dynamic memory is not allowed.
*
* This constructor also allows arbitrary initialization of the data inside
* the container before it is managed by the @c wait_queue.
*
* An internal @c std::stop_source is used to provide a @c std::stop_token for
* coordinating shutdown.
*
* @note This is the only constructor that can be used with a @c ring_span
* container type.
*
* @param beg Beginning iterator.
*
* @param end Ending iterator.
* @param container Container object to be moved from (or copied from if not
* movable).
*
* @post @c empty returns true if @c beg equals @c end otherwise returns false.
* @post @c empty returns @c true if @c beg equals @c end otherwise returns @c false.
* @post @c size returns the distance between @c beg and @c end parameters.
*/
template <typename Iter>
wait_queue(Iter beg, Iter end)
// noexcept(std::is_nothrow_constructible<Container, Iter, Iter>::value)
wait_queue(Container&& container)
// noexcept(std::is_ something movable or maybe copyable )
: m_stop_src(std::stop_source{}), m_stop_tok((*m_stop_src).get_token()),
m_data_queue(beg, end) {
assert(empty() == (beg == end));
assert((size() == size_type(0)) == (beg == end)); // std::distance constrains beg, end.
m_data_queue(std::move(container)) {
// assert(empty() == (beg == end));
// assert((size() == size_type(0)) == (beg == end)); // std::distance constrains beg, end.
}

/**
* @brief Construct a @c wait_queue with an iterator range and a @c std::stop_token.
* This constructor allows a container view to be used for the @c wait_queue
* container. It also takes a @c std::stop_token for external shutdown.
*
* @param stop_tok A @c std::stop_token which can be used to shutdown @c wait_queue
* processing.
*
* @param beg Beginning iterator.
*
* @param end Ending iterator.
* @param container Container object to be moved from (or copied from if not
* movable).
*
* @post @c empty returns true if @c beg equals @c end otherwise returns false.
* @post @c empty returns @c true if @c beg equals @c end otherwise returns @c false.
* @post @c size returns the distance between @c beg and @c end parameters.
*/
template <typename Iter>
wait_queue(std::stop_token stop_tok, Iter beg, Iter end)
wait_queue(std::stop_token stop_tok, Container&& container)
// noexcept(std::is_nothrow_constructible<Container, Iter, Iter>::value)
: m_stop_tok(stop_tok), m_data_queue(beg, end) {
assert(empty() == (beg == end));
assert((size() == size_type(0)) == (beg == end)); // std::distance constrains beg, end.
: m_stop_tok(stop_tok), m_data_queue(std::move(container)) {
// assert(empty() == (beg == end));
// assert((size() == size_type(0)) == (beg == end)); // std::distance constrains beg, end.
}

/**
Expand All @@ -285,7 +286,7 @@ class wait_queue {
*
* @param sz Capacity or initial size, depending on container type.
*
* @post If @c sz is 0 @c empty returns true, else behavior depends on container used.
* @post If @c sz is 0 @c empty returns @c true, else behavior depends on container used.
* @post @c size returns 0 or @c sz depending on container used.
*/
wait_queue(size_type sz)
Expand All @@ -305,7 +306,7 @@ class wait_queue {
*
* @param sz Capacity or initial size, depending on container type.
*
* @post If @c sz is 0 @c empty returns true, else behavior depends on container used.
* @post If @c sz is 0 @c empty returns @c true, else behavior depends on container used.
* @post @c size returns 0 or @c sz depending on container used.
*/
wait_queue(std::stop_token stop_tok, size_type sz)
Expand Down Expand Up @@ -360,7 +361,7 @@ class wait_queue {
* @return @c true if successful, @c false if the @c wait_queue has been
* requested to stop.
*
* @post If @c true is returned and @c empty is false, one of any threads waiting for a
* @post If @c true is returned and @c empty is @c false, one of any threads waiting for a
* value will be unblocked.
*/
auto push(const T& val) /* noexcept(std::is_nothrow_copy_constructible<T>::value) */
Expand All @@ -382,7 +383,7 @@ class wait_queue {
* This method has the same semantics as the other @c push, except that the value will
* be moved (if possible) instead of copied.
*
* @post If @c true is returned and @c empty is false, one of any threads waiting for a
* @post If @c true is returned and @c empty is @c false, one of any threads waiting for a
* value will be unblocked.
*/
auto push(T&& val) /* noexcept(std::is_nothrow_move_constructible<T>::value) */
Expand Down Expand Up @@ -411,7 +412,7 @@ class wait_queue {
* @return @c true if successful, @c false if the @c wait_queue is has been requested
* to stop.
*
* @post If @c true is returned and @c empty is false, one of any threads waiting for a
* @post If @c true is returned and @c empty is @c false, one of any threads waiting for a
* value will be unblocked.
*/
template <typename ... Args>
Expand Down
4 changes: 2 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ target_compile_features ( wait_queue_test PRIVATE cxx_std_20 )
# add dependencies
include ( ../cmake/download_cpm.cmake )

CPMAddPackage ( "gh:catchorg/Catch2@3.6.0" )
CPMAddPackage ( "gh:martinmoene/ring-span-lite@0.6.0" )
CPMAddPackage ( "gh:catchorg/Catch2@3.7.0" )
CPMAddPackage ( "gh:martinmoene/ring-span-lite@0.7.0" )
# CPMAddPackage ( "gh:JustasMasiulis/circular_buffer@master" )
CPMAddPackage ( NAME circular_buffer
URL https://github.com/JustasMasiulis/circular_buffer/archive/refs/heads/master.zip )
Expand Down
108 changes: 56 additions & 52 deletions test/wait_queue_test.cpp
A93C
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,6 @@ using namespace std::literals::string_literals;

constexpr int N = 40;

// WQ creation function with non-used pointer for overloading
template <typename T>
auto create_wait_queue(const std::deque<T>*) {
return chops::wait_queue<T, std::deque<T> > { };
}

template <typename T>
auto create_wait_queue(const nonstd::ring_span<T>*) {
static T buf[N];
return chops::wait_queue<T, nonstd::ring_span<T> > { buf+0, buf+N };
}

template <typename T>
auto create_wait_queue(const jm::circular_buffer<T, N>*) {
return chops::wait_queue<T, jm::circular_buffer<T, N> > { };
}


template <typename Q>
void non_threaded_push_test(Q& wq, const typename Q::value_type& val, int count) {

Expand Down Expand Up @@ -110,6 +92,28 @@ void non_threaded_arithmetic_test(Q& wq, int count) {

}

template <typename Q>
void non_threaded_test (Q& wq) {
using val_type = typename Q::value_type;
val_type val1;
val_type val2;
if constexpr (std::is_arithmetic_v<val_type>) {
val1 = 42;
val2 = 43;
}
else { // assume std::string value type in container - generalize as needed
val1 = "Howzit going, bro!";
val2 = "It's hanging, bro!";
}

non_threaded_push_test(wq, val1, N);
if constexpr (std::is_arithmetic_v<val_type>) {
non_threaded_arithmetic_test(wq, N);
}
}

// threaded testing code

template <typename T>
using set_elem = std::pair<int, T>;

Expand Down Expand Up @@ -187,36 +191,35 @@ bool threaded_test(Q& wq, int num_readers, int num_writers, int slice, const T&

// non threaded test, multiple container types, multiple element types

TEMPLATE_TEST_CASE ( "Non-threaded wait_queue test", "[wait_queue] [non_threaded]",
(std::deque<int>), (std::deque<double>), (std::deque<short>), (std::deque<std::string>),
(nonstd::ring_span<int>), (nonstd::ring_span<double>), (nonstd::ring_span<short>), (nonstd::ring_span<std::string>),
(jm::circular_buffer<int, N>), (jm::circular_buffer<double, N>),
(jm::circular_buffer<short, N>), (jm::circular_buffer<std::string, N>) ) {
TEMPLATE_TEST_CASE ( "Non-threaded wait_queue test, deque",
"[wait_queue] [non_threaded] [deque]",
int, double, short, std::string ) {
chops::wait_queue<TestType> wq;
non_threaded_test(wq);
}

using val_type = typename TestType::value_type;
val_type val1;
val_type val2;
if constexpr (std::is_arithmetic_v<val_type>) {
val1 = 42;
val2 = 43;
}
else { // assume std::string value type in container - generalize as needed
val1 = "Howzit going, bro!";
val2 = "It's hanging, bro!";
}
TEMPLATE_TEST_CASE ( "Non-threaded wait_queue test, ring_span",
"[wait_queue] [non_threaded] [ring_span]",
int, double, short, std::string ) {

auto wq = create_wait_queue( static_cast<const TestType*>(nullptr) );
non_threaded_push_test(wq, val1, N);
if constexpr (std::is_arithmetic_v<val_type>) {
non_threaded_arithmetic_test(wq, N);
}
static TestType buf[N];
chops::wait_queue<TestType, nonstd::ring_span<TestType>> wq
{ nonstd::ring_span<TestType> { buf+0, buf+N } };
non_threaded_test(wq);
}

TEMPLATE_TEST_CASE ( "Non-threaded wait_queue test, circular_buffer",
"[wait_queue] [non_threaded] [circular_buffer]",
int, double, short, std::string ) {
chops::wait_queue<TestType, jm::circular_buffer<TestType, N>> wq;
non_threaded_test(wq);
}

/*
*/

SCENARIO ( "Non-threaded wait_queue test, testing copy construction without move construction",
"[wait_queue] [no_move]" ) {
TEST_CASE ( "Non-threaded wait_queue test, testing copy construction without move construction",
"[wait_queue] [no_move]" ) {

struct Foo {
Foo() = delete;
Expand All @@ -235,8 +238,8 @@ SCENARIO ( "Non-threaded wait_queue test, testing copy construction without move
non_threaded_push_test(wq, Foo(42.0), N);
}

SCENARIO ( "Non-threaded wait_queue test, testing move construction without copy construction",
"[wait_queue] [no_copy]" ) {
TEST_CASE ( "Non-threaded wait_queue test, testing move construction without copy construction",
"[wait_queue] [no_copy]" ) {

struct Bar {
Bar() = delete;
Expand All @@ -262,8 +265,8 @@ SCENARIO ( "Non-threaded wait_queue test, testing move construction without copy
REQUIRE (wq.empty());
}

SCENARIO ( "Non-threaded wait_queue test, testing complex constructor and emplacement",
"[wait_queue] [complex_type] [deque]" ) {
TEST_CASE ( "Non-threaded wait_queue test, testing complex constructor and emplacement",
"[wait_queue] [complex_type] [deque]" ) {

struct Band {
using engagement_type = std::vector<std::vector<std::string> >;
Expand Down Expand Up @@ -375,12 +378,13 @@ TEST_CASE ( "Vector of vector of float, move and copy",

}

SCENARIO ( "Fixed size ring_span, testing wrap around with int type",
"[wait_queue] [int] [ring_span_wrap_around]" ) {
TEST_CASE ( "Fixed size ring_span, testing wrap around with int type",
"[wait_queue] [int] [ring_span_wrap_around]" ) {


int buf[N];
chops::wait_queue<int, nonstd::ring_span<int> > wq(buf+0, buf+N);
chops::wait_queue<int, nonstd::ring_span<int> > wq
{ nonstd::ring_span<int> { buf+0, buf+N } };

constexpr int Answer = 42;
constexpr int AnswerPlus = 42+5;
Expand Down Expand Up @@ -409,8 +413,8 @@ SCENARIO ( "Fixed size ring_span, testing wrap around with int type",
REQUIRE (wq.empty());
}

SCENARIO ( "Threaded wait queue, deque int",
"[wait_queue] [threaded] [int] [deque]" ) {
TEST_CASE ( "Threaded wait queue, deque int",
"[wait_queue] [threaded] [int] [deque]" ) {

chops::wait_queue<set_elem<int> > wq;

Expand All @@ -426,8 +430,8 @@ SCENARIO ( "Threaded wait queue, deque int",
}
}

SCENARIO ( "Threaded wait queue, deque string",
"[wait_queue] [threaded] [string] [deque]" ) {
TEST_CASE ( "Threaded wait queue, deque string",
"[wait_queue] [threaded] [string] [deque]" ) {

chops::wait_queue<set_elem<std::string> > wq;

Expand Down
0