Skip to content

Commit a164e06

Browse files
committed
remove boost mutex and condition_variable in TcpTransport and ReponseFunture.
1 parent dc602a7 commit a164e06

File tree

4 files changed

+29
-25
lines changed

4 files changed

+29
-25
lines changed

src/transport/ResponseFuture.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
* limitations under the License.
1616
*/
1717
#include "ResponseFuture.h"
18+
19+
#include <chrono>
20+
1821
#include "Logging.h"
1922
#include "TcpRemotingClient.h"
2023

@@ -52,12 +55,12 @@ void ResponseFuture::releaseThreadCondition() {
5255
}
5356

5457
RemotingCommand *ResponseFuture::waitResponse(int timeoutMillis) {
55-
boost::unique_lock<boost::mutex> eventLock(m_defaultEventLock);
58+
std::unique_lock<std::mutex> eventLock(m_defaultEventLock);
5659
if (!m_haveResponse) {
5760
if (timeoutMillis <= 0) {
5861
timeoutMillis = m_timeout;
5962
}
60-
if (!m_defaultEvent.timed_wait(eventLock, boost::posix_time::milliseconds(timeoutMillis))) {
63+
if (m_defaultEvent.wait_for(eventLock, std::chrono::milliseconds(timeoutMillis)) == std::cv_status::timeout) {
6164
LOG_WARN("waitResponse of code:%d with opaque:%d timeout", m_requestCode, m_opaque);
6265
m_haveResponse = true;
6366
}
@@ -66,7 +69,7 @@ RemotingCommand *ResponseFuture::waitResponse(int timeoutMillis) {
6669
}
6770

6871
bool ResponseFuture::setResponse(RemotingCommand *pResponseCommand) {
69-
boost::unique_lock<boost::mutex> eventLock(m_defaultEventLock);
72+
std::unique_lock<std::mutex> eventLock(m_defaultEventLock);
7073

7174
if (m_haveResponse) {
7275
return false;

src/transport/ResponseFuture.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
#ifndef __RESPONSEFUTURE_H__
1818
#define __RESPONSEFUTURE_H__
1919

20-
#include <boost/atomic.hpp>
21-
#include <boost/thread/condition_variable.hpp>
20+
#include <atomic>
21+
#include <condition_variable>
2222

2323
#include "AsyncCallbackWrap.h"
2424
#include "RemotingCommand.h"
@@ -77,11 +77,11 @@ class ResponseFuture {
7777
AsyncCallbackWrap *m_pCallbackWrap;
7878

7979
AsyncCallbackStatus m_asyncCallbackStatus;
80-
boost::mutex m_asyncCallbackLock;
80+
std::mutex m_asyncCallbackLock;
8181

8282
bool m_haveResponse;
83-
boost::mutex m_defaultEventLock;
84-
boost::condition_variable_any m_defaultEvent;
83+
std::mutex m_defaultEventLock;
84+
std::condition_variable m_defaultEvent;
8585

8686
int64 m_beginTimestamp;
8787
bool m_sendRequestOK;

src/transport/TcpTransport.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
#include "TcpTransport.h"
1818

19+
#include <chrono>
20+
1921
#ifndef WIN32
2022
#include <arpa/inet.h> // for sockaddr_in and inet_ntoa...
2123
#include <netinet/tcp.h>
@@ -65,9 +67,9 @@ TcpConnectStatus TcpTransport::getTcpConnectStatus() {
6567
}
6668

6769
TcpConnectStatus TcpTransport::waitTcpConnectEvent(int timeoutMillis) {
68-
boost::unique_lock<boost::mutex> eventLock(m_connectEventLock);
70+
std::unique_lock<std::mutex> eventLock(m_connectEventLock);
6971
if (m_tcpConnectStatus == TCP_CONNECT_STATUS_WAIT) {
70-
if (!m_connectEvent.timed_wait(eventLock, boost::posix_time::milliseconds(timeoutMillis))) {
72+
if (m_connectEvent.wait_for(eventLock, std::chrono::milliseconds(timeoutMillis)) == std::cv_status::timeout) {
7173
LOG_INFO("connect timeout");
7274
}
7375
}
@@ -76,16 +78,16 @@ TcpConnectStatus TcpTransport::waitTcpConnectEvent(int timeoutMillis) {
7678

7779
// internal method
7880
void TcpTransport::setTcpConnectEvent(TcpConnectStatus connectStatus) {
79-
TcpConnectStatus baseStatus = m_tcpConnectStatus.exchange(connectStatus, boost::memory_order_relaxed);
81+
TcpConnectStatus baseStatus = m_tcpConnectStatus.exchange(connectStatus, std::memory_order_relaxed);
8082
if (baseStatus == TCP_CONNECT_STATUS_WAIT) {
81-
boost::unique_lock<boost::mutex> eventLock(m_connectEventLock);
83+
std::unique_lock<std::mutex> eventLock(m_connectEventLock);
8284
m_connectEvent.notify_all();
8385
}
8486
}
8587

8688
void TcpTransport::disconnect(const string &addr) {
8789
// disconnect is idempotent.
88-
boost::lock_guard<boost::mutex> lock(m_eventLock);
90+
std::lock_guard<std::mutex> lock(m_eventLock);
8991
if (getTcpConnectStatus() != TCP_CONNECT_STATUS_INIT) {
9092
LOG_INFO("disconnect:%s start. event:%p", addr.c_str(), m_event.get());
9193
freeBufferEvent();
@@ -104,7 +106,7 @@ TcpConnectStatus TcpTransport::connect(const string &strServerURL, int timeoutMi
104106
// TODO: call DNS for hostname
105107

106108
{
107-
boost::lock_guard<boost::mutex> lock(m_eventLock);
109+
std::lock_guard<std::mutex> lock(m_eventLock);
108110

109111
struct sockaddr_in sin;
110112
memset(&sin, 0, sizeof(sin));
@@ -135,7 +137,7 @@ TcpConnectStatus TcpTransport::connect(const string &strServerURL, int timeoutMi
135137
if (connectStatus != TCP_CONNECT_STATUS_SUCCESS) {
136138
LOG_WARN("can not connect to server:%s", strServerURL.c_str());
137139

138-
boost::lock_guard<boost::mutex> lock(m_eventLock);
140+
std::lock_guard<std::mutex> lock(m_eventLock);
139141
freeBufferEvent();
140142
setTcpConnectStatus(TCP_CONNECT_STATUS_FAILED);
141143
return TCP_CONNECT_STATUS_FAILED;
@@ -228,7 +230,7 @@ void TcpTransport::messageReceived(const MemoryBlock &mem, const std::string &ad
228230
}
229231

230232
bool TcpTransport::sendMessage(const char *pData, size_t len) {
231-
boost::lock_guard<boost::mutex> lock(m_eventLock);
233+
std::lock_guard<std::mutex> lock(m_eventLock);
232234
if (getTcpConnectStatus() != TCP_CONNECT_STATUS_SUCCESS) {
233235
return false;
234236
}
@@ -241,7 +243,7 @@ bool TcpTransport::sendMessage(const char *pData, size_t len) {
241243
}
242244

243245
const string TcpTransport::getPeerAddrAndPort() {
244-
boost::lock_guard<boost::mutex> lock(m_eventLock);
246+
std::lock_guard<std::mutex> lock(m_eventLock);
245247
return m_event ? m_event->getPeerAddrPort() : "";
246248
}
247249

src/transport/TcpTransport.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
#ifndef __TCPTRANSPORT_H__
1818
#define __TCPTRANSPORT_H__
1919

20-
#include <boost/atomic.hpp>
21-
#include <boost/thread/condition_variable.hpp>
22-
#include <boost/thread/mutex.hpp>
23-
#include <boost/thread/thread.hpp>
20+
#include <atomic>
21+
#include <mutex>
22+
#include <condition_variable>
2423

2524
#include "dataBlock.h"
2625
#include "EventLoop.h"
@@ -76,11 +75,11 @@ class TcpTransport : public std::enable_shared_from_this<TcpTransport> {
7675
uint64_t m_startTime;
7776

7877
std::shared_ptr<BufferEvent> m_event; // NOTE: use m_event in callback is unsafe.
79-
boost::mutex m_eventLock;
80-
boost::atomic<TcpConnectStatus> m_tcpConnectStatus;
78+
std::mutex m_eventLock;
79+
std::atomic<TcpConnectStatus> m_tcpConnectStatus;
8180

82-
boost::mutex m_connectEventLock;
83-
boost::condition_variable_any m_connectEvent;
81+
std::mutex m_connectEventLock;
82+
std::condition_variable m_connectEvent;
8483

8584
//<! read data callback
8685
TcpTransportReadCallback m_readCallback;

0 commit comments

Comments
 (0)