1 /* 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 23 namespace android { 24 namespace os { 25 namespace 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 */ 33 class MultiConditionTrigger { 34 public: 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 45 private: 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 56