add: [ipc] concurrent (TBD)

This commit is contained in:
mutouyun 2022-11-07 21:29:19 +08:00
parent 03dce063f9
commit 8e40adc1f1
4 changed files with 60 additions and 1 deletions

View File

@ -45,6 +45,7 @@ auto construct(void *p, A &&... args)
template <typename T>
void *destroy(T *p) noexcept {
if (p == nullptr) return nullptr;
#if defined(LIBIMP_CPP_17)
std::destroy_at(p);
#else
@ -55,6 +56,7 @@ void *destroy(T *p) noexcept {
template <typename T, std::size_t N>
void *destroy(T (*p)[N]) noexcept {
if (p == nullptr) return nullptr;
#if defined(LIBIMP_CPP_20)
std::destroy_at(p);
#elif defined(LIBIMP_CPP_17)

View File

@ -6,8 +6,11 @@
*/
#pragma once
#include <cstddef>
#include <cstddef> // std::max_align_t
#include <cstdint>
#include <new> // std::hardware_destructive_interference_size
#include "libimp/detect_plat.h"
#define LIBIPC_ ipc
#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_

26
src/libipc/concurrent.h Normal file
View 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_

View 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));
}