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.");
while (true) {
std::unique_lock<std::mutex> lock(request_queue_mutex);
request_queue_cv.wait(lock, [&] { return !running || request_queue.Pop(request_packet); });
if (!running) {
while (request_queue.PopWait(request_packet)) {
if (!request_packet) {
break;
}
HandleSingleRequest(std::move(request_packet));
@ -117,23 +115,18 @@ void RPCServer::HandleRequestsLoop() {
}
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_cv.notify_one();
}
void RPCServer::Start() {
running = true;
const auto threadFunction = [this]() { HandleRequestsLoop(); };
request_handler_thread = std::thread(threadFunction);
server.Start();
}
void RPCServer::Stop() {
running = false;
request_queue_cv.notify_one();
request_handler_thread.join();
server.Stop();
request_handler_thread.join();
}
}; // namespace RPC

View File

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

View File

@ -1,6 +1,5 @@
#include <functional>
#include "common/threadsafe_queue.h"
#include "core/core.h"
#include "core/rpc/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");
}
}
std::unique_ptr<Packet> end_packet = nullptr;
new_request_callback(std::move(end_packet));
// Destroying the socket must be done by this thread.
zmq_socket.reset();
}