From 3931f83205f153f2bc7fc36d1a894cdc3f14b4db Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期三, 21 四月 2021 16:52:51 +0800 Subject: [PATCH] change node socket to vector; try lock free queue. --- src/sendq.cpp | 91 ++++++++++++++++++++++++++++++++++----------- 1 files changed, 68 insertions(+), 23 deletions(-) diff --git a/src/sendq.cpp b/src/sendq.cpp index 4b1f947..4be24f1 100644 --- a/src/sendq.cpp +++ b/src/sendq.cpp @@ -19,44 +19,73 @@ #include "shm_queue.h" #include <chrono> -bool SendQ::TrySend(bhome_shm::ShmMsgQueue &mq) +int SendQ::DoSend1Remote(bhome_shm::ShmMsgQueue &mq, const Remote &remote, Array &arr) { - if (out_.empty()) { - std::unique_lock<std::mutex> lock(mutex_); - out_.swap(in_); - } - - auto FirstNotExpired = [](MsgList &l) { + auto FirstNotExpired = [](Array &l) { auto Less = [](const TimedMsg &msg, const TimePoint &tp) { return msg.expire() < tp; }; return std::lower_bound(l.begin(), l.end(), Now(), Less); }; - auto SendOneRemote = [&](const Remote &remote, MsgList &msg_list) { - auto pos = FirstNotExpired(msg_list); - for (auto it = msg_list.begin(); it != pos; ++it) { - auto &info = it->data(); - if (info.on_expire_) { - info.on_expire_(info.msg_); - } - info.msg_.Release(mq.shm()); + auto pos = FirstNotExpired(arr); + for (auto it = arr.begin(); it != pos; ++it) { + auto &info = it->data(); + if (info.on_expire_) { + info.on_expire_(info.data_); } + if (info.data_.index() == 0) { + boost::variant2::get<0>(info.data_).Release(mq.shm()); + } + } - int n = mq.TrySendAll(*(MQId *) remote.data(), MsgIter(pos), MsgIter(msg_list.end())); - for (int i = 0; i < n; ++i) { - auto &msg = pos->data().msg_; - if (msg.IsCounted()) { + auto SendData = [&](Data &d) { + bool r = false; + if (d.index() == 0) { + auto &msg = boost::variant2::get<0>(pos->data().data_); + r = mq.TrySend(*(MQId *) remote.data(), msg); + if (r && msg.IsCounted()) { msg.Release(mq.shm()); } - ++pos; + } else { + auto &content = boost::variant2::get<1>(pos->data().data_); + MsgI msg; + if (msg.Make(mq.shm(), content)) { + r = mq.TrySend(*(MQId *) remote.data(), msg); + if (!r || msg.IsCounted()) { + msg.Release(mq.shm()); + } + } } - - msg_list.erase(msg_list.begin(), pos); + return r; }; + while (pos != arr.end() && SendData(pos->data().data_)) { + ++pos; + } + + int nprocessed = std::distance(arr.begin(), pos); + arr.erase(arr.begin(), pos); + return nprocessed; +} + +int SendQ::DoSend1Remote(bhome_shm::ShmMsgQueue &mq, const Remote &remote, ArrayList &al) +{ + int nsend = 0; + auto AllSent = [&](Array &arr) { + nsend += DoSend1Remote(mq, remote, arr); + return arr.empty(); + }; + for (auto it = al.begin(); it != al.end() && AllSent(*it); it = al.erase(it)) {} + return nsend; +} + +bool SendQ::TrySend(bhome_shm::ShmMsgQueue &mq) +{ + std::unique_lock<std::mutex> lock(mutex_out_); + size_t nsend = 0; if (!out_.empty()) { auto rec = out_.begin(); do { - SendOneRemote(rec->first, rec->second); + nsend += DoSend1Remote(mq, rec->first, rec->second); if (rec->second.empty()) { rec = out_.erase(rec); } else { @@ -64,5 +93,21 @@ } } while (rec != out_.end()); } + + auto Collect = [&]() { + std::unique_lock<std::mutex> lock(mutex_in_); + if (out_.empty()) { + out_.swap(in_); + } else if (nsend == 0) { // remote blocked + for (auto &kv : in_) { + auto &to = out_[kv.first]; + to.splice(to.end(), kv.second); + } + in_.clear(); + } + }; + + Collect(); + return !out_.empty(); } \ No newline at end of file -- Gitblit v1.8.0