proxygen
SharedPromise-inl.h
Go to the documentation of this file.
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 
19 namespace folly {
20 
21 template <class T>
23  *this = std::move(other);
24 }
25 
26 template <class T>
28  SharedPromise<T>&& other) noexcept {
29  if (this == &other) {
30  return *this;
31  }
32 
33  // std::lock will perform deadlock avoidance, in case
34  // Thread A: p1 = std::move(p2)
35  // Thread B: p2 = std::move(p1)
36  // race each other
37  std::lock(mutex_, other.mutex_);
38  std::lock_guard<std::mutex> g1(mutex_, std::adopt_lock);
39  std::lock_guard<std::mutex> g2(other.mutex_, std::adopt_lock);
40 
41  std::swap(size_, other.size_);
42  std::swap(hasValue_, other.hasValue_);
43  std::swap(try_, other.try_);
44  std::swap(interruptHandler_, other.interruptHandler_);
45  std::swap(promises_, other.promises_);
46 
47  return *this;
48 }
49 
50 template <class T>
52  std::lock_guard<std::mutex> g(mutex_);
53  return size_;
54 }
55 
56 template <class T>
58  std::lock_guard<std::mutex> g(mutex_);
59  size_++;
60  if (hasValue_) {
61  return makeFuture<T>(Try<T>(try_));
62  } else {
63  promises_.emplace_back();
64  if (interruptHandler_) {
65  promises_.back().setInterruptHandler(interruptHandler_);
66  }
67  return promises_.back().getSemiFuture();
68  }
69 }
70 
71 template <class T>
73  return getSemiFuture().via(&InlineExecutor::instance());
74 }
75 
76 template <class T>
77 template <class E>
80  setTry(Try<T>(e));
81 }
82 
83 template <class T>
85  setTry(Try<T>(std::move(ew)));
86 }
87 
88 template <class T>
90  std::function<void(exception_wrapper const&)> fn) {
91  std::lock_guard<std::mutex> g(mutex_);
92  if (hasValue_) {
93  return;
94  }
95  interruptHandler_ = fn;
96  for (auto& p : promises_) {
97  p.setInterruptHandler(fn);
98  }
99 }
100 
101 template <class T>
102 template <class M>
104  setTry(Try<T>(std::forward<M>(v)));
105 }
106 
107 template <class T>
108 template <class F>
110  setTry(makeTryWith(std::forward<F>(func)));
111 }
112 
113 template <class T>
115  std::vector<Promise<T>> promises;
116 
117  {
118  std::lock_guard<std::mutex> g(mutex_);
119  if (hasValue_) {
120  throw_exception<PromiseAlreadySatisfied>();
121  }
122  hasValue_ = true;
123  try_ = std::move(t);
124  promises.swap(promises_);
125  }
126 
127  for (auto& p : promises) {
128  p.setTry(Try<T>(try_));
129  }
130 }
131 
132 template <class T>
134  std::lock_guard<std::mutex> g(mutex_);
135  return hasValue_;
136 }
137 
138 } // namespace folly
PskType type
void setInterruptHandler(std::function< void(exception_wrapper const &)>)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
void setTry(Try< T > &&t)
std::mutex mutex_
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)
auto lock(SynchronizedLocker...lockersIn) -> std::tuple< typename SynchronizedLocker::LockedPtr... >
Definition: Synchronized.h:871
FOLLY_ATTR_VISIBILITY_HIDDEN static FOLLY_ALWAYS_INLINE InlineExecutor & instance() noexcept
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::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
g_t g(f_t)
void swap(SwapTrackingAlloc< T > &, SwapTrackingAlloc< T > &)
Definition: F14TestUtil.h:414
SemiFuture< T > getSemiFuture()