Use SPSCQueue::PopWait in Scripting

This commit is contained in:
B3n30 2018-10-05 11:25:23 +02:00
parent 9b49a79a72
commit 7efe60ed23
4 changed files with 5 additions and 15 deletions

View file

@ -106,10 +106,8 @@ void RPCServer::HandleRequestsLoop() {
LOG_INFO(RPC_Server, "Request handler started."); LOG_INFO(RPC_Server, "Request handler started.");
while (true) { while (request_queue.PopWait(request_packet)) {
std::unique_lock<std::mutex> lock(request_queue_mutex); if (!request_packet) {
request_queue_cv.wait(lock, [&] { return !running || request_queue.Pop(request_packet); });
if (!running) {
break; break;
} }
HandleSingleRequest(std::move(request_packet)); HandleSingleRequest(std::move(request_packet));
@ -117,23 +115,18 @@ void RPCServer::HandleRequestsLoop() {
} }
void RPCServer::QueueRequest(std::unique_ptr<RPC::Packet> request) { void RPCServer::QueueRequest(std::unique_ptr<RPC::Packet> request) {
std::unique_lock<std::mutex> lock(request_queue_mutex);
request_queue.Push(std::move(request)); request_queue.Push(std::move(request));
request_queue_cv.notify_one();
} }
void RPCServer::Start() { void RPCServer::Start() {
running = true;
const auto threadFunction = [this]() { HandleRequestsLoop(); }; const auto threadFunction = [this]() { HandleRequestsLoop(); };
request_handler_thread = std::thread(threadFunction); request_handler_thread = std::thread(threadFunction);
server.Start(); server.Start();
} }
void RPCServer::Stop() { void RPCServer::Stop() {
running = false;
request_queue_cv.notify_one();
request_handler_thread.join();
server.Stop(); server.Stop();
request_handler_thread.join();
} }
}; // namespace RPC }; // namespace RPC

View file

@ -31,10 +31,7 @@ private:
Server server; Server server;
Common::SPSCQueue<std::unique_ptr<Packet>> request_queue; Common::SPSCQueue<std::unique_ptr<Packet>> request_queue;
bool running = false;
std::thread request_handler_thread; std::thread request_handler_thread;
std::mutex request_queue_mutex;
std::condition_variable request_queue_cv;
}; };
} // namespace RPC } // namespace RPC

View file

@ -1,6 +1,5 @@
#include <functional> #include <functional>
#include "common/threadsafe_queue.h"
#include "core/core.h" #include "core/core.h"
#include "core/rpc/rpc_server.h" #include "core/rpc/rpc_server.h"
#include "core/rpc/server.h" #include "core/rpc/server.h"

View file

@ -52,7 +52,8 @@ void ZMQServer::WorkerLoop() {
LOG_WARNING(RPC_Server, "Failed to receive data on ZeroMQ socket"); LOG_WARNING(RPC_Server, "Failed to receive data on ZeroMQ socket");
} }
} }
std::unique_ptr<Packet> end_packet = nullptr;
new_request_callback(std::move(end_packet));
// Destroying the socket must be done by this thread. // Destroying the socket must be done by this thread.
zmq_socket.reset(); zmq_socket.reset();
} }