1/*
2 * Copyright (c) Facebook, Inc. and its affiliates.
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/Unit.h>
20#include <folly/futures/Future.h>
21
22namespace facebook::velox {
23/// Simple wrapper around folly's promise to track down destruction of
24/// unfulfilled promises.
25template <class T>
26class VeloxPromise : public folly::Promise<T> {
27 public:
28 VeloxPromise() : folly::Promise<T>() {}
29
30 explicit VeloxPromise(const std::string& context)
31 : folly::Promise<T>(), context_(context) {}
32
33 VeloxPromise(
34 folly::futures::detail::EmptyConstruct,
35 const std::string& context) noexcept
36 : folly::Promise<T>(folly::Promise<T>::makeEmpty()), context_(context) {}
37
38 ~VeloxPromise() {
39 if (!this->isFulfilled()) {
40 LOG(WARNING) << "PROMISE: Unfulfilled promise is being deleted. Context: "
41 << context_;
42 }
43 }
44
45 explicit VeloxPromise(VeloxPromise<T>&& other)
46 : folly::Promise<T>(std::move(other)),
47 context_(std::move(other.context_)) {}
48
49 VeloxPromise& operator=(VeloxPromise<T>&& other) noexcept {
50 folly::Promise<T>::operator=(std::move(other));
51 context_ = std::move(other.context_);
52 return *this;
53 }
54
55 static VeloxPromise makeEmpty(const std::string& context = "") noexcept {
56 return VeloxPromise<T>(folly::futures::detail::EmptyConstruct{}, context);
57 }
58
59 private:
60 /// Optional parameter to understand where this promise was created.
61 std::string context_;
62};
63
64using ContinuePromise = VeloxPromise<folly::Unit>;
65using ContinueFuture = folly::SemiFuture<folly::Unit>;
66
67/// Equivalent of folly's makePromiseContract for VeloxPromise.
68static inline std::pair<ContinuePromise, ContinueFuture>
69makeVeloxContinuePromiseContract(const std::string& promiseContext = "") {
70 auto p = ContinuePromise(promiseContext);
71 auto f = p.getSemiFuture();
72 return std::make_pair(x: std::move(p), y: std::move(f));
73}
74
75} // namespace facebook::velox
76