Skip to content

Commit

Permalink
use autolock
Browse files Browse the repository at this point in the history
  • Loading branch information
binwei committed Jul 20, 2024
1 parent b8a083d commit 836b67a
Showing 1 changed file with 1 addition and 4 deletions.
5 changes: 1 addition & 4 deletions cpp/core/memory/AllocationListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ class BlockAllocationListener final : public AllocationListener {
if (diff == 0) {
return;
}
std::unique_lock<std::mutex> guard{mutex_, std::defer_lock};
std::unique_lock<std::mutex> guard{mutex_};
if (diff > 0) {
guard.lock();
if (reservationBytes_ - usedBytes_ < diff) {
auto roundSize = (diff + (blockSize_ - 1)) / blockSize_ * blockSize_;
reservationBytes_ += roundSize;
Expand All @@ -69,9 +68,7 @@ class BlockAllocationListener final : public AllocationListener {
delegated_->allocationChanged(roundSize);
}
usedBytes_ += diff;
guard.unlock();
} else {
guard.lock();
usedBytes_ += diff;
auto unreservedSize = (reservationBytes_ - usedBytes_) / blockSize_ * blockSize_;
reservationBytes_ -= unreservedSize;
Expand Down

0 comments on commit 836b67a

Please sign in to comment.