Skip to content

Commit 7e87033

Browse files
committed
httpserver: replace boost threads with std
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.
1 parent d3773ca commit 7e87033

File tree

2 files changed

+22
-27
lines changed

2 files changed

+22
-27
lines changed

src/httpserver.cpp

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@
3535
#endif
3636
#endif
3737

38-
#include <boost/algorithm/string/case_conv.hpp> // for to_lower()
39-
#include <boost/foreach.hpp>
40-
4138
/** Maximum size of http request (request line + headers) */
4239
static const size_t MAX_HEADERS_SIZE = 8192;
4340

@@ -69,8 +66,8 @@ class WorkQueue
6966
{
7067
private:
7168
/** Mutex protects entire object */
72-
CWaitableCriticalSection cs;
73-
CConditionVariable cond;
69+
std::mutex cs;
70+
std::condition_variable cond;
7471
std::deque<std::unique_ptr<WorkItem>> queue;
7572
bool running;
7673
size_t maxDepth;
@@ -83,12 +80,12 @@ class WorkQueue
8380
WorkQueue &wq;
8481
ThreadCounter(WorkQueue &w): wq(w)
8582
{
86-
boost::lock_guard<boost::mutex> lock(wq.cs);
83+
std::lock_guard<std::mutex> lock(wq.cs);
8784
wq.numThreads += 1;
8885
}
8986
~ThreadCounter()
9087
{
91-
boost::lock_guard<boost::mutex> lock(wq.cs);
88+
std::lock_guard<std::mutex> lock(wq.cs);
9289
wq.numThreads -= 1;
9390
wq.cond.notify_all();
9491
}
@@ -109,7 +106,7 @@ class WorkQueue
109106
/** Enqueue a work item */
110107
bool Enqueue(WorkItem* item)
111108
{
112-
boost::unique_lock<boost::mutex> lock(cs);
109+
std::unique_lock<std::mutex> lock(cs);
113110
if (queue.size() >= maxDepth) {
114111
return false;
115112
}
@@ -124,7 +121,7 @@ class WorkQueue
124121
while (running) {
125122
std::unique_ptr<WorkItem> i;
126123
{
127-
boost::unique_lock<boost::mutex> lock(cs);
124+
std::unique_lock<std::mutex> lock(cs);
128125
while (running && queue.empty())
129126
cond.wait(lock);
130127
if (!running)
@@ -138,22 +135,22 @@ class WorkQueue
138135
/** Interrupt and exit loops */
139136
void Interrupt()
140137
{
141-
boost::unique_lock<boost::mutex> lock(cs);
138+
std::unique_lock<std::mutex> lock(cs);
142139
running = false;
143140
cond.notify_all();
144141
}
145142
/** Wait for worker threads to exit */
146143
void WaitExit()
147144
{
148-
boost::unique_lock<boost::mutex> lock(cs);
145+
std::unique_lock<std::mutex> lock(cs);
149146
while (numThreads > 0)
150147
cond.wait(lock);
151148
}
152149

153150
/** Return current depth of queue */
154151
size_t Depth()
155152
{
156-
boost::unique_lock<boost::mutex> lock(cs);
153+
std::unique_lock<std::mutex> lock(cs);
157154
return queue.size();
158155
}
159156
};
@@ -190,7 +187,7 @@ static bool ClientAllowed(const CNetAddr& netaddr)
190187
{
191188
if (!netaddr.IsValid())
192189
return false;
193-
BOOST_FOREACH (const CSubNet& subnet, rpc_allow_subnets)
190+
for(const CSubNet& subnet : rpc_allow_subnets)
194191
if (subnet.Match(netaddr))
195192
return true;
196193
return false;
@@ -204,7 +201,7 @@ static bool InitHTTPAllowList()
204201
rpc_allow_subnets.push_back(CSubNet("::1")); // always allow IPv6 localhost
205202
if (mapMultiArgs.count("-rpcallowip")) {
206203
const std::vector<std::string>& vAllow = mapMultiArgs["-rpcallowip"];
207-
BOOST_FOREACH (std::string strAllow, vAllow) {
204+
for (std::string strAllow : vAllow) {
208205
CSubNet subnet(strAllow);
209206
if (!subnet.IsValid()) {
210207
uiInterface.ThreadSafeMessageBox(
@@ -216,7 +213,7 @@ static bool InitHTTPAllowList()
216213
}
217214
}
218215
std::string strAllowed;
219-
BOOST_FOREACH (const CSubNet& subnet, rpc_allow_subnets)
216+
for (const CSubNet& subnet : rpc_allow_subnets)
220217
strAllowed += subnet.ToString() + " ";
221218
LogPrint("http", "Allowing HTTP connections from: %s\n", strAllowed);
222219
return true;
@@ -439,7 +436,7 @@ bool InitHTTPServer()
439436
return true;
440437
}
441438

442-
boost::thread threadHTTP;
439+
std::thread threadHTTP;
443440
std::future<bool> threadResult;
444441

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

454451
for (int i = 0; i < rpcThreads; i++) {
455-
boost::thread rpc_worker(HTTPWorkQueueRun, workQueue);
452+
std::thread rpc_worker(HTTPWorkQueueRun, workQueue);
456453
rpc_worker.detach();
457454
}
458455
return true;
@@ -463,7 +460,7 @@ void InterruptHTTPServer()
463460
LogPrint("http", "Interrupting HTTP server\n");
464461
if (eventHTTP) {
465462
// Unlisten sockets
466-
BOOST_FOREACH (evhttp_bound_socket *socket, boundSockets) {
463+
for (evhttp_bound_socket *socket : boundSockets) {
467464
evhttp_del_accept_socket(eventHTTP, socket);
468465
}
469466
// Reject requests on current connections
@@ -520,7 +517,7 @@ static void httpevent_callback_fn(evutil_socket_t, short, void* data)
520517
delete self;
521518
}
522519

523-
HTTPEvent::HTTPEvent(struct event_base* base, bool deleteWhenTriggered, const boost::function<void(void)>& handler):
520+
HTTPEvent::HTTPEvent(struct event_base* base, bool deleteWhenTriggered, const std::function<void(void)>& handler):
524521
deleteWhenTriggered(deleteWhenTriggered), handler(handler)
525522
{
526523
ev = event_new(base, -1, 0, httpevent_callback_fn, this);
@@ -602,7 +599,7 @@ void HTTPRequest::WriteReply(int nStatus, const std::string& strReply)
602599
assert(evb);
603600
evbuffer_add(evb, strReply.data(), strReply.size());
604601
HTTPEvent* ev = new HTTPEvent(eventBase, true,
605-
boost::bind(evhttp_send_reply, req, nStatus, (const char*)NULL, (struct evbuffer *)NULL));
602+
std::bind(evhttp_send_reply, req, nStatus, (const char*)NULL, (struct evbuffer *)NULL));
606603
ev->trigger(0);
607604
replySent = true;
608605
req = 0; // transferred back to main thread

src/httpserver.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77

88
#include <string>
99
#include <stdint.h>
10-
#include <boost/thread.hpp>
11-
#include <boost/scoped_ptr.hpp>
12-
#include <boost/function.hpp>
10+
#include <functional>
1311

1412
static const int DEFAULT_HTTP_THREADS=4;
1513
static const int DEFAULT_HTTP_WORKQUEUE=16;
@@ -35,7 +33,7 @@ void InterruptHTTPServer();
3533
void StopHTTPServer();
3634

3735
/** Handler for requests to a certain HTTP path */
38-
typedef boost::function<void(HTTPRequest* req, const std::string &)> HTTPRequestHandler;
36+
typedef std::function<void(HTTPRequest* req, const std::string &)> HTTPRequestHandler;
3937
/** Register handler for prefix.
4038
* If multiple handlers match a prefix, the first-registered one will
4139
* be invoked.
@@ -132,7 +130,7 @@ class HTTPEvent
132130
* deleteWhenTriggered deletes this event object after the event is triggered (and the handler called)
133131
* handler is the handler to call when the event is triggered.
134132
*/
135-
HTTPEvent(struct event_base* base, bool deleteWhenTriggered, const boost::function<void(void)>& handler);
133+
HTTPEvent(struct event_base* base, bool deleteWhenTriggered, const std::function<void(void)>& handler);
136134
~HTTPEvent();
137135

138136
/** Trigger the event. If tv is 0, trigger it immediately. Otherwise trigger it after
@@ -141,7 +139,7 @@ class HTTPEvent
141139
void trigger(struct timeval* tv);
142140

143141
bool deleteWhenTriggered;
144-
boost::function<void(void)> handler;
142+
std::function<void(void)> handler;
145143
private:
146144
struct event* ev;
147145
};

0 commit comments

Comments
 (0)