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

Implicitly default the SharedPromise ctors, assigns, dtor

Summary:
[Folly] Implicitly default the `SharedPromise` ctors, assigns, dtor.

If the default implementations are correct, then they are easier to maintain over time.

Has the effect of removing thread-safety from the move-assign operator. Which is quite alright - copy-assignment and move-assignment operators being thread-safe is weird.

Reviewed By: davidtgoldblatt

Differential Revision: D13131063

fbshipit-source-id: 2be67b06d2a65867dc8ebbd7364b74a9cd1c2a20
parent 3c798d86
...@@ -18,35 +18,6 @@ ...@@ -18,35 +18,6 @@
namespace folly { namespace folly {
template <class T>
SharedPromise<T>::SharedPromise(SharedPromise<T>&& other) noexcept {
*this = std::move(other);
}
template <class T>
SharedPromise<T>& SharedPromise<T>::operator=(
SharedPromise<T>&& other) noexcept {
if (this == &other) {
return *this;
}
// std::lock will perform deadlock avoidance, in case
// Thread A: p1 = std::move(p2)
// Thread B: p2 = std::move(p1)
// race each other
std::lock(mutex_, other.mutex_);
std::lock_guard<std::mutex> g1(mutex_, std::adopt_lock);
std::lock_guard<std::mutex> g2(other.mutex_, std::adopt_lock);
std::swap(size_, other.size_);
std::swap(hasValue_, other.hasValue_);
std::swap(try_, other.try_);
std::swap(interruptHandler_, other.interruptHandler_);
std::swap(promises_, other.promises_);
return *this;
}
template <class T> template <class T>
size_t SharedPromise<T>::size() { size_t SharedPromise<T>::size() {
std::lock_guard<std::mutex> g(mutex_); std::lock_guard<std::mutex> g(mutex_);
......
...@@ -40,17 +40,6 @@ namespace folly { ...@@ -40,17 +40,6 @@ namespace folly {
template <class T> template <class T>
class SharedPromise { class SharedPromise {
public: public:
SharedPromise() = default;
~SharedPromise() = default;
// not copyable
SharedPromise(SharedPromise const&) = delete;
SharedPromise& operator=(SharedPromise const&) = delete;
// movable
SharedPromise(SharedPromise<T>&&) noexcept;
SharedPromise& operator=(SharedPromise<T>&&) noexcept;
/** /**
* Return a Future tied to the shared core state. Unlike Promise::getFuture, * Return a Future tied to the shared core state. Unlike Promise::getFuture,
* this can be called an unlimited number of times per SharedPromise. * this can be called an unlimited number of times per SharedPromise.
...@@ -111,7 +100,16 @@ class SharedPromise { ...@@ -111,7 +100,16 @@ class SharedPromise {
bool isFulfilled(); bool isFulfilled();
private: private:
std::mutex mutex_; // this allows SharedPromise move-ctor/move-assign to be defaulted
struct Mutex : std::mutex {
Mutex() = default;
Mutex(Mutex&&) noexcept {}
Mutex& operator=(Mutex&&) noexcept {
return *this;
}
};
Mutex mutex_;
size_t size_{0}; size_t size_{0};
bool hasValue_{false}; bool hasValue_{false};
Try<T> try_; Try<T> try_;
......
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