Destroy promise/future callback functions before waking waiters
[folly.git] / folly / futures / Promise-inl.h
1 /*
2  * Copyright 2017 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 #include <atomic>
20 #include <thread>
21
22 #include <folly/futures/FutureException.h>
23 #include <folly/futures/detail/Core.h>
24
25 namespace folly {
26
27 template <class T>
28 Promise<T>::Promise() : retrieved_(false), core_(new detail::Core<T>())
29 {}
30
31 template <class T>
32 Promise<T>::Promise(Promise<T>&& other) noexcept
33     : retrieved_(other.retrieved_), core_(other.core_) {
34   other.core_ = nullptr;
35   other.retrieved_ = false;
36 }
37
38 template <class T>
39 Promise<T>& Promise<T>::operator=(Promise<T>&& other) noexcept {
40   std::swap(core_, other.core_);
41   std::swap(retrieved_, other.retrieved_);
42   return *this;
43 }
44
45 template <class T>
46 void Promise<T>::throwIfFulfilled() {
47   if (UNLIKELY(!core_)) {
48     throw NoState();
49   }
50   if (UNLIKELY(core_->ready())) {
51     throw PromiseAlreadySatisfied();
52   }
53 }
54
55 template <class T>
56 void Promise<T>::throwIfRetrieved() {
57   if (UNLIKELY(retrieved_)) {
58     throw FutureAlreadyRetrieved();
59   }
60 }
61
62 template <class T>
63 Promise<T>::Promise(detail::EmptyConstruct) noexcept
64     : retrieved_(false), core_(nullptr) {}
65
66 template <class T>
67 Promise<T>::~Promise() {
68   detach();
69 }
70
71 template <class T>
72 void Promise<T>::detach() {
73   if (core_) {
74     if (!retrieved_)
75       core_->detachFuture();
76     core_->detachPromise();
77     core_ = nullptr;
78   }
79 }
80
81 template <class T>
82 Future<T> Promise<T>::getFuture() {
83   throwIfRetrieved();
84   retrieved_ = true;
85   return Future<T>(core_);
86 }
87
88 template <class T>
89 template <class E>
90 typename std::enable_if<std::is_base_of<std::exception, E>::value>::type
91 Promise<T>::setException(E const& e) {
92   setException(make_exception_wrapper<E>(e));
93 }
94
95 template <class T>
96 void Promise<T>::setException(std::exception_ptr const& ep) {
97   try {
98     std::rethrow_exception(ep);
99   } catch (const std::exception& e) {
100     setException(exception_wrapper(std::current_exception(), e));
101   } catch (...) {
102     setException(exception_wrapper(std::current_exception()));
103   }
104 }
105
106 template <class T>
107 void Promise<T>::setException(exception_wrapper ew) {
108   throwIfFulfilled();
109   core_->setResult(Try<T>(std::move(ew)));
110 }
111
112 template <class T>
113 void Promise<T>::setInterruptHandler(
114   std::function<void(exception_wrapper const&)> fn) {
115   core_->setInterruptHandler(std::move(fn));
116 }
117
118 template <class T>
119 void Promise<T>::setTry(Try<T>&& t) {
120   throwIfFulfilled();
121   core_->setResult(std::move(t));
122 }
123
124 template <class T>
125 template <class M>
126 void Promise<T>::setValue(M&& v) {
127   static_assert(!std::is_same<T, void>::value,
128                 "Use setValue() instead");
129
130   setTry(Try<T>(std::forward<M>(v)));
131 }
132
133 template <class T>
134 template <class F>
135 void Promise<T>::setWith(F&& func) {
136   throwIfFulfilled();
137   setTry(makeTryWith(std::forward<F>(func)));
138 }
139
140 template <class T>
141 bool Promise<T>::isFulfilled() const noexcept {
142   if (core_) {
143     return core_->hasResult();
144   }
145   return true;
146 }
147
148 }