proxygen
Promise-inl.h
Go to the documentation of this file.
1 /*
2  * Copyright 2014-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 #include <atomic>
20 #include <thread>
21 
24 
25 namespace folly {
26 
27 namespace futures {
28 namespace detail {
29 template <typename T>
31  if (!core.hasResult()) {
33  }
34  core.detachPromise();
35 }
36 } // namespace detail
37 } // namespace futures
38 
39 template <class T>
42 }
43 
44 template <class T>
45 Promise<T>::Promise() : retrieved_(false), core_(Core::make()) {}
46 
47 template <class T>
49  : retrieved_(exchange(other.retrieved_, false)),
50  core_(exchange(other.core_, nullptr)) {}
51 
52 template <class T>
54  detach();
55  retrieved_ = exchange(other.retrieved_, false);
56  core_ = exchange(other.core_, nullptr);
57  return *this;
58 }
59 
60 template <class T>
62  if (getCore().hasResult()) {
63  throw_exception<PromiseAlreadySatisfied>();
64  }
65 }
66 
67 template <class T>
69  : retrieved_(false), core_(nullptr) {}
70 
71 template <class T>
73  detach();
74 }
75 
76 template <class T>
78  if (core_) {
79  if (!retrieved_) {
81  }
83  core_ = nullptr;
84  }
85 }
86 
87 template <class T>
89  if (retrieved_) {
90  throw_exception<FutureAlreadyRetrieved>();
91  }
92  retrieved_ = true;
93  return SemiFuture<T>(&getCore());
94 }
95 
96 template <class T>
98  // An InlineExecutor approximates the old behaviour of continuations
99  // running inine on setting the value of the promise.
100  return getSemiFuture().via(&InlineExecutor::instance());
101 }
102 
103 template <class T>
104 template <class E>
107  setException(make_exception_wrapper<E>(e));
108 }
109 
110 template <class T>
112  setTry(Try<T>(std::move(ew)));
113 }
114 
115 template <class T>
116 template <typename F>
118  getCore().setInterruptHandler(std::forward<F>(fn));
119 }
120 
121 template <class T>
125 }
126 
127 template <class T>
128 template <class M>
130  static_assert(!std::is_same<T, void>::value, "Use setValue() instead");
131 
132  setTry(Try<T>(std::forward<M>(v)));
133 }
134 
135 template <class T>
136 template <class F>
137 void Promise<T>::setWith(F&& func) {
139  setTry(makeTryWith(std::forward<F>(func)));
140 }
141 
142 template <class T>
144  if (core_) {
145  return core_->hasResult();
146  }
147  return true;
148 }
149 
150 } // namespace folly
void setInterruptHandler(F &&fn)
Definition: Promise-inl.h:117
void throwIfFulfilled() const
Definition: Promise-inl.h:61
static Promise< T > makeEmpty() noexcept
Definition: Promise-inl.h:40
PskType type
void setWith(F &&func)
Definition: Promise-inl.h:137
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void coreDetachPromiseMaybeWithResult(Core< T > &core)
Definition: Promise-inl.h:30
bool isFulfilled() const noexcept
Definition: Promise-inl.h:143
void setException(exception_wrapper ew)
Definition: Promise-inl.h:111
folly::std T
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
void setTry(Try< T > &&t)
Definition: Promise-inl.h:122
void detachFuture() noexcept
Definition: Core.h:409
const char * name
Definition: http_parser.c:437
void detachPromise() noexcept
Definition: Core.h:416
Core * core_
Definition: Promise.h:438
FOLLY_ATTR_VISIBILITY_HIDDEN static FOLLY_ALWAYS_INLINE InlineExecutor & instance() noexcept
Future< T > getFuture()
Definition: Promise-inl.h:97
void setInterruptHandler(F &&fn)
Definition: Core.h:483
static const char *const value
Definition: Conv.cpp:50
Definition: Try.h:51
**Optimized Holders **The template hazptr_array< M > provides most of the functionality *of M hazptr_holder s but with faster construction destruction *for M
Definition: Hazptr.h:104
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Definition: Promise.h:326
SemiFuture< T > getSemiFuture()
Definition: Promise-inl.h:88
T exchange(T &obj, U &&new_value)
Definition: Utility.h:120
std::enable_if< !std::is_same< invoke_result_t< F >, void >::value, Try< invoke_result_t< F > > >::type makeTryWith(F &&f)
Definition: Try-inl.h:223
const
Definition: upload.py:398
Core & getCore()
Definition: Promise.h:421
void setResult(Try< T > &&t)
Definition: Core.h:376
Promise & operator=(Promise const &)=delete
bool hasResult() const noexcept
Definition: Core.h:242
bool retrieved_
Definition: Promise.h:412