From e0de466e5758ce8ef6452e2a25614c2cb2844d91 Mon Sep 17 00:00:00 2001 From: Determinant Date: Mon, 26 Aug 2019 17:42:40 -0400 Subject: fix data race --- examples/hotstuff_app.cpp | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/examples/hotstuff_app.cpp b/examples/hotstuff_app.cpp index 8bc092d..51baf26 100644 --- a/examples/hotstuff_app.cpp +++ b/examples/hotstuff_app.cpp @@ -83,7 +83,7 @@ class HotStuffApp: public HotStuff { std::unordered_map unconfirmed; using conn_t = ClientNetwork::conn_t; - using resp_queue_t = salticidae::MPSCQueueEventDriven; + using resp_queue_t = salticidae::MPSCQueueEventDriven>; /* for the dedicated thread sending responses to the clients */ std::thread req_thread; @@ -110,7 +110,6 @@ class HotStuffApp: public HotStuff { #ifndef HOTSTUFF_ENABLE_BENCHMARK HOTSTUFF_LOG_INFO("replicated %s", std::string(fin).c_str()); #endif - resp_queue.enqueue(fin); } #ifdef HOTSTUFF_MSG_STAT @@ -312,14 +311,13 @@ HotStuffApp::HotStuffApp(uint32_t blk_size, resp_tcall = new salticidae::ThreadCall(resp_ec); req_tcall = new salticidae::ThreadCall(req_ec); resp_queue.reg_handler(resp_ec, [this](resp_queue_t &q) { - Finality fin; - while (q.try_dequeue(fin)) + std::pair p; + while (q.try_dequeue(p)) { - auto it = unconfirmed.find(fin.cmd_hash); - if (it != unconfirmed.end()) - { - it->second.resolve(fin); - unconfirmed.erase(it); + try { + cn.send_msg(MsgRespCmd(std::move(p.first)), p.second); + } catch (std::exception &err) { + HOTSTUFF_LOG_WARN("unable to send to the client: %s", err.what()); } } return false; @@ -337,21 +335,7 @@ void HotStuffApp::client_request_cmd_handler(MsgReqCmd &&msg, const conn_t &conn const auto &cmd_hash = cmd->get_hash(); HOTSTUFF_LOG_DEBUG("processing %s", std::string(*cmd).c_str()); exec_command(cmd_hash, [this, addr](Finality fin) { - resp_queue.enqueue(fin); - }); - /* the following function is executed on the dedicated thread for confirming commands */ - resp_tcall->async_call([this, addr, cmd_hash](salticidae::ThreadCall::Handle &) { - auto it = unconfirmed.find(cmd_hash); - if (it == unconfirmed.end()) - it = unconfirmed.insert( - std::make_pair(cmd_hash, promise_t([](promise_t &){}))).first; - it->second.then([this, addr](const Finality &fin) { - try { - cn.send_msg(MsgRespCmd(std::move(fin)), addr); - } catch (std::exception &err) { - HOTSTUFF_LOG_WARN("unable to send to the client: %s", err.what()); - } - }); + resp_queue.enqueue(std::make_pair(fin, addr)); }); } -- cgit v1.2.3