|
19 | 19 | #include <sys/types.h> |
20 | 20 | #include <sys/stat.h> |
21 | 21 | #include <signal.h> |
| 22 | +#include <future> |
22 | 23 |
|
23 | 24 | #include <event2/event.h> |
24 | 25 | #include <event2/http.h> |
@@ -302,13 +303,14 @@ static void http_reject_request_cb(struct evhttp_request* req, void*) |
302 | 303 | } |
303 | 304 |
|
304 | 305 | /** 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) |
306 | 307 | { |
307 | 308 | RenameThread("bitcoin-http"); |
308 | 309 | LogPrint("http", "Entering http event loop\n"); |
309 | 310 | event_base_dispatch(base); |
310 | 311 | // Event loop will be interrupted by InterruptHTTPServer() |
311 | 312 | LogPrint("http", "Exited http event loop\n"); |
| 313 | + return event_base_got_break(base) == 0; |
312 | 314 | } |
313 | 315 |
|
314 | 316 | /** Bind HTTP server to specified addresses */ |
@@ -438,13 +440,16 @@ bool InitHTTPServer() |
438 | 440 | } |
439 | 441 |
|
440 | 442 | boost::thread threadHTTP; |
| 443 | +std::future<bool> threadResult; |
441 | 444 |
|
442 | 445 | bool StartHTTPServer() |
443 | 446 | { |
444 | 447 | LogPrint("http", "Starting HTTP server\n"); |
445 | 448 | int rpcThreads = std::max((long)GetArg("-rpcthreads", DEFAULT_HTTP_THREADS), 1L); |
446 | 449 | 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)); |
448 | 453 |
|
449 | 454 | for (int i = 0; i < rpcThreads; i++) |
450 | 455 | boost::thread(boost::bind(&HTTPWorkQueueRun, workQueue)); |
@@ -482,15 +487,11 @@ void StopHTTPServer() |
482 | 487 | // master that appears to be solved, so in the future that solution |
483 | 488 | // could be used again (if desirable). |
484 | 489 | // (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) { |
490 | 491 | LogPrintf("HTTP event loop did not exit within allotted time, sending loopbreak\n"); |
491 | 492 | event_base_loopbreak(eventBase); |
492 | | - threadHTTP.join(); |
493 | 493 | } |
| 494 | + threadHTTP.join(); |
494 | 495 | } |
495 | 496 | if (eventHTTP) { |
496 | 497 | evhttp_free(eventHTTP); |
|
0 commit comments