proxygen
SharedPromise.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 <folly/Portability.h>
21 #include <folly/futures/Promise.h>
22 #include <folly/lang/Exception.h>
23 
24 namespace folly {
25 
26 /*
27  * SharedPromise provides the same interface as Promise, but you can extract
28  * multiple Futures from it, i.e. you can call getFuture() as many times as
29  * you'd like. When the SharedPromise is fulfilled, all of the Futures are
30  * completed. Calls to getFuture() after the SharedPromise is fulfilled return
31  * a completed Future. If you find yourself constructing collections of Promises
32  * and fulfilling them simultaneously with the same value, consider this
33  * utility instead. Likewise, if you find yourself in need of setting multiple
34  * callbacks on the same Future (which is indefinitely unsupported), consider
35  * refactoring to use SharedPromise to "split" the Future.
36  *
37  * The ShardPromise must be kept alive manually. Consider FutureSplitter for
38  * automatic lifetime management.
39  */
40 template <class T>
42  public:
43  SharedPromise() = default;
44  ~SharedPromise() = default;
45 
46  // not copyable
47  SharedPromise(SharedPromise const&) = delete;
48  SharedPromise& operator=(SharedPromise const&) = delete;
49 
50  // movable
53 
59 
68 
70  size_t size();
71 
74 
79  template <class E>
81  setException(E const&);
82 
88  void setInterruptHandler(std::function<void(exception_wrapper const&)>);
89 
91  template <class B = T>
93  setTry(Try<T>(T()));
94  }
95 
97  template <class M>
98  void setValue(M&& value);
99 
100  void setTry(Try<T>&& t);
101 
108  template <class F>
109  void setWith(F&& func);
110 
111  bool isFulfilled();
112 
113  private:
115  size_t size_{0};
116  bool hasValue_{false};
118  std::vector<Promise<T>> promises_;
119  std::function<void(exception_wrapper const&)> interruptHandler_;
120 };
121 
122 } // namespace folly
123 
124 #include <folly/futures/Future.h>
PskType type
void setInterruptHandler(std::function< void(exception_wrapper const &)>)
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)
~SharedPromise()=default
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Sugar to fulfill this SharedPromise<Unit>
Definition: SharedPromise.h:92
void setException(exception_wrapper ew)
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
SharedPromise & operator=(SharedPromise const &)=delete
std::mutex mutex
std::vector< Promise< T > > promises_
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
std::function< void(exception_wrapper const &)> interruptHandler_
SemiFuture< T > getSemiFuture()