Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/brpc/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,8 @@ void Socket::Revive() {
LOG(WARNING) << *this << " was abandoned during revival";
return;
}

std::unique_lock<butil::Mutex> recycle_lock(_recycle_mutex);
// +1 is the additional ref added in Create(). TODO(gejun): we should
// remove this additional nref someday.
if (_versioned_ref.compare_exchange_weak(
Expand All @@ -759,6 +761,7 @@ void Socket::Revive() {
butil::memory_order_relaxed)) {
// Set this flag to true since we add additional ref again
_recycle_flag.store(false, butil::memory_order_relaxed);
recycle_lock.unlock();
if (_user) {
_user->AfterRevived(this);
} else {
Expand All @@ -771,11 +774,14 @@ void Socket::Revive() {

int Socket::ReleaseAdditionalReference() {
bool expect = false;

std::unique_lock<butil::Mutex> recycle_lock(_recycle_mutex);
// Use `relaxed' fence here since `Dereference' has `released' fence
if (_recycle_flag.compare_exchange_strong(
expect, true,
butil::memory_order_relaxed,
butil::memory_order_relaxed)) {
recycle_lock.unlock();
return Dereference();
}
return -1;
Expand Down
1 change: 1 addition & 0 deletions src/brpc/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@ friend void DereferenceSocket(Socket*);
// Flag used to mark whether additional reference has been decreased
// by either `SetFailed' or `SetRecycle'
butil::atomic<bool> _recycle_flag;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

既然都加锁了,这个就不需要是原子变量了吧?

butil::Mutex _recycle_mutex;

// Concrete error information from SetFailed()
// Accesses to these 2 fields(especially _error_text) must be protected
Expand Down