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
19namespace folly {
20
21template <class T>
22size_t SharedPromise<T>::size() {
23 std::lock_guard<std::mutex> g(mutex_);
24 return size_.value;
25}
26
27template <class T>
28SemiFuture<T> SharedPromise<T>::getSemiFuture() {
29 std::lock_guard<std::mutex> g(mutex_);
30 size_.value++;
31 if (hasResult()) {
32 return makeFuture<T>(Try<T>(try_.value));
33 } else {
34 promises_.emplace_back();
35 if (interruptHandler_) {
36 promises_.back().setInterruptHandler(interruptHandler_);
37 }
38 return promises_.back().getSemiFuture();
39 }
40}
41
42template <class T>
43Future<T> SharedPromise<T>::getFuture() {
44 return getSemiFuture().via(&InlineExecutor::instance());
45}
46
47template <class T>
48template <class E>
49typename std::enable_if<std::is_base_of<std::exception, E>::value>::type
50SharedPromise<T>::setException(E const& e) {
51 setTry(Try<T>(e));
52}
53
54template <class T>
55void SharedPromise<T>::setException(exception_wrapper ew) {
56 setTry(Try<T>(std::move(ew)));
57}
58
59template <class T>
60void SharedPromise<T>::setInterruptHandler(
61 std::function<void(exception_wrapper const&)> fn) {
62 std::lock_guard<std::mutex> g(mutex_);
63 if (hasResult()) {
64 return;
65 }
66 interruptHandler_ = fn;
67 for (auto& p : promises_) {
68 p.setInterruptHandler(fn);
69 }
70}
71
72template <class T>
73template <class M>
74void SharedPromise<T>::setValue(M&& v) {
75 setTry(Try<T>(std::forward<M>(v)));
76}
77
78template <class T>
79template <class F>
80void SharedPromise<T>::setWith(F&& func) {
81 setTry(makeTryWith(std::forward<F>(func)));
82}
83
84template <class T>
85void SharedPromise<T>::setTry(Try<T>&& t) {
86 std::vector<Promise<T>> promises;
87
88 {
89 std::lock_guard<std::mutex> g(mutex_);
90 if (hasResult()) {
91 throw_exception<PromiseAlreadySatisfied>();
92 }
93 try_.value = std::move(t);
94 promises.swap(promises_);
95 }
96
97 for (auto& p : promises) {
98 p.setTry(Try<T>(try_.value));
99 }
100}
101
102template <class T>
103bool SharedPromise<T>::isFulfilled() {
104 std::lock_guard<std::mutex> g(mutex_);
105 return hasResult();
106}
107
108} // namespace folly
109