mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
add: [concur] queue (TBD)
This commit is contained in:
parent
4e7688f109
commit
0b4f69266f
16
include/libconcur/bus.h
Normal file
16
include/libconcur/bus.h
Normal file
@ -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_
|
||||||
@ -29,6 +29,8 @@ enum : std::size_t {
|
|||||||
#else
|
#else
|
||||||
64,
|
64,
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
default_circle_buffer_size = 1024,
|
||||||
};
|
};
|
||||||
|
|
||||||
LIBCONCUR_NAMESPACE_END_
|
LIBCONCUR_NAMESPACE_END_
|
||||||
|
|||||||
@ -6,11 +6,74 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
#include "libimp/construct.h"
|
||||||
|
#include "libimp/detect_plat.h"
|
||||||
|
|
||||||
|
#include "libpmr/allocator.h"
|
||||||
|
#include "libpmr/memory_resource.h"
|
||||||
|
|
||||||
#include "libconcur/def.h"
|
#include "libconcur/def.h"
|
||||||
#include "libconcur/element.h"
|
#include "libconcur/concurrent.h"
|
||||||
|
|
||||||
LIBCONCUR_NAMESPACE_BEG_
|
LIBCONCUR_NAMESPACE_BEG_
|
||||||
|
|
||||||
|
template <typename T, typename PRelationT, typename CRelationT>
|
||||||
|
class queue {
|
||||||
|
public:
|
||||||
|
using producer_relation_t = PRelationT;
|
||||||
|
using consumer_relation_t = CRelationT;
|
||||||
|
using model_type = prod_cons<trans::unicast, producer_relation_t, consumer_relation_t>;
|
||||||
|
using value_type = T;
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct data {
|
||||||
|
model_type model_;
|
||||||
|
typename concur::traits<model_type>::header header_;
|
||||||
|
element<value_type> elements_start_;
|
||||||
|
|
||||||
|
template <typename U>
|
||||||
|
data(U &&model) noexcept
|
||||||
|
: header_(std::forward<U>(model)) {}
|
||||||
|
|
||||||
|
/// \brief element<value_type> elements[0];
|
||||||
|
element<value_type> *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<value_type>));
|
||||||
|
if (data_ptr == nullptr) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return ::LIBIMP::construct<data>(data_ptr, circ_size);
|
||||||
|
} LIBIMP_CATCH(...) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
::LIBPMR::allocator data_allocator_;
|
||||||
|
data *data_;
|
||||||
|
typename concur::traits<model_type>::context context_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
queue() = default;
|
||||||
|
queue(queue const &) = delete;
|
||||||
|
queue(queue &&) = delete;
|
||||||
|
queue &operator=(queue const &) = delete;
|
||||||
|
queue &operator=(queue &&) = delete;
|
||||||
|
|
||||||
|
template <typename MR, ::LIBPMR::verify_memory_resource<T> = 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_
|
LIBCONCUR_NAMESPACE_END_
|
||||||
|
|||||||
@ -47,7 +47,7 @@ class LIBIMP_EXPORT allocator {
|
|||||||
bool valid() const noexcept override { return false; }
|
bool valid() const noexcept override { return false; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename MemRes, typename = void>
|
template <typename MemRes, typename = bool>
|
||||||
class holder_memory_resource;
|
class holder_memory_resource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -55,7 +55,7 @@ class LIBIMP_EXPORT allocator {
|
|||||||
* \tparam MR memory resource type
|
* \tparam MR memory resource type
|
||||||
*/
|
*/
|
||||||
template <typename MR>
|
template <typename MR>
|
||||||
class holder_memory_resource<MR, is_memory_resource<MR>> : public holder_base {
|
class holder_memory_resource<MR, verify_memory_resource<MR>> : public holder_base {
|
||||||
MR *p_mem_res_;
|
MR *p_mem_res_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -102,7 +102,7 @@ public:
|
|||||||
|
|
||||||
/// \brief Constructs a allocator from a memory resource pointer
|
/// \brief Constructs a allocator from a memory resource pointer
|
||||||
/// The lifetime of the pointer must be longer than that of allocator.
|
/// The lifetime of the pointer must be longer than that of allocator.
|
||||||
template <typename T, typename = is_memory_resource<T>>
|
template <typename T, verify_memory_resource<T> = true>
|
||||||
allocator(T *p_mr) : allocator() {
|
allocator(T *p_mr) : allocator() {
|
||||||
if (p_mr == nullptr) return;
|
if (p_mr == nullptr) return;
|
||||||
::LIBIMP::construct<holder_memory_resource<T>>(holder_.data(), p_mr);
|
::LIBIMP::construct<holder_memory_resource<T>>(holder_.data(), p_mr);
|
||||||
|
|||||||
@ -35,9 +35,9 @@ struct has_deallocate<T,
|
|||||||
> : std::true_type {};
|
> : std::true_type {};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
using is_memory_resource =
|
using verify_memory_resource =
|
||||||
typename std::enable_if<has_allocate <T>::value &&
|
std::enable_if_t<has_allocate <T>::value &&
|
||||||
has_deallocate<T>::value>::type;
|
has_deallocate<T>::value, bool>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief A memory resource that uses the
|
* \brief A memory resource that uses the
|
||||||
|
|||||||
4
test/concur/test_concur_bus.cpp
Normal file
4
test/concur/test_concur_bus.cpp
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
|
#include "libconcur/bus.h"
|
||||||
Loading…
x
Reference in New Issue
Block a user