Skip to content

Commit

Permalink
httpserver: replace boost threads with std
Browse files Browse the repository at this point in the history
along with mutex/condvar/bind/etc.

httpserver handles its own interruption, so there's no reason not to use std
threading.

While we're at it, may as well kill the BOOST_FOREACH's as well.
  • Loading branch information
theuni authored and str4d committed Sep 30, 2020
1 parent 97194f2 commit 860e6e6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
37 changes: 17 additions & 20 deletions src/httpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
#endif
#endif

#include <boost/algorithm/string/case_conv.hpp> // for to_lower()
#include <boost/foreach.hpp>

/** Maximum size of http request (request line + headers) */
static const size_t MAX_HEADERS_SIZE = 8192;

Expand Down Expand Up @@ -69,8 +66,8 @@ class WorkQueue
{
private:
/** Mutex protects entire object */
CWaitableCriticalSection cs;
CConditionVariable cond;
std::mutex cs;
std::condition_variable cond;
std::deque<std::unique_ptr<WorkItem>> queue;
bool running;
size_t maxDepth;
Expand All @@ -83,12 +80,12 @@ class WorkQueue
WorkQueue &wq;
ThreadCounter(WorkQueue &w): wq(w)
{
boost::lock_guard<boost::mutex> lock(wq.cs);
std::lock_guard<std::mutex> lock(wq.cs);
wq.numThreads += 1;
}
~ThreadCounter()
{
boost::lock_guard<boost::mutex> lock(wq.cs);
std::lock_guard<std::mutex> lock(wq.cs);
wq.numThreads -= 1;
wq.cond.notify_all();
}
Expand All @@ -109,7 +106,7 @@ class WorkQueue
/** Enqueue a work item */
bool Enqueue(WorkItem* item)
{
boost::unique_lock<boost::mutex> lock(cs);
std::unique_lock<std::mutex> lock(cs);
if (queue.size() >= maxDepth) {
return false;
}
Expand All @@ -124,7 +121,7 @@ class WorkQueue
while (running) {
std::unique_ptr<WorkItem> i;
{
boost::unique_lock<boost::mutex> lock(cs);
std::unique_lock<std::mutex> lock(cs);
while (running && queue.empty())
cond.wait(lock);
if (!running)
Expand All @@ -138,22 +135,22 @@ class WorkQueue
/** Interrupt and exit loops */
void Interrupt()
{
boost::unique_lock<boost::mutex> lock(cs);
std::unique_lock<std::mutex> lock(cs);
running = false;
cond.notify_all();
}
/** Wait for worker threads to exit */
void WaitExit()
{
boost::unique_lock<boost::mutex> lock(cs);
std::unique_lock<std::mutex> lock(cs);
while (numThreads > 0)
cond.wait(lock);
}

/** Return current depth of queue */
size_t Depth()
{
boost::unique_lock<boost::mutex> lock(cs);
std::unique_lock<std::mutex> lock(cs);
return queue.size();
}
};
Expand Down Expand Up @@ -190,7 +187,7 @@ static bool ClientAllowed(const CNetAddr& netaddr)
{
if (!netaddr.IsValid())
return false;
BOOST_FOREACH (const CSubNet& subnet, rpc_allow_subnets)
for(const CSubNet& subnet : rpc_allow_subnets)
if (subnet.Match(netaddr))
return true;
return false;
Expand All @@ -204,7 +201,7 @@ static bool InitHTTPAllowList()
rpc_allow_subnets.push_back(CSubNet("::1")); // always allow IPv6 localhost
if (mapMultiArgs.count("-rpcallowip")) {
const std::vector<std::string>& vAllow = mapMultiArgs["-rpcallowip"];
BOOST_FOREACH (std::string strAllow, vAllow) {
for (std::string strAllow : vAllow) {
CSubNet subnet(strAllow);
if (!subnet.IsValid()) {
uiInterface.ThreadSafeMessageBox(
Expand All @@ -216,7 +213,7 @@ static bool InitHTTPAllowList()
}
}
std::string strAllowed;
BOOST_FOREACH (const CSubNet& subnet, rpc_allow_subnets)
for (const CSubNet& subnet : rpc_allow_subnets)
strAllowed += subnet.ToString() + " ";
LogPrint("http", "Allowing HTTP connections from: %s\n", strAllowed);
return true;
Expand Down Expand Up @@ -439,7 +436,7 @@ bool InitHTTPServer()
return true;
}

boost::thread threadHTTP;
std::thread threadHTTP;
std::future<bool> threadResult;

bool StartHTTPServer()
Expand All @@ -449,10 +446,10 @@ bool StartHTTPServer()
LogPrintf("HTTP: starting %d worker threads\n", rpcThreads);
std::packaged_task<bool(event_base*, evhttp*)> task(ThreadHTTP);
threadResult = task.get_future();
threadHTTP = boost::thread(std::bind(std::move(task), eventBase, eventHTTP));
threadHTTP = std::thread(std::move(task), eventBase, eventHTTP);

for (int i = 0; i < rpcThreads; i++) {
boost::thread rpc_worker(HTTPWorkQueueRun, workQueue);
std::thread rpc_worker(HTTPWorkQueueRun, workQueue);
rpc_worker.detach();
}
return true;
Expand All @@ -463,7 +460,7 @@ void InterruptHTTPServer()
LogPrint("http", "Interrupting HTTP server\n");
if (eventHTTP) {
// Unlisten sockets
BOOST_FOREACH (evhttp_bound_socket *socket, boundSockets) {
for (evhttp_bound_socket *socket : boundSockets) {
evhttp_del_accept_socket(eventHTTP, socket);
}
// Reject requests on current connections
Expand Down Expand Up @@ -604,7 +601,7 @@ void HTTPRequest::WriteReply(int nStatus, const std::string& strReply)
assert(evb);
evbuffer_add(evb, strReply.data(), strReply.size());
HTTPEvent* ev = new HTTPEvent(eventBase, true,
boost::bind(evhttp_send_reply, req, nStatus, (const char*)NULL, (struct evbuffer *)NULL));
std::bind(evhttp_send_reply, req, nStatus, (const char*)NULL, (struct evbuffer *)NULL));
ev->trigger(0);
replySent = true;
req = 0; // transferred back to main thread
Expand Down
3 changes: 1 addition & 2 deletions src/httpserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

#include <string>
#include <stdint.h>
#include <boost/thread.hpp>
#include <boost/scoped_ptr.hpp>
#include <functional>

static const int DEFAULT_HTTP_THREADS=4;
static const int DEFAULT_HTTP_WORKQUEUE=16;
Expand Down

0 comments on commit 860e6e6

Please sign in to comment.