From e86d3e10e153389d622f7b5ec9c3d26d03d902ff Mon Sep 17 00:00:00 2001 From: mutouyun Date: Sat, 26 Jan 2019 11:20:04 +0800 Subject: [PATCH] modify producer-consumer policy flag --- README.md | 2 +- build/test.pro | 3 +-- build/test/CMakeLists.txt | 2 +- include/def.h | 4 ++-- include/ipc.h | 39 +++++++++++++++++++++------------------ src/channel.cpp | 12 ++++++------ src/ipc.cpp | 22 +++++++++++----------- src/prod_cons.h | 16 ++++++++-------- test/test_circ.cpp | 4 ++-- 9 files changed, 53 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 9e7f45c..50a0437 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ A high-performance inter-process communication using shared memory on Linux/Wind * 除STL外,无其他依赖 * 无锁(lock-free)或轻量级spin-lock * 底层数据结构为循环数组(circular array) - * `ipc::route`支持单生产多消费,`ipc::channel`支持多生产多消费 + * `ipc::route`支持单写多读,`ipc::channel`支持多写多读 ## Usage diff --git a/build/test.pro b/build/test.pro index 0520f79..d74222d 100644 --- a/build/test.pro +++ b/build/test.pro @@ -13,8 +13,7 @@ DESTDIR = ../output -Wno-attributes \ -Wno-missing-field-initializers \ -Wno-unused-variable \ - -Wno-unused-function \ - -Wno-class-memaccess + -Wno-unused-function INCLUDEPATH += \ ../test \ diff --git a/build/test/CMakeLists.txt b/build/test/CMakeLists.txt index cfb33c7..da9da36 100644 --- a/build/test/CMakeLists.txt +++ b/build/test/CMakeLists.txt @@ -6,7 +6,7 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON) find_package(Qt5 COMPONENTS Core Test REQUIRED) if(NOT MSVC) - add_compile_options(-Wno-attributes -Wno-missing-field-initializers -Wno-unused-variable -Wno-unused-function -Wno-class-memaccess) + add_compile_options(-Wno-attributes -Wno-missing-field-initializers -Wno-unused-variable -Wno-unused-function) endif() include_directories(../../include ../../src ../../test ../../test/capo) diff --git a/include/def.h b/include/def.h index b9eb348..4e704ef 100644 --- a/include/def.h +++ b/include/def.h @@ -44,10 +44,10 @@ enum class trans { // transmission // producer-consumer policy flag template -struct prod_cons {}; +struct wr {}; // implement with multi routes -struct prod_cons_routes {}; +struct wr_routes {}; // concept helpers diff --git a/include/ipc.h b/include/ipc.h index 501b225..7e4d0a9 100644 --- a/include/ipc.h +++ b/include/ipc.h @@ -14,7 +14,7 @@ using handle_t = void*; using buff_t = buffer; template -struct IPC_EXPORT channel_detail { +struct IPC_EXPORT chan_impl { static handle_t connect (char const * name); static void disconnect(handle_t h); @@ -26,7 +26,7 @@ struct IPC_EXPORT channel_detail { }; template <> -struct IPC_EXPORT channel_detail { +struct IPC_EXPORT chan_impl { static handle_t connect (char const * name); static void disconnect(handle_t h); @@ -38,34 +38,34 @@ struct IPC_EXPORT channel_detail { }; template -class channel_impl { +class chan_wrapper { private: - using detail_t = channel_detail; + using detail_t = chan_impl; handle_t h_ = nullptr; std::string n_; public: - channel_impl() = default; + chan_wrapper() = default; - explicit channel_impl(char const * name) { + explicit chan_wrapper(char const * name) { this->connect(name); } - channel_impl(channel_impl&& rhs) { + chan_wrapper(chan_wrapper&& rhs) { swap(rhs); } - ~channel_impl() { + ~chan_wrapper() { disconnect(); } - void swap(channel_impl& rhs) { + void swap(chan_wrapper& rhs) { std::swap(h_, rhs.h_); n_.swap(rhs.n_); } - channel_impl& operator=(channel_impl rhs) { + chan_wrapper& operator=(chan_wrapper rhs) { swap(rhs); return *this; } @@ -82,8 +82,8 @@ public: return (handle() != nullptr); } - channel_impl clone() const { - return channel_impl { name() }; + chan_wrapper clone() const { + return chan_wrapper { name() }; } bool connect(char const * name) { @@ -109,7 +109,7 @@ public: } static bool wait_for_recv(char const * name, std::size_t r_count) { - return channel_impl(name).wait_for_recv(r_count); + return chan_wrapper(name).wait_for_recv(r_count); } bool send(void const * data, std::size_t size) { @@ -129,6 +129,9 @@ public: } }; +template +using chan = chan_wrapper; + /* * class route * @@ -137,19 +140,19 @@ public: * would receive your sent messages. * * A route could only be used in 1 to N - * (one producer/server/sender to multi consumers/clients/receivers) + * (one producer/writer to multi consumers/readers) */ -using route = channel_impl>; +using route = chan>; /* * class channel * - * You could use multi producers/servers/senders for sending messages to a channel, - * then all the consumers/clients/receivers which are receiving with this channel, + * You could use multi producers/writers for sending messages to a channel, + * then all the consumers/readers which are receiving with this channel, * would receive your sent messages. */ -using channel = channel_impl>; +using channel = chan>; } // namespace ipc diff --git a/src/channel.cpp b/src/channel.cpp index a3a1e58..19e75bd 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -88,26 +88,26 @@ struct ch_multi_routes { namespace ipc { -ipc::handle_t channel_detail::connect(char const * /*name*/) { +ipc::handle_t chan_impl::connect(char const * /*name*/) { return nullptr; } -void channel_detail::disconnect(ipc::handle_t /*h*/) { +void chan_impl::disconnect(ipc::handle_t /*h*/) { } -std::size_t channel_detail::recv_count(ipc::handle_t /*h*/) { +std::size_t chan_impl::recv_count(ipc::handle_t /*h*/) { return 0; } -bool channel_detail::wait_for_recv(ipc::handle_t /*h*/, std::size_t /*r_count*/) { +bool chan_impl::wait_for_recv(ipc::handle_t /*h*/, std::size_t /*r_count*/) { return false; } -bool channel_detail::send(ipc::handle_t /*h*/, void const * /*data*/, std::size_t /*size*/) { +bool chan_impl::send(ipc::handle_t /*h*/, void const * /*data*/, std::size_t /*size*/) { return false; } -buff_t channel_detail::recv(ipc::handle_t /*h*/) { +buff_t chan_impl::recv(ipc::handle_t /*h*/) { return {}; } diff --git a/src/ipc.cpp b/src/ipc.cpp index 450baf2..5293e96 100644 --- a/src/ipc.cpp +++ b/src/ipc.cpp @@ -214,39 +214,39 @@ using policy_t = policy::choose; namespace ipc { template -ipc::handle_t channel_detail::connect(char const * name) { +ipc::handle_t chan_impl::connect(char const * name) { return detail_impl>::connect(name); } template -void channel_detail::disconnect(ipc::handle_t h) { +void chan_impl::disconnect(ipc::handle_t h) { detail_impl>::disconnect(h); } template -std::size_t channel_detail::recv_count(ipc::handle_t h) { +std::size_t chan_impl::recv_count(ipc::handle_t h) { return detail_impl>::recv_count(h); } template -bool channel_detail::wait_for_recv(ipc::handle_t h, std::size_t r_count) { +bool chan_impl::wait_for_recv(ipc::handle_t h, std::size_t r_count) { return detail_impl>::wait_for_recv(h, r_count); } template -bool channel_detail::send(ipc::handle_t h, void const * data, std::size_t size) { +bool chan_impl::send(ipc::handle_t h, void const * data, std::size_t size) { return detail_impl>::send(h, data, size); } template -buff_t channel_detail::recv(ipc::handle_t h) { +buff_t chan_impl::recv(ipc::handle_t h) { return detail_impl>::recv(h); } -template struct channel_detail>; -template struct channel_detail>; -template struct channel_detail>; -template struct channel_detail>; -template struct channel_detail>; +template struct chan_impl>; +template struct chan_impl>; +template struct chan_impl>; +template struct chan_impl>; +template struct chan_impl>; } // namespace ipc diff --git a/src/prod_cons.h b/src/prod_cons.h index 9c571d4..ac1d17c 100644 --- a/src/prod_cons.h +++ b/src/prod_cons.h @@ -19,7 +19,7 @@ template struct prod_cons_impl; template <> -struct prod_cons_impl> { +struct prod_cons_impl> { std::atomic rd_; // read index std::atomic wt_; // write index @@ -63,8 +63,8 @@ struct prod_cons_impl> { }; template <> -struct prod_cons_impl> - : prod_cons_impl> { +struct prod_cons_impl> + : prod_cons_impl> { template bool pop(E* /*elems*/, circ::u2_t& /*cur*/, F&& f, EB* elem_start) { @@ -86,8 +86,8 @@ struct prod_cons_impl> }; template <> -struct prod_cons_impl> - : prod_cons_impl> { +struct prod_cons_impl> + : prod_cons_impl> { std::atomic ct_; // commit index @@ -118,7 +118,7 @@ struct prod_cons_impl> }; template <> -struct prod_cons_impl> { +struct prod_cons_impl> { std::atomic wt_; // write index #if __cplusplus >= 201703L @@ -180,8 +180,8 @@ struct prod_cons_impl> }; template <> -struct prod_cons_impl> - : prod_cons_impl> { +struct prod_cons_impl> + : prod_cons_impl> { std::atomic ct_; // commit index diff --git a/test/test_circ.cpp b/test/test_circ.cpp index 5e722db..9b982cd 100644 --- a/test/test_circ.cpp +++ b/test/test_circ.cpp @@ -21,7 +21,7 @@ struct msg_t { }; template -using pc_t = ipc::prod_cons_impl>; +using pc_t = ipc::prod_cons_impl>; template struct ea_t : public ipc::circ::elem_array { @@ -372,7 +372,7 @@ void Unit::test_prod_cons_performance() { void Unit::test_queue() { using queue_t = ipc::queue + ipc::wr >>; queue_t queue;