/* * Copyright (C) 2020 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include namespace android { namespace os { namespace statsd { /** * This class provides a utility to wait for a set of named conditions to occur. * * It will execute the trigger runnable in a separate thread (which will be joined at instance * destructor time) once all conditions have been marked true. */ class MultiConditionTrigger { public: explicit MultiConditionTrigger(const std::set& conditionNames, std::function trigger); MultiConditionTrigger(const MultiConditionTrigger&) = delete; MultiConditionTrigger& operator=(const MultiConditionTrigger&) = delete; ~MultiConditionTrigger(); // Mark a specific condition as true. If this condition has called markComplete already or if // the event was not specified in the constructor, the function is a no-op. void markComplete(const std::string& conditionName); private: void startExecutorThread(); mutable std::mutex mMutex; std::set mRemainingConditionNames; std::function mTrigger; bool mCompleted; std::unique_ptr mExecutorThread; FRIEND_TEST(MultiConditionTriggerTest, TestCountDownCalledBySameEventName); }; } // namespace statsd } // namespace os } // namespace android