From 0b4f69266f61a952d6850a4bf3fa5ba153c51b9d Mon Sep 17 00:00:00 2001 From: mutouyun Date: Fri, 28 Apr 2023 13:25:29 +0800 Subject: [PATCH] add: [concur] queue (TBD) --- include/libconcur/bus.h | 16 ++++++++ include/libconcur/def.h | 2 + include/libconcur/queue.h | 65 +++++++++++++++++++++++++++++++- include/libpmr/allocator.h | 6 +-- include/libpmr/memory_resource.h | 6 +-- test/concur/test_concur_bus.cpp | 4 ++ 6 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 include/libconcur/bus.h create mode 100644 test/concur/test_concur_bus.cpp diff --git a/include/libconcur/bus.h b/include/libconcur/bus.h new file mode 100644 index 0000000..5f68c64 --- /dev/null +++ b/include/libconcur/bus.h @@ -0,0 +1,16 @@ +/** + * \file libconcur/bus.h + * \author mutouyun (orz@orzz.org) + * \brief Define concurrent data bus. + * \date 2023-04-17 + */ +#pragma once + +#include "libconcur/def.h" +#include "libconcur/concurrent.h" + +LIBCONCUR_NAMESPACE_BEG_ + + + +LIBCONCUR_NAMESPACE_END_ diff --git a/include/libconcur/def.h b/include/libconcur/def.h index 032f239..3e8b49c 100644 --- a/include/libconcur/def.h +++ b/include/libconcur/def.h @@ -29,6 +29,8 @@ enum : std::size_t { #else 64, #endif + + default_circle_buffer_size = 1024, }; LIBCONCUR_NAMESPACE_END_ diff --git a/include/libconcur/queue.h b/include/libconcur/queue.h index af17f7d..7aa20a1 100644 --- a/include/libconcur/queue.h +++ b/include/libconcur/queue.h @@ -6,11 +6,74 @@ */ #pragma once +#include + +#include "libimp/construct.h" +#include "libimp/detect_plat.h" + +#include "libpmr/allocator.h" +#include "libpmr/memory_resource.h" + #include "libconcur/def.h" -#include "libconcur/element.h" +#include "libconcur/concurrent.h" LIBCONCUR_NAMESPACE_BEG_ +template +class queue { +public: + using producer_relation_t = PRelationT; + using consumer_relation_t = CRelationT; + using model_type = prod_cons; + using value_type = T; +private: + struct data { + model_type model_; + typename concur::traits::header header_; + element elements_start_; + + template + data(U &&model) noexcept + : header_(std::forward(model)) {} + + /// \brief element elements[0]; + element *elements() noexcept { + return &elements_start_; + } + }; + + data *init(index_t circ_size) noexcept { + if (!data_allocator_) { + return nullptr; + } + LIBIMP_TRY { + auto data_ptr = data_allocator_.allocate(sizeof(data) + (circ_size - 1) * sizeof(element)); + if (data_ptr == nullptr) { + return nullptr; + } + return ::LIBIMP::construct(data_ptr, circ_size); + } LIBIMP_CATCH(...) { + return nullptr; + } + } + + ::LIBPMR::allocator data_allocator_; + data *data_; + typename concur::traits::context context_; + +public: + queue() = default; + queue(queue const &) = delete; + queue(queue &&) = delete; + queue &operator=(queue const &) = delete; + queue &operator=(queue &&) = delete; + + template = true> + explicit queue(index_t circ_size = default_circle_buffer_size, + MR *memory_resource = ::LIBPMR::new_delete_resource::get()) noexcept + : data_allocator_(memory_resource) + , data_(init(circ_size)) {} +}; LIBCONCUR_NAMESPACE_END_ diff --git a/include/libpmr/allocator.h b/include/libpmr/allocator.h index da5de38..51a3955 100644 --- a/include/libpmr/allocator.h +++ b/include/libpmr/allocator.h @@ -47,7 +47,7 @@ class LIBIMP_EXPORT allocator { bool valid() const noexcept override { return false; } }; - template + template class holder_memory_resource; /** @@ -55,7 +55,7 @@ class LIBIMP_EXPORT allocator { * \tparam MR memory resource type */ template - class holder_memory_resource> : public holder_base { + class holder_memory_resource> : public holder_base { MR *p_mem_res_; public: @@ -102,7 +102,7 @@ public: /// \brief Constructs a allocator from a memory resource pointer /// The lifetime of the pointer must be longer than that of allocator. - template > + template = true> allocator(T *p_mr) : allocator() { if (p_mr == nullptr) return; ::LIBIMP::construct>(holder_.data(), p_mr); diff --git a/include/libpmr/memory_resource.h b/include/libpmr/memory_resource.h index 004e821..523f6c2 100644 --- a/include/libpmr/memory_resource.h +++ b/include/libpmr/memory_resource.h @@ -35,9 +35,9 @@ struct has_deallocate : std::true_type {}; template -using is_memory_resource = - typename std::enable_if::value && - has_deallocate::value>::type; +using verify_memory_resource = + std::enable_if_t::value && + has_deallocate::value, bool>; /** * \brief A memory resource that uses the diff --git a/test/concur/test_concur_bus.cpp b/test/concur/test_concur_bus.cpp new file mode 100644 index 0000000..87776ec --- /dev/null +++ b/test/concur/test_concur_bus.cpp @@ -0,0 +1,4 @@ + +#include "gtest/gtest.h" + +#include "libconcur/bus.h"