Skip to content

Commit bf6e631

Browse files
committed
optimize process of allocate opaque by atomic without lock.
1 parent 990c3f2 commit bf6e631

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

src/protocol/RemotingCommand.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
#include "SessionCredentials.h"
2222

2323
namespace rocketmq {
24+
2425
boost::atomic<int> RemotingCommand::s_seqNumber;
25-
boost::mutex RemotingCommand::m_clock;
26+
2627
//<!************************************************************************
2728
RemotingCommand::RemotingCommand(int code,
2829
CommandHeader* pExtHeader /* = NULL */)
@@ -32,11 +33,9 @@ RemotingCommand::RemotingCommand(int code,
3233
m_flag(0),
3334
m_remark(""),
3435
m_pExtHeader(pExtHeader) {
35-
boost::lock_guard<boost::mutex> lock(m_clock);
36-
m_opaque = (s_seqNumber.load(boost::memory_order_acquire)) %
37-
(numeric_limits<int>::max());
38-
s_seqNumber.store(m_opaque, boost::memory_order_release);
39-
++s_seqNumber;
36+
37+
// mask sign bit
38+
m_opaque = s_seqNumber.fetch_add(1, boost::memory_order_relaxed) & numeric_limits<int>::max();
4039
}
4140

4241
RemotingCommand::RemotingCommand(int code, string language, int version,

src/protocol/RemotingCommand.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ class RemotingCommand {
7575
string m_msgBody;
7676
map<string, string> m_extFields;
7777

78-
static boost::mutex m_clock;
7978
MemoryBlock m_head;
8079
MemoryBlock m_body;
8180
//<!save here
8281
Json::Value m_parsedJson;
83-
static boost::atomic<int> s_seqNumber;
8482
unique_ptr<CommandHeader> m_pExtHeader;
83+
84+
static boost::atomic<int> s_seqNumber;
8585
};
8686

8787
} //<!end namespace;

0 commit comments

Comments
 (0)