(Wangle) unorderedReduce
[folly.git] / folly / futures / SharedPromise-inl.h
1 /*
2  * Copyright 2015 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18
19 namespace folly {
20
21 template <class T>
22 SharedPromise<T>::SharedPromise(SharedPromise<T>&& other) noexcept {
23   *this = std::move(other);
24 }
25
26 template <class T>
27 SharedPromise<T>& SharedPromise<T>::operator=(
28     SharedPromise<T>&& other) noexcept {
29   if (this == &other) {
30     return *this;
31   }
32
33   // std::lock will perform deadlock avoidance, in case
34   // Thread A: p1 = std::move(p2)
35   // Thread B: p2 = std::move(p1)
36   // race each other
37   std::lock(mutex_, other.mutex_);
38   std::lock_guard<std::mutex> g1(mutex_, std::adopt_lock);
39   std::lock_guard<std::mutex> g2(other.mutex_, std::adopt_lock);
40
41   std::swap(size_, other.size_);
42   std::swap(hasValue_, other.hasValue_);
43   std::swap(try_, other.try_);
44   std::swap(promises_, other.promises_);
45
46   return *this;
47 }
48
49 template <class T>
50 size_t SharedPromise<T>::size() {
51   std::lock_guard<std::mutex> g(mutex_);
52   return size_;
53 }
54
55 template <class T>
56 Future<T> SharedPromise<T>::getFuture() {
57   std::lock_guard<std::mutex> g(mutex_);
58   size_++;
59   if (hasValue_) {
60     return makeFuture<T>(Try<T>(try_));
61   } else {
62     promises_.emplace_back();
63     return promises_.back().getFuture();
64   }
65 }
66
67 template <class T>
68 template <class E>
69 typename std::enable_if<std::is_base_of<std::exception, E>::value>::type
70 SharedPromise<T>::setException(E const& e) {
71   setTry(Try<T>(e));
72 }
73
74 template <class T>
75 void SharedPromise<T>::setException(std::exception_ptr const& ep) {
76   setTry(Try<T>(ep));
77 }
78
79 template <class T>
80 void SharedPromise<T>::setException(exception_wrapper ew) {
81   setTry(Try<T>(std::move(ew)));
82 }
83
84 template <class T>
85 void SharedPromise<T>::setInterruptHandler(
86     std::function<void(exception_wrapper const&)> fn) {
87   std::lock_guard<std::mutex> g(mutex_);
88   if (hasValue_) {
89     return;
90   }
91   for (auto& p : promises_) {
92     p.setInterruptHandler(fn);
93   }
94 }
95
96 template <class T>
97 template <class M>
98 void SharedPromise<T>::setValue(M&& v) {
99   setTry(Try<T>(std::forward<M>(v)));
100 }
101
102 template <class T>
103 template <class F>
104 void SharedPromise<T>::setWith(F&& func) {
105   setTry(makeTryFunction(std::forward<F>(func)));
106 }
107
108 template <class T>
109 void SharedPromise<T>::setTry(Try<T>&& t) {
110   std::vector<Promise<T>> promises;
111
112   {
113     std::lock_guard<std::mutex> g(mutex_);
114     if (hasValue_) {
115       throw PromiseAlreadySatisfied();
116     }
117     hasValue_ = true;
118     try_ = std::move(t);
119     promises.swap(promises_);
120   }
121
122   for (auto& p : promises) {
123     p.setTry(Try<T>(try_));
124   }
125 }
126
127 }