mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-07 01:06:45 +08:00
add: [ipc] concurrent (TBD)
This commit is contained in:
parent
03dce063f9
commit
8e40adc1f1
@ -45,6 +45,7 @@ auto construct(void *p, A &&... args)
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void *destroy(T *p) noexcept {
|
void *destroy(T *p) noexcept {
|
||||||
|
if (p == nullptr) return nullptr;
|
||||||
#if defined(LIBIMP_CPP_17)
|
#if defined(LIBIMP_CPP_17)
|
||||||
std::destroy_at(p);
|
std::destroy_at(p);
|
||||||
#else
|
#else
|
||||||
@ -55,6 +56,7 @@ void *destroy(T *p) noexcept {
|
|||||||
|
|
||||||
template <typename T, std::size_t N>
|
template <typename T, std::size_t N>
|
||||||
void *destroy(T (*p)[N]) noexcept {
|
void *destroy(T (*p)[N]) noexcept {
|
||||||
|
if (p == nullptr) return nullptr;
|
||||||
#if defined(LIBIMP_CPP_20)
|
#if defined(LIBIMP_CPP_20)
|
||||||
std::destroy_at(p);
|
std::destroy_at(p);
|
||||||
#elif defined(LIBIMP_CPP_17)
|
#elif defined(LIBIMP_CPP_17)
|
||||||
|
|||||||
@ -6,8 +6,11 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef> // std::max_align_t
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <new> // std::hardware_destructive_interference_size
|
||||||
|
|
||||||
|
#include "libimp/detect_plat.h"
|
||||||
|
|
||||||
#define LIBIPC_ ipc
|
#define LIBIPC_ ipc
|
||||||
#define LIBIPC_NAMESPACE_BEG_ namespace LIBIPC_ {
|
#define LIBIPC_NAMESPACE_BEG_ namespace LIBIPC_ {
|
||||||
@ -35,4 +38,16 @@ struct mode {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum : std::size_t {
|
||||||
|
/// @brief Minimum offset between two objects to avoid false sharing.
|
||||||
|
/// @see https://en.cppreference.com/w/cpp/thread/hardware_destructive_interference_size
|
||||||
|
cache_line_size =
|
||||||
|
#if defined(LIBIMP_CPP_17) && defined(__cpp_lib_hardware_interference_size)
|
||||||
|
( std::hardware_destructive_interference_size < alignof(std::max_align_t) ) ? 64
|
||||||
|
: std::hardware_destructive_interference_size,
|
||||||
|
#else
|
||||||
|
64,
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
LIBIPC_NAMESPACE_END_
|
LIBIPC_NAMESPACE_END_
|
||||||
|
|||||||
26
src/libipc/concurrent.h
Normal file
26
src/libipc/concurrent.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
|
#include "libipc/def.h"
|
||||||
|
|
||||||
|
LIBIPC_NAMESPACE_BEG_
|
||||||
|
namespace concurrent {
|
||||||
|
|
||||||
|
/// @brief The queue index type.
|
||||||
|
using index_t = std::uint32_t;
|
||||||
|
|
||||||
|
namespace state {
|
||||||
|
|
||||||
|
/// @brief The state flag type for the queue element.
|
||||||
|
using flag_t = std::uint64_t;
|
||||||
|
|
||||||
|
enum : flag_t {
|
||||||
|
/// @brief The invalid state value.
|
||||||
|
invalid_value = (std::numeric_limits<flag_t>::max)(),
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace state
|
||||||
|
|
||||||
|
} // namespace concurrent
|
||||||
|
LIBIPC_NAMESPACE_END_
|
||||||
16
test/test_ipc_concurrent.cpp
Normal file
16
test/test_ipc_concurrent.cpp
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
|
#include "libipc/concurrent.h"
|
||||||
|
|
||||||
|
TEST(concurrent, cache_line_size) {
|
||||||
|
std::cout << ipc::cache_line_size << "\n";
|
||||||
|
EXPECT_TRUE(ipc::cache_line_size >= alignof(std::max_align_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(concurrent, index_and_flag) {
|
||||||
|
EXPECT_TRUE(sizeof(ipc::concurrent::index_t) < sizeof(ipc::concurrent::state::flag_t));
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user