blob: 412038ce107916809c062bf23cb29a2027403c7f [file] [log] [blame]
Dominik Laskowskid4a22752024-03-08 17:39:04 -05001/*
2 * Copyright 2024 The Android Open Source Project
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 <future>
20#include <mutex>
21
22#include <android-base/thread_annotations.h>
23
24namespace android::utils {
25
26// Allows a thread to `wait` for a future produced by a different thread. The future is returned by
27// the first call to a function `F` that multiple threads may `callOnce`. If no `callOnce` happens,
28// then `wait` does nothing. Otherwise, it blocks on the future, then destroys it, which resets the
29// `OnceFuture`.
30class OnceFuture {
31public:
32 template <typename F>
33 void callOnce(F f) {
34 std::lock_guard lock(mMutex);
35 if (!mFuture.valid()) {
36 mFuture = f();
37 }
38 }
39
40 void wait() {
41 std::lock_guard lock(mMutex);
42 if (mFuture.valid()) {
43 mFuture.wait();
44 mFuture = {};
45 }
46 }
47
48private:
49 std::mutex mMutex;
50 std::future<void> mFuture GUARDED_BY(mMutex);
51};
52
53} // namespace android::utils