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 #include <folly/fibers/Baton.h>
17 
18 namespace folly {
19 namespace fibers {
20 
21 template <class T, class BatonT>
23  : value_(&value), baton_(&baton) {}
24 
25 template <class T, class BatonT>
27  : value_(other.value_), baton_(other.baton_) {
28  other.value_ = nullptr;
29  other.baton_ = nullptr;
30 }
31 
32 template <class T, class BatonT>
34  std::swap(value_, other.value_);
35  std::swap(baton_, other.baton_);
36  return *this;
37 }
38 
39 template <class T, class BatonT>
41  if (!value_) {
42  throw std::logic_error("promise already fulfilled");
43  }
44 }
45 
46 template <class T, class BatonT>
48  if (value_) {
49  setException(folly::make_exception_wrapper<std::logic_error>(
50  "promise not fulfilled"));
51  }
52 }
53 
54 template <class T, class BatonT>
57 }
58 
59 template <class T, class BatonT>
62 
63  *value_ = std::move(t);
64  value_ = nullptr;
65 
66  // Baton::post has to be the last step here, since if Promise is not owned by
67  // the posting thread, it may be destroyed right after Baton::post is called.
68  baton_->post();
69 }
70 
71 template <class T, class BatonT>
72 template <class M>
74  static_assert(!std::is_same<T, void>::value, "Use setValue() instead");
75 
76  setTry(folly::Try<T>(std::forward<M>(v)));
77 }
78 
79 template <class T, class BatonT>
81  static_assert(std::is_same<T, void>::value, "Use setValue(value) instead");
82 
84 }
85 
86 template <class T, class BatonT>
87 template <class F>
89  setTry(makeTryWith(std::forward<F>(func)));
90 }
91 
92 template <class T, class BatonT>
93 template <class F>
96  std::exception_ptr funcException;
97 
98  BatonT baton;
99  baton.wait([&func, &result, &baton, &funcException]() mutable {
100  try {
101  func(Promise<value_type, BatonT>(result, baton));
102  } catch (...) {
103  // Save the exception, but still wait for baton to be posted by user code
104  // or promise destructor.
105  funcException = std::current_exception();
106  }
107  });
108 
109  if (UNLIKELY(funcException != nullptr)) {
110  std::rethrow_exception(funcException);
111  }
112 
113  return std::move(result).value();
114 }
115 } // namespace fibers
116 } // namespace folly
void setTry(folly::Try< T > &&t)
Definition: Promise-inl.h:60
void setWith(F &&func)
Definition: Promise-inl.h:88
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
folly::Try< T > * value_
Definition: Promise.h:86
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
void setException(folly::exception_wrapper)
Definition: Promise-inl.h:55
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
Promise(const Promise &)=delete
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
void throwIfFulfilled() const
Definition: Promise-inl.h:40
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
void swap(SwapTrackingAlloc< T > &, SwapTrackingAlloc< T > &)
Definition: F14TestUtil.h:414
static value_type await(F &&func)
Definition: Promise-inl.h:94
#define UNLIKELY(x)
Definition: Likely.h:48
Promise & operator=(const Promise &)=delete