mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
compatible with old compilers
This commit is contained in:
parent
7b5ab36fc1
commit
e5a5b9f680
@ -2,10 +2,9 @@ TEMPLATE = lib
|
|||||||
TARGET = ipc
|
TARGET = ipc
|
||||||
|
|
||||||
CONFIG -= qt
|
CONFIG -= qt
|
||||||
CONFIG += c++1z # may be useless
|
CONFIG += c++14 c++1z
|
||||||
|
|
||||||
msvc:QMAKE_CXXFLAGS += /std:c++17
|
!msvc:QMAKE_CXXFLAGS += -Wno-attributes -Wno-missing-field-initializers -Wno-unused-variable
|
||||||
else:QMAKE_CXXFLAGS += -std=gnu++1z
|
|
||||||
|
|
||||||
DEFINES += __IPC_LIBRARY__
|
DEFINES += __IPC_LIBRARY__
|
||||||
DESTDIR = ../output
|
DESTDIR = ../output
|
||||||
|
|||||||
@ -2,13 +2,14 @@ TEMPLATE = app
|
|||||||
|
|
||||||
QT += core testlib
|
QT += core testlib
|
||||||
QT -= gui
|
QT -= gui
|
||||||
CONFIG += console c++1z
|
|
||||||
|
CONFIG += console
|
||||||
|
CONFIG += c++14 c++1z
|
||||||
CONFIG -= app_bundle
|
CONFIG -= app_bundle
|
||||||
|
|
||||||
DESTDIR = ../output
|
DESTDIR = ../output
|
||||||
|
|
||||||
msvc:QMAKE_CXXFLAGS += /std:c++17
|
!msvc:QMAKE_CXXFLAGS += -Wno-attributes -Wno-missing-field-initializers -Wno-unused-variable -Wno-unused-function
|
||||||
else:QMAKE_CXXFLAGS += -std=gnu++1z -Wno-unused-function -Wno-attributes
|
|
||||||
|
|
||||||
INCLUDEPATH += \
|
INCLUDEPATH += \
|
||||||
../test \
|
../test \
|
||||||
|
|||||||
@ -36,6 +36,7 @@ public:
|
|||||||
void * data() noexcept;
|
void * data() noexcept;
|
||||||
void const * data() const noexcept;
|
void const * data() const noexcept;
|
||||||
|
|
||||||
|
# if __cplusplus >= 201703L
|
||||||
template <typename T>
|
template <typename T>
|
||||||
auto data() noexcept -> std::enable_if_t<!std::is_const_v<T>, T*> {
|
auto data() noexcept -> std::enable_if_t<!std::is_const_v<T>, T*> {
|
||||||
return static_cast<T*>(data());
|
return static_cast<T*>(data());
|
||||||
@ -45,6 +46,17 @@ public:
|
|||||||
auto data() const noexcept -> std::enable_if_t<std::is_const_v<T>, T*> {
|
auto data() const noexcept -> std::enable_if_t<std::is_const_v<T>, T*> {
|
||||||
return static_cast<T*>(data());
|
return static_cast<T*>(data());
|
||||||
}
|
}
|
||||||
|
# else /*__cplusplus < 201703L*/
|
||||||
|
template <typename T>
|
||||||
|
auto data() noexcept -> std::enable_if_t<!std::is_const<T>::value, T*> {
|
||||||
|
return static_cast<T*>(data());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
auto data() const noexcept -> std::enable_if_t<std::is_const<T>::value, T*> {
|
||||||
|
return static_cast<T*>(data());
|
||||||
|
}
|
||||||
|
# endif/*__cplusplus < 201703L*/
|
||||||
|
|
||||||
std::size_t size() const noexcept;
|
std::size_t size() const noexcept;
|
||||||
|
|
||||||
@ -57,7 +69,13 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::vector<byte_t> to_vector() const {
|
std::vector<byte_t> to_vector() const {
|
||||||
|
# if __cplusplus >= 201703L
|
||||||
auto [d, s] = to_tuple();
|
auto [d, s] = to_tuple();
|
||||||
|
# else /*__cplusplus < 201703L*/
|
||||||
|
auto tp = to_tuple();
|
||||||
|
auto d = std::get<0>(tp);
|
||||||
|
auto s = std::get<1>(tp);
|
||||||
|
# endif/*__cplusplus < 201703L*/
|
||||||
return {
|
return {
|
||||||
static_cast<byte_t const *>(d),
|
static_cast<byte_t const *>(d),
|
||||||
static_cast<byte_t const *>(d) + s
|
static_cast<byte_t const *>(d) + s
|
||||||
|
|||||||
@ -55,8 +55,17 @@ struct prod_cons<orgnz::cyclic, relat::single, relat::single, trans::unicast> {
|
|||||||
std::atomic<circ::detail::u2_t> rd_ { 0 }; // read index
|
std::atomic<circ::detail::u2_t> rd_ { 0 }; // read index
|
||||||
std::atomic<circ::detail::u2_t> wt_ { 0 }; // write index
|
std::atomic<circ::detail::u2_t> wt_ { 0 }; // write index
|
||||||
|
|
||||||
|
#if __cplusplus >= 201703L
|
||||||
template <std::size_t DataSize>
|
template <std::size_t DataSize>
|
||||||
constexpr static std::size_t elem_param = DataSize - sizeof(circ::detail::elem_head);
|
constexpr static std::size_t elem_param = DataSize - sizeof(circ::detail::elem_head);
|
||||||
|
#else /*__cplusplus < 201703L*/
|
||||||
|
template <std::size_t DataSize>
|
||||||
|
struct elem_param {
|
||||||
|
enum : std::size_t {
|
||||||
|
value = DataSize - sizeof(circ::detail::elem_head)
|
||||||
|
};
|
||||||
|
};
|
||||||
|
#endif/*__cplusplus < 201703L*/
|
||||||
|
|
||||||
constexpr circ::detail::u2_t cursor() const noexcept {
|
constexpr circ::detail::u2_t cursor() const noexcept {
|
||||||
return 0;
|
return 0;
|
||||||
@ -144,8 +153,13 @@ template <>
|
|||||||
struct prod_cons<orgnz::cyclic, relat::single, relat::multi, trans::broadcast> {
|
struct prod_cons<orgnz::cyclic, relat::single, relat::multi, trans::broadcast> {
|
||||||
std::atomic<circ::detail::u2_t> wt_ { 0 }; // write index
|
std::atomic<circ::detail::u2_t> wt_ { 0 }; // write index
|
||||||
|
|
||||||
|
#if __cplusplus >= 201703L
|
||||||
template <std::size_t DataSize>
|
template <std::size_t DataSize>
|
||||||
constexpr static std::size_t elem_param = DataSize;
|
constexpr static std::size_t elem_param = DataSize;
|
||||||
|
#else /*__cplusplus < 201703L*/
|
||||||
|
template <std::size_t DataSize>
|
||||||
|
struct elem_param { enum : std::size_t { value = DataSize }; };
|
||||||
|
#endif/*__cplusplus < 201703L*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
<Remarks> std::atomic<T> may not have value_type.
|
<Remarks> std::atomic<T> may not have value_type.
|
||||||
@ -248,7 +262,11 @@ public:
|
|||||||
using policy_t = Policy;
|
using policy_t = Policy;
|
||||||
using base_t = Policy;
|
using base_t = Policy;
|
||||||
using head_t = ipc::conn_head<detail::u2_t>;
|
using head_t = ipc::conn_head<detail::u2_t>;
|
||||||
|
#if __cplusplus >= 201703L
|
||||||
using elem_t = detail::elem_t<policy_t::template elem_param<DataSize>>;
|
using elem_t = detail::elem_t<policy_t::template elem_param<DataSize>>;
|
||||||
|
#else /*__cplusplus < 201703L*/
|
||||||
|
using elem_t = detail::elem_t<policy_t::template elem_param<DataSize>::value>;
|
||||||
|
#endif/*__cplusplus < 201703L*/
|
||||||
|
|
||||||
enum : std::size_t {
|
enum : std::size_t {
|
||||||
head_size = sizeof(policy_t) + sizeof(head_t),
|
head_size = sizeof(policy_t) + sizeof(head_t),
|
||||||
|
|||||||
@ -87,7 +87,7 @@ inline void sleep(K& k, F&& f) noexcept {
|
|||||||
|
|
||||||
template <std::size_t N = 4096, typename K>
|
template <std::size_t N = 4096, typename K>
|
||||||
inline void sleep(K& k) noexcept {
|
inline void sleep(K& k) noexcept {
|
||||||
sleep<N>(k, []() constexpr { return false; });
|
sleep<N>(k, [] { return false; });
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ipc
|
} // namespace ipc
|
||||||
|
|||||||
@ -11,8 +11,8 @@ namespace mem {
|
|||||||
|
|
||||||
class static_alloc {
|
class static_alloc {
|
||||||
public:
|
public:
|
||||||
static constexpr void clear() {}
|
static void clear() {}
|
||||||
static constexpr void swap(static_alloc&) {}
|
static void swap(static_alloc&) {}
|
||||||
|
|
||||||
static void* alloc(std::size_t size) {
|
static void* alloc(std::size_t size) {
|
||||||
return size ? std::malloc(size) : nullptr;
|
return size ? std::malloc(size) : nullptr;
|
||||||
@ -131,7 +131,11 @@ public:
|
|||||||
using alloc_policy = AllocP;
|
using alloc_policy = AllocP;
|
||||||
|
|
||||||
enum : std::size_t {
|
enum : std::size_t {
|
||||||
|
# if __cplusplus >= 201703L
|
||||||
block_size = (std::max)(BlockSize, sizeof(void*))
|
block_size = (std::max)(BlockSize, sizeof(void*))
|
||||||
|
# else /*__cplusplus < 201703L*/
|
||||||
|
block_size = (BlockSize < sizeof(void*)) ? sizeof(void*) : BlockSize
|
||||||
|
# endif/*__cplusplus < 201703L*/
|
||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@ -41,8 +41,13 @@ enum : std::size_t {
|
|||||||
base_size = sizeof(void*)
|
base_size = sizeof(void*)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#if __cplusplus >= 201703L
|
||||||
constexpr std::size_t classify(std::size_t size) {
|
constexpr std::size_t classify(std::size_t size) {
|
||||||
constexpr std::size_t mapping[] = {
|
constexpr std::size_t mapping[] = {
|
||||||
|
#else /*__cplusplus < 201703L*/
|
||||||
|
inline std::size_t classify(std::size_t size) {
|
||||||
|
static const std::size_t mapping[] = {
|
||||||
|
#endif/*__cplusplus < 201703L*/
|
||||||
/* 1 */
|
/* 1 */
|
||||||
0 , 1 , 2 , 3 ,
|
0 , 1 , 2 , 3 ,
|
||||||
/* 2 */
|
/* 2 */
|
||||||
@ -56,7 +61,11 @@ constexpr std::size_t classify(std::size_t size) {
|
|||||||
31, 31, 31, 31
|
31, 31, 31, 31
|
||||||
};
|
};
|
||||||
size = (size - 1) / base_size;
|
size = (size - 1) / base_size;
|
||||||
|
#if __cplusplus >= 201703L
|
||||||
return (size < std::size(mapping)) ? mapping[size] : 32;
|
return (size < std::size(mapping)) ? mapping[size] : 32;
|
||||||
|
#else /*__cplusplus < 201703L*/
|
||||||
|
return (size < (sizeof(mapping) / sizeof(mapping[0]))) ? mapping[size] : 32;
|
||||||
|
#endif/*__cplusplus < 201703L*/
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename F>
|
template <typename F>
|
||||||
|
|||||||
@ -10,11 +10,25 @@
|
|||||||
#include <functional>
|
#include <functional>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
#include "rw_lock.h"
|
#include "rw_lock.h"
|
||||||
#include "tls_pointer.h"
|
#include "tls_pointer.h"
|
||||||
|
|
||||||
namespace ipc {
|
namespace ipc {
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
#if __cplusplus >= 201703L
|
||||||
|
using std::unique_lock;
|
||||||
|
#else /*__cplusplus < 201703L*/
|
||||||
|
// deduction guides for std::unique_lock
|
||||||
|
template <typename T>
|
||||||
|
constexpr auto unique_lock(T&& lc) {
|
||||||
|
return std::unique_lock<std::decay_t<T>> { std::forward<T>(lc) };
|
||||||
|
}
|
||||||
|
#endif/*__cplusplus < 201703L*/
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
namespace mem {
|
namespace mem {
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////
|
||||||
@ -38,7 +52,7 @@ private:
|
|||||||
alloc_t(synchronized* t)
|
alloc_t(synchronized* t)
|
||||||
: t_ { t } {
|
: t_ { t } {
|
||||||
{
|
{
|
||||||
[[maybe_unused]] auto guard = std::unique_lock { t_->lc_ };
|
[[maybe_unused]] auto guard = ipc::detail::unique_lock(t_->lc_);
|
||||||
auto it = t_->allocs_.begin();
|
auto it = t_->allocs_.begin();
|
||||||
if (it != t_->allocs_.end()) {
|
if (it != t_->allocs_.end()) {
|
||||||
std::tie(s_, a_) = *it;
|
std::tie(s_, a_) = *it;
|
||||||
@ -51,7 +65,7 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
~alloc_t() {
|
~alloc_t() {
|
||||||
[[maybe_unused]] auto guard = std::unique_lock { t_->lc_ };
|
[[maybe_unused]] auto guard = ipc::detail::unique_lock(t_->lc_);
|
||||||
t_->allocs_.emplace(s_, a_);
|
t_->allocs_.emplace(s_, a_);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,7 +96,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void clear() {
|
void clear() {
|
||||||
auto guard = std::unique_lock { lc_ };
|
auto guard = ipc::detail::unique_lock(lc_);
|
||||||
std::vector<alloc_policy*> vec(allocs_.size());
|
std::vector<alloc_policy*> vec(allocs_.size());
|
||||||
std::size_t i = 0;
|
std::size_t i = 0;
|
||||||
for (auto& pair : allocs_) {
|
for (auto& pair : allocs_) {
|
||||||
|
|||||||
@ -65,7 +65,7 @@ void* acquire(char const * name, std::size_t size) {
|
|||||||
auto acc = acc_of(mem);
|
auto acc = acc_of(mem);
|
||||||
acc->fetch_add(1, std::memory_order_release);
|
acc->fetch_add(1, std::memory_order_release);
|
||||||
{
|
{
|
||||||
[[maybe_unused]] auto guard = std::unique_lock { m2h()->lc_ };
|
[[maybe_unused]] auto guard = ipc::detail::unique_lock(m2h()->lc_);
|
||||||
m2h()->cache_.emplace(++acc, std::move(op_name));
|
m2h()->cache_.emplace(++acc, std::move(op_name));
|
||||||
}
|
}
|
||||||
return acc;
|
return acc;
|
||||||
@ -75,7 +75,7 @@ void release(void* mem, std::size_t size) {
|
|||||||
if (mem == nullptr) {
|
if (mem == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
[[maybe_unused]] auto guard = std::unique_lock { m2h()->lc_ };
|
[[maybe_unused]] auto guard = ipc::detail::unique_lock(m2h()->lc_);
|
||||||
auto& cc = m2h()->cache_;
|
auto& cc = m2h()->cache_;
|
||||||
auto it = cc.find(mem);
|
auto it = cc.find(mem);
|
||||||
if (it == cc.end()) {
|
if (it == cc.end()) {
|
||||||
|
|||||||
@ -43,7 +43,7 @@ void* acquire(char const * name, std::size_t size) {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
[[maybe_unused]] auto guard = std::unique_lock { m2h()->lc_ };
|
[[maybe_unused]] auto guard = ipc::detail::unique_lock(m2h()->lc_);
|
||||||
m2h()->cache_.emplace(mem, h);
|
m2h()->cache_.emplace(mem, h);
|
||||||
}
|
}
|
||||||
return mem;
|
return mem;
|
||||||
@ -53,7 +53,7 @@ void release(void* mem, std::size_t /*size*/) {
|
|||||||
if (mem == nullptr) {
|
if (mem == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
[[maybe_unused]] auto guard = std::unique_lock { m2h()->lc_ };
|
[[maybe_unused]] auto guard = ipc::detail::unique_lock(m2h()->lc_);
|
||||||
auto& cc = m2h()->cache_;
|
auto& cc = m2h()->cache_;
|
||||||
auto it = cc.find(mem);
|
auto it = cc.find(mem);
|
||||||
if (it == cc.end()) {
|
if (it == cc.end()) {
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
|
#if __cplusplus >= 201703L
|
||||||
namespace std {
|
namespace std {
|
||||||
|
|
||||||
// deduction guides for std::unique_ptr
|
// deduction guides for std::unique_ptr
|
||||||
@ -18,6 +19,19 @@ unique_ptr(T* p, D&& d) -> unique_ptr<T, std::decay_t<D>>;
|
|||||||
namespace ipc {
|
namespace ipc {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
|
using std::unique_ptr;
|
||||||
|
|
||||||
|
#else /*__cplusplus < 201703L*/
|
||||||
|
namespace ipc {
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
// deduction guides for std::unique_ptr
|
||||||
|
template <typename T, typename D>
|
||||||
|
constexpr auto unique_ptr(T* p, D&& d) {
|
||||||
|
return std::unique_ptr<T, std::decay_t<D>> { p, std::forward<D>(d) };
|
||||||
|
}
|
||||||
|
#endif/*__cplusplus < 201703L*/
|
||||||
|
|
||||||
class waiter {
|
class waiter {
|
||||||
pthread_mutex_t mutex_ = PTHREAD_MUTEX_INITIALIZER;
|
pthread_mutex_t mutex_ = PTHREAD_MUTEX_INITIALIZER;
|
||||||
pthread_cond_t cond_ = PTHREAD_COND_INITIALIZER;
|
pthread_cond_t cond_ = PTHREAD_COND_INITIALIZER;
|
||||||
@ -44,20 +58,20 @@ public:
|
|||||||
if (::pthread_mutexattr_init(&mutex_attr) != 0) {
|
if (::pthread_mutexattr_init(&mutex_attr) != 0) {
|
||||||
return invalid();
|
return invalid();
|
||||||
}
|
}
|
||||||
[[maybe_unused]] auto guard_mutex_attr = std::unique_ptr { &mutex_attr, ::pthread_mutexattr_destroy };
|
[[maybe_unused]] auto guard_mutex_attr = unique_ptr(&mutex_attr, ::pthread_mutexattr_destroy);
|
||||||
if (::pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED) != 0) {
|
if (::pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED) != 0) {
|
||||||
return invalid();
|
return invalid();
|
||||||
}
|
}
|
||||||
if (::pthread_mutex_init(&mutex_, &mutex_attr) != 0) {
|
if (::pthread_mutex_init(&mutex_, &mutex_attr) != 0) {
|
||||||
return invalid();
|
return invalid();
|
||||||
}
|
}
|
||||||
auto guard_mutex = std::unique_ptr { &mutex_, ::pthread_mutex_destroy };
|
auto guard_mutex = unique_ptr(&mutex_, ::pthread_mutex_destroy);
|
||||||
// init condition
|
// init condition
|
||||||
pthread_condattr_t cond_attr;
|
pthread_condattr_t cond_attr;
|
||||||
if (::pthread_condattr_init(&cond_attr) != 0) {
|
if (::pthread_condattr_init(&cond_attr) != 0) {
|
||||||
return invalid();
|
return invalid();
|
||||||
}
|
}
|
||||||
[[maybe_unused]] auto guard_cond_attr = std::unique_ptr { &cond_attr, ::pthread_condattr_destroy };
|
[[maybe_unused]] auto guard_cond_attr = unique_ptr(&cond_attr, ::pthread_condattr_destroy);
|
||||||
if (::pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED) != 0) {
|
if (::pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED) != 0) {
|
||||||
return invalid();
|
return invalid();
|
||||||
}
|
}
|
||||||
@ -86,7 +100,7 @@ public:
|
|||||||
if (::pthread_mutex_lock(&(w->mutex_)) != 0) {
|
if (::pthread_mutex_lock(&(w->mutex_)) != 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
[[maybe_unused]] auto guard = std::unique_ptr { &(w->mutex_), ::pthread_mutex_unlock };
|
[[maybe_unused]] auto guard = unique_ptr(&(w->mutex_), ::pthread_mutex_unlock);
|
||||||
if (::pthread_cond_wait(&(w->cond_), &(w->mutex_)) != 0) {
|
if (::pthread_cond_wait(&(w->cond_), &(w->mutex_)) != 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -201,12 +201,12 @@ struct test_cq<ipc::queue<T...>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cn_t connect_send() {
|
cn_t* connect_send() {
|
||||||
return cn_t{ ca_ };
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void send(cn_t& cn, msg_t const & msg) {
|
void send(cn_t* /*cn*/, msg_t const & msg) {
|
||||||
cn.push(msg);
|
cn_t{ ca_ }.push(msg);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user