mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
add: [concur] define concurrent algorithms separately
This commit is contained in:
parent
8e40adc1f1
commit
2e0f074287
@ -1,11 +1,17 @@
|
|||||||
|
/**
|
||||||
|
* @file libconcur/concurrent.h
|
||||||
|
* @author mutouyun (orz@orzz.org)
|
||||||
|
* @brief Define different policies for concurrent queue
|
||||||
|
* @date 2022-11-07
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
|
||||||
#include "libipc/def.h"
|
#include "libconcur/def.h"
|
||||||
|
|
||||||
LIBIPC_NAMESPACE_BEG_
|
LIBCONCUR_NAMESPACE_BEG_
|
||||||
namespace concurrent {
|
|
||||||
|
|
||||||
/// @brief The queue index type.
|
/// @brief The queue index type.
|
||||||
using index_t = std::uint32_t;
|
using index_t = std::uint32_t;
|
||||||
@ -22,5 +28,4 @@ enum : flag_t {
|
|||||||
|
|
||||||
} // namespace state
|
} // namespace state
|
||||||
|
|
||||||
} // namespace concurrent
|
LIBCONCUR_NAMESPACE_END_
|
||||||
LIBIPC_NAMESPACE_END_
|
|
||||||
34
include/libconcur/def.h
Normal file
34
include/libconcur/def.h
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/**
|
||||||
|
* @file libconcur/def.h
|
||||||
|
* @author mutouyun (orz@orzz.org)
|
||||||
|
* @brief Define the trivial configuration information for concurrency
|
||||||
|
* @date 2022-11-07
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstddef> // std::max_align_t
|
||||||
|
#include <new> // std::hardware_destructive_interference_size
|
||||||
|
|
||||||
|
#include "libimp/detect_plat.h"
|
||||||
|
|
||||||
|
#define LIBCONCUR_ concur
|
||||||
|
#define LIBCONCUR_NAMESPACE_BEG_ namespace LIBCONCUR_ {
|
||||||
|
#define LIBCONCUR_NAMESPACE_END_ }
|
||||||
|
|
||||||
|
LIBCONCUR_NAMESPACE_BEG_
|
||||||
|
|
||||||
|
/// @brief Constants.
|
||||||
|
|
||||||
|
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
|
||||||
|
};
|
||||||
|
|
||||||
|
LIBCONCUR_NAMESPACE_END_
|
||||||
@ -6,11 +6,8 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstddef> // std::max_align_t
|
#include <cstddef>
|
||||||
#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_ {
|
||||||
@ -18,7 +15,7 @@
|
|||||||
|
|
||||||
LIBIPC_NAMESPACE_BEG_
|
LIBIPC_NAMESPACE_BEG_
|
||||||
|
|
||||||
// constants
|
/// @brief Constants.
|
||||||
|
|
||||||
struct prot {
|
struct prot {
|
||||||
using type = std::uint32_t;
|
using type = std::uint32_t;
|
||||||
@ -38,16 +35,4 @@ 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_
|
||||||
|
|||||||
16
test/test_concur_concurrent.cpp
Normal file
16
test/test_concur_concurrent.cpp
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
|
#include "libconcur/concurrent.h"
|
||||||
|
|
||||||
|
TEST(concurrent, cache_line_size) {
|
||||||
|
std::cout << concur::cache_line_size << "\n";
|
||||||
|
EXPECT_TRUE(concur::cache_line_size >= alignof(std::max_align_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(concurrent, index_and_flag) {
|
||||||
|
EXPECT_TRUE(sizeof(concur::index_t) < sizeof(concur::state::flag_t));
|
||||||
|
}
|
||||||
@ -1,16 +0,0 @@
|
|||||||
|
|
||||||
#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