@@ -31,17 +31,16 @@ ExecutorService::~ExecutorService() { close(0); }
3131void ExecutorService::start () {
3232 auto self = shared_from_this ();
3333 std::thread t{[this , self] {
34- LOG_DEBUG (" Run io_service in a single thread" );
35- ASIO_ERROR ec;
34+ LOG_DEBUG (" Run io_context in a single thread" );
3635 while (!closed_) {
37- io_service_ .restart ();
38- IOService::work work{ getIOService ()} ;
39- io_service_. run (ec);
40- }
41- if (ec) {
42- LOG_ERROR ( " Failed to run io_service: " << ec. message ());
43- } else {
44- LOG_DEBUG ( " Event loop of ExecutorService exits successfully " );
36+ io_context_ .restart ();
37+ auto work_guard = ASIO::make_work_guard (io_context_) ;
38+ try {
39+ io_context_. run ();
40+ LOG_DEBUG ( " Event loop of ExecutorService exits successfully " );
41+ } catch ( const ASIO_ERROR &e) {
42+ LOG_ERROR ( " Failed to run io_context: " << e. message ());
43+ }
4544 }
4645 {
4746 std::lock_guard<std::mutex> lock{mutex_};
@@ -63,12 +62,12 @@ ExecutorServicePtr ExecutorService::create() {
6362}
6463
6564/*
66- * factory method of ASIO::ip::tcp::socket associated with io_service_ instance
65+ * factory method of ASIO::ip::tcp::socket associated with io_context_ instance
6766 * @ returns shared_ptr to this socket
6867 */
6968SocketPtr ExecutorService::createSocket () {
7069 try {
71- return SocketPtr (new ASIO::ip::tcp::socket (io_service_ ));
70+ return SocketPtr (new ASIO::ip::tcp::socket (io_context_ ));
7271 } catch (const ASIO_SYSTEM_ERROR &e) {
7372 restart ();
7473 auto error = std::string (" Failed to create socket: " ) + e.what ();
@@ -82,12 +81,12 @@ TlsSocketPtr ExecutorService::createTlsSocket(SocketPtr &socket, ASIO::ssl::cont
8281}
8382
8483/*
85- * factory method of Resolver object associated with io_service_ instance
84+ * factory method of Resolver object associated with io_context_ instance
8685 * @returns shraed_ptr to resolver object
8786 */
8887TcpResolverPtr ExecutorService::createTcpResolver () {
8988 try {
90- return TcpResolverPtr (new ASIO::ip::tcp::resolver (io_service_ ));
89+ return TcpResolverPtr (new ASIO::ip::tcp::resolver (io_context_ ));
9190 } catch (const ASIO_SYSTEM_ERROR &e) {
9291 restart ();
9392 auto error = std::string (" Failed to create resolver: " ) + e.what ();
@@ -97,36 +96,36 @@ TcpResolverPtr ExecutorService::createTcpResolver() {
9796
9897DeadlineTimerPtr ExecutorService::createDeadlineTimer () {
9998 try {
100- return DeadlineTimerPtr (new ASIO::steady_timer (io_service_ ));
99+ return DeadlineTimerPtr (new ASIO::steady_timer (io_context_ ));
101100 } catch (const ASIO_SYSTEM_ERROR &e) {
102101 restart ();
103102 auto error = std::string (" Failed to create steady_timer: " ) + e.what ();
104103 throw std::runtime_error (error);
105104 }
106105}
107106
108- void ExecutorService::restart () { io_service_ .stop (); }
107+ void ExecutorService::restart () { io_context_ .stop (); }
109108
110109void ExecutorService::close (long timeoutMs) {
111110 bool expectedState = false ;
112111 if (!closed_.compare_exchange_strong (expectedState, true )) {
113112 return ;
114113 }
115114 if (timeoutMs == 0 ) { // non-blocking
116- io_service_ .stop ();
115+ io_context_ .stop ();
117116 return ;
118117 }
119118
120119 std::unique_lock<std::mutex> lock{mutex_};
121- io_service_ .stop ();
120+ io_context_ .stop ();
122121 if (timeoutMs > 0 ) {
123122 cond_.wait_for (lock, std::chrono::milliseconds (timeoutMs), [this ] { return ioServiceDone_; });
124123 } else { // < 0
125124 cond_.wait (lock, [this ] { return ioServiceDone_; });
126125 }
127126}
128127
129- void ExecutorService::postWork (std::function<void (void )> task) { io_service_. post (task); }
128+ void ExecutorService::postWork (std::function<void (void )> task) { ASIO:: post (io_context_, std::move ( task) ); }
130129
131130// ///////////////////
132131
0 commit comments