Commit 13256cb4 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Use exchange in folly/Synchronized.h

Summary: [Folly] Use exchange in `folly/Synchronized.h` as an example and to make the code there slightly shorter.

Reviewed By: aary

Differential Revision: D7722730

fbshipit-source-id: c594fc96cff11781b60a3848a860454bcf21c3ca
parent 16e98a0d
...@@ -995,9 +995,7 @@ class LockedPtrBase<SynchronizedType, std::mutex, LockPolicy> { ...@@ -995,9 +995,7 @@ class LockedPtrBase<SynchronizedType, std::mutex, LockPolicy> {
} }
LockedPtrBase(LockedPtrBase&& rhs) noexcept LockedPtrBase(LockedPtrBase&& rhs) noexcept
: lock_(std::move(rhs.lock_)), parent_(rhs.parent_) { : lock_(std::move(rhs.lock_)), parent_(exchange(rhs.parent_, nullptr)) {}
rhs.parent_ = nullptr;
}
LockedPtrBase& operator=(LockedPtrBase&& rhs) noexcept { LockedPtrBase& operator=(LockedPtrBase&& rhs) noexcept {
lock_ = std::move(rhs.lock_); lock_ = std::move(rhs.lock_);
parent_ = rhs.parent_; parent_ = rhs.parent_;
...@@ -1081,9 +1079,7 @@ class ScopedUnlocker { ...@@ -1081,9 +1079,7 @@ class ScopedUnlocker {
ScopedUnlocker(const ScopedUnlocker&) = delete; ScopedUnlocker(const ScopedUnlocker&) = delete;
ScopedUnlocker& operator=(const ScopedUnlocker&) = delete; ScopedUnlocker& operator=(const ScopedUnlocker&) = delete;
ScopedUnlocker(ScopedUnlocker&& other) noexcept ScopedUnlocker(ScopedUnlocker&& other) noexcept
: ptr_(other.ptr_), data_(std::move(other.data_)) { : ptr_(exchange(other.ptr_, nullptr)), data_(std::move(other.data_)) {}
other.ptr_ = nullptr;
}
ScopedUnlocker& operator=(ScopedUnlocker&& other) = delete; ScopedUnlocker& operator=(ScopedUnlocker&& other) = delete;
~ScopedUnlocker() { ~ScopedUnlocker() {
...@@ -1248,10 +1244,8 @@ class LockedPtr : public LockedPtrBase< ...@@ -1248,10 +1244,8 @@ class LockedPtr : public LockedPtrBase<
LockTraits<typename SyncType::MutexType>::is_upgrade>::type> LockTraits<typename SyncType::MutexType>::is_upgrade>::type>
LockedPtr<SynchronizedType, LockPolicyFromUpgradeToExclusive> LockedPtr<SynchronizedType, LockPolicyFromUpgradeToExclusive>
moveFromUpgradeToWrite() { moveFromUpgradeToWrite() {
auto* parent_to_pass_on = this->parent_;
this->parent_ = nullptr;
return LockedPtr<SynchronizedType, LockPolicyFromUpgradeToExclusive>( return LockedPtr<SynchronizedType, LockPolicyFromUpgradeToExclusive>(
parent_to_pass_on); exchange(this->parent_, nullptr));
} }
/** /**
...@@ -1264,10 +1258,8 @@ class LockedPtr : public LockedPtrBase< ...@@ -1264,10 +1258,8 @@ class LockedPtr : public LockedPtrBase<
LockTraits<typename SyncType::MutexType>::is_upgrade>::type> LockTraits<typename SyncType::MutexType>::is_upgrade>::type>
LockedPtr<SynchronizedType, LockPolicyFromExclusiveToUpgrade> LockedPtr<SynchronizedType, LockPolicyFromExclusiveToUpgrade>
moveFromWriteToUpgrade() { moveFromWriteToUpgrade() {
auto* parent_to_pass_on = this->parent_;
this->parent_ = nullptr;
return LockedPtr<SynchronizedType, LockPolicyFromExclusiveToUpgrade>( return LockedPtr<SynchronizedType, LockPolicyFromExclusiveToUpgrade>(
parent_to_pass_on); exchange(this->parent_, nullptr));
} }
/** /**
...@@ -1280,10 +1272,8 @@ class LockedPtr : public LockedPtrBase< ...@@ -1280,10 +1272,8 @@ class LockedPtr : public LockedPtrBase<
LockTraits<typename SyncType::MutexType>::is_upgrade>::type> LockTraits<typename SyncType::MutexType>::is_upgrade>::type>
LockedPtr<SynchronizedType, LockPolicyFromUpgradeToShared> LockedPtr<SynchronizedType, LockPolicyFromUpgradeToShared>
moveFromUpgradeToRead() { moveFromUpgradeToRead() {
auto* parent_to_pass_on = this->parent_;
this->parent_ = nullptr;
return LockedPtr<SynchronizedType, LockPolicyFromUpgradeToShared>( return LockedPtr<SynchronizedType, LockPolicyFromUpgradeToShared>(
parent_to_pass_on); exchange(this->parent_, nullptr));
} }
/** /**
...@@ -1296,10 +1286,8 @@ class LockedPtr : public LockedPtrBase< ...@@ -1296,10 +1286,8 @@ class LockedPtr : public LockedPtrBase<
LockTraits<typename SyncType::MutexType>::is_upgrade>::type> LockTraits<typename SyncType::MutexType>::is_upgrade>::type>
LockedPtr<SynchronizedType, LockPolicyFromExclusiveToShared> LockedPtr<SynchronizedType, LockPolicyFromExclusiveToShared>
moveFromWriteToRead() { moveFromWriteToRead() {
auto* parent_to_pass_on = this->parent_;
this->parent_ = nullptr;
return LockedPtr<SynchronizedType, LockPolicyFromExclusiveToShared>( return LockedPtr<SynchronizedType, LockPolicyFromExclusiveToShared>(
parent_to_pass_on); exchange(this->parent_, nullptr));
} }
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment