Skip to content

Commit 755aa05

Browse files
committed
httpserver: use a future rather than relying on boost's try_join_for
1 parent 133c727 commit 755aa05

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/httpserver.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <sys/types.h>
2020
#include <sys/stat.h>
2121
#include <signal.h>
22+
#include <future>
2223

2324
#include <event2/event.h>
2425
#include <event2/http.h>
@@ -302,13 +303,14 @@ static void http_reject_request_cb(struct evhttp_request* req, void*)
302303
}
303304

304305
/** Event dispatcher thread */
305-
static void ThreadHTTP(struct event_base* base, struct evhttp* http)
306+
static bool ThreadHTTP(struct event_base* base, struct evhttp* http)
306307
{
307308
RenameThread("bitcoin-http");
308309
LogPrint("http", "Entering http event loop\n");
309310
event_base_dispatch(base);
310311
// Event loop will be interrupted by InterruptHTTPServer()
311312
LogPrint("http", "Exited http event loop\n");
313+
return event_base_got_break(base) == 0;
312314
}
313315

314316
/** Bind HTTP server to specified addresses */
@@ -438,13 +440,16 @@ bool InitHTTPServer()
438440
}
439441

440442
boost::thread threadHTTP;
443+
std::future<bool> threadResult;
441444

442445
bool StartHTTPServer()
443446
{
444447
LogPrint("http", "Starting HTTP server\n");
445448
int rpcThreads = std::max((long)GetArg("-rpcthreads", DEFAULT_HTTP_THREADS), 1L);
446449
LogPrintf("HTTP: starting %d worker threads\n", rpcThreads);
447-
threadHTTP = boost::thread(boost::bind(&ThreadHTTP, eventBase, eventHTTP));
450+
std::packaged_task<bool(event_base*, evhttp*)> task(ThreadHTTP);
451+
threadResult = task.get_future();
452+
threadHTTP = boost::thread(std::bind(std::move(task), eventBase, eventHTTP));
448453

449454
for (int i = 0; i < rpcThreads; i++)
450455
boost::thread(boost::bind(&HTTPWorkQueueRun, workQueue));
@@ -482,15 +487,11 @@ void StopHTTPServer()
482487
// master that appears to be solved, so in the future that solution
483488
// could be used again (if desirable).
484489
// (see discussion in https://github.com/bitcoin/bitcoin/pull/6990)
485-
#if BOOST_VERSION >= 105000
486-
if (!threadHTTP.try_join_for(boost::chrono::milliseconds(2000))) {
487-
#else
488-
if (!threadHTTP.timed_join(boost::posix_time::milliseconds(2000))) {
489-
#endif
490+
if (threadResult.valid() && threadResult.wait_for(std::chrono::milliseconds(2000)) == std::future_status::timeout) {
490491
LogPrintf("HTTP event loop did not exit within allotted time, sending loopbreak\n");
491492
event_base_loopbreak(eventBase);
492-
threadHTTP.join();
493493
}
494+
threadHTTP.join();
494495
}
495496
if (eventHTTP) {
496497
evhttp_free(eventHTTP);

0 commit comments

Comments
 (0)