From 0eb61b8745b69a1da3bce54bc397fcd9cbc2ca0d Mon Sep 17 00:00:00 2001 From: mutouyun Date: Mon, 24 Dec 2018 11:56:42 +0800 Subject: [PATCH] adjust file naming; supplement test cases --- build/src.pro | 6 +++--- src/{channel.hpp => channel.inc} | 23 ++++++++++++++------- src/{id_pool.hpp => id_pool.inc} | 0 src/ipc.cpp | 4 ++-- src/{route.hpp => route.inc} | 2 ++ test/test_ipc.cpp | 35 +++++++++++++++++++++++++++++++- 6 files changed, 57 insertions(+), 13 deletions(-) rename src/{channel.hpp => channel.inc} (88%) rename src/{id_pool.hpp => id_pool.inc} (100%) rename src/{route.hpp => route.inc} (97%) diff --git a/build/src.pro b/build/src.pro index 5777716..e279c7a 100644 --- a/build/src.pro +++ b/build/src.pro @@ -24,9 +24,9 @@ HEADERS += \ ../include/def.h \ ../include/rw_lock.h \ ../include/tls_pointer.h \ - ../src/route.hpp \ - ../src/channel.hpp \ - ../src/id_pool.hpp + ../src/channel.inc \ + ../src/route.inc \ + ../src/id_pool.inc SOURCES += \ ../src/shm.cpp \ diff --git a/src/channel.hpp b/src/channel.inc similarity index 88% rename from src/channel.hpp rename to src/channel.inc index 0468831..264a688 100644 --- a/src/channel.hpp +++ b/src/channel.inc @@ -7,12 +7,13 @@ #include #include #include +#include #include "def.h" #include "shm.h" #include "rw_lock.h" -#include "id_pool.hpp" +#include "id_pool.inc" namespace { @@ -131,23 +132,31 @@ std::size_t channel::recv_count() const { return impl(p_)->r_.recv_count(); } +template +inline bool channel_send(route& rt, P&&... params) { + while (rt.recv_count() == 0) { + std::this_thread::yield(); + } + return rt.send(params...); // no need std::forward +} + bool channel::send(void const * data, std::size_t size) { - return impl(p_)->r_.send(data, size); + return channel_send(impl(p_)->r_, data, size); } bool channel::send(buff_t const & buff) { - return impl(p_)->r_.send(buff); + return channel_send(impl(p_)->r_, buff); } bool channel::send(std::string const & str) { - return impl(p_)->r_.send(str); + return channel_send(impl(p_)->r_, str); } buff_t channel::recv() { if (!valid()) return {}; std::array ques; return ipc::multi_recv([&] { - std::array acqeds; + std::array acqs; std::size_t counter = 0; std::unordered_map cache; // get all acquired ids @@ -156,13 +165,13 @@ buff_t channel::recv() { impl(p_)->acc().for_each([&](std::size_t id, bool acquired) { if (id == impl(p_)->id_) return; if (acquired) { - acqeds[counter++] = id; + acqs[counter++] = id; } }); } // populate route cache & ques for (std::size_t i = 0; i < counter; ++i) { - auto id = acqeds[i]; + auto id = acqs[i]; auto it = impl(p_)->rts_.find(id); // it's a new id if (it == impl(p_)->rts_.end()) { diff --git a/src/id_pool.hpp b/src/id_pool.inc similarity index 100% rename from src/id_pool.hpp rename to src/id_pool.inc diff --git a/src/ipc.cpp b/src/ipc.cpp index 1094153..42b6abf 100644 --- a/src/ipc.cpp +++ b/src/ipc.cpp @@ -193,5 +193,5 @@ buff_t recv(handle_t h) { } // namespace ipc -#include "route.hpp" -#include "channel.hpp" +#include "route.inc" +#include "channel.inc" diff --git a/src/route.hpp b/src/route.inc similarity index 97% rename from src/route.hpp rename to src/route.inc index 9bdfc07..f851302 100644 --- a/src/route.hpp +++ b/src/route.inc @@ -65,8 +65,10 @@ bool route::connect(char const * name) { } void route::disconnect() { + if (!valid()) return; ipc::disconnect(impl(p_)->h_); impl(p_)->h_ = nullptr; + impl(p_)->n_.clear(); } std::size_t route::recv_count() const { diff --git a/test/test_ipc.cpp b/test/test_ipc.cpp index 089e83b..9ac1f97 100644 --- a/test/test_ipc.cpp +++ b/test/test_ipc.cpp @@ -123,6 +123,7 @@ private slots: void test_route(); void test_route_performance(); void test_channel(); + void test_channel_rtt(); } unit__; #include "test_ipc.moc" @@ -158,7 +159,7 @@ struct lc_wrapper : Mutex { void unlock_shared() { Mutex::unlock(); } }; -template +template void benchmark_lc() { std::thread w_trd[W]; std::thread r_trd[R]; @@ -415,4 +416,36 @@ void Unit::test_channel() { t2.join(); } +void Unit::test_channel_rtt() { + test_stopwatch sw; + + std::thread t1 {[&] { + ipc::channel cc { "my-ipc-channel" }; + for (std::size_t i = 0;; ++i) { + auto dd = cc.recv(); + if (dd.size() < 2) return; +// std::cout << "recving: " << i << "-[" << dd.size() << "]" << std::endl; + cc.send(ipc::buff_t { 'a' }); + } + }}; + + std::thread t2 {[&] { + ipc::channel cc { "my-ipc-channel" }; + sw.start(); + for (std::size_t i = 0; i < LoopCount; ++i) { +// std::cout << "sending: " << i << "-[" << datas__[i].size() << "]" << std::endl; + cc.send(datas__[i]); + /*auto dd = */cc.recv(); +// if (dd.size() != 1 || dd[0] != 'a') { +// QVERIFY(false); +// } + } + cc.send(ipc::buff_t { '\0' }); + t1.join(); + sw.print_elapsed(DataMin, DataMax, LoopCount); + }}; + + t2.join(); +} + } // internal-linkage