blob: 51f6029915be2fb460c4123e58d1eb3b469e4a36 [file] [log] [blame]
Tej Singhe678cb72020-04-14 16:23:30 -07001/*
2 * Copyright (C) 2020 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#pragma once
17
18#include <gtest/gtest_prod.h>
19
20#include <mutex>
21#include <set>
22
23namespace android {
24namespace os {
25namespace statsd {
26
27/**
28 * This class provides a utility to wait for a set of named conditions to occur.
29 *
30 * It will execute the trigger runnable in a detached thread once all conditions have been marked
31 * true.
32 */
33class MultiConditionTrigger {
34public:
35 explicit MultiConditionTrigger(const std::set<std::string>& conditionNames,
36 std::function<void()> trigger);
37
38 MultiConditionTrigger(const MultiConditionTrigger&) = delete;
39 MultiConditionTrigger& operator=(const MultiConditionTrigger&) = delete;
40
41 // Mark a specific condition as true. If this condition has called markComplete already or if
42 // the event was not specified in the constructor, the function is a no-op.
43 void markComplete(const std::string& eventName);
44
45private:
46 mutable std::mutex mMutex;
47 std::set<std::string> mRemainingConditionNames;
48 std::function<void()> mTrigger;
49 bool mCompleted;
50
51 FRIEND_TEST(MultiConditionTriggerTest, TestCountDownCalledBySameEventName);
52};
53} // namespace statsd
54} // namespace os
55} // namespace android