Fix copyright lines
[folly.git] / folly / futures / SharedPromise-inl.h
1 /*
2  * Copyright 2015-present 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(interruptHandler_, other.interruptHandler_);
45   std::swap(promises_, other.promises_);
46
47   return *this;
48 }
49
50 template <class T>
51 size_t SharedPromise<T>::size() {
52   std::lock_guard<std::mutex> g(mutex_);
53   return size_;
54 }
55
56 template <class T>
57 SemiFuture<T> SharedPromise<T>::getSemiFuture() {
58   std::lock_guard<std::mutex> g(mutex_);
59   size_++;
60   if (hasValue_) {
61     return makeFuture<T>(Try<T>(try_));
62   } else {
63     promises_.emplace_back();
64     if (interruptHandler_) {
65       promises_.back().setInterruptHandler(interruptHandler_);
66     }
67     return promises_.back().getSemiFuture();
68   }
69 }
70
71 template <class T>
72 Future<T> SharedPromise<T>::getFuture() {
73   return getSemiFuture().via(&folly::InlineExecutor::instance());
74 }
75
76 template <class T>
77 template <class E>
78 typename std::enable_if<std::is_base_of<std::exception, E>::value>::type
79 SharedPromise<T>::setException(E const& e) {
80   setTry(Try<T>(e));
81 }
82
83 template <class T>
84 void SharedPromise<T>::setException(std::exception_ptr const& ep) {
85   setTry(Try<T>(ep));
86 }
87
88 template <class T>
89 void SharedPromise<T>::setException(exception_wrapper ew) {
90   setTry(Try<T>(std::move(ew)));
91 }
92
93 template <class T>
94 void SharedPromise<T>::setInterruptHandler(
95     std::function<void(exception_wrapper const&)> fn) {
96   std::lock_guard<std::mutex> g(mutex_);
97   if (hasValue_) {
98     return;
99   }
100   interruptHandler_ = fn;
101   for (auto& p : promises_) {
102     p.setInterruptHandler(fn);
103   }
104 }
105
106 template <class T>
107 template <class M>
108 void SharedPromise<T>::setValue(M&& v) {
109   setTry(Try<T>(std::forward<M>(v)));
110 }
111
112 template <class T>
113 template <class F>
114 void SharedPromise<T>::setWith(F&& func) {
115   setTry(makeTryWith(std::forward<F>(func)));
116 }
117
118 template <class T>
119 void SharedPromise<T>::setTry(Try<T>&& t) {
120   std::vector<Promise<T>> promises;
121
122   {
123     std::lock_guard<std::mutex> g(mutex_);
124     if (hasValue_) {
125       throwPromiseAlreadySatisfied();
126     }
127     hasValue_ = true;
128     try_ = std::move(t);
129     promises.swap(promises_);
130   }
131
132   for (auto& p : promises) {
133     p.setTry(Try<T>(try_));
134   }
135 }
136
137 template <class T>
138 bool SharedPromise<T>::isFulfilled() {
139   std::lock_guard<std::mutex> g(mutex_);
140   return hasValue_;
141 }
142
143 } // namespace folly