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 #define DEBUG false  // STOPSHIP if true
17 
18 #include "MultiConditionTrigger.h"
19 
20 #include <thread>
21 
22 using namespace std;
23 
24 namespace android {
25 namespace os {
26 namespace statsd {
27 
MultiConditionTrigger(const set<string> & conditionNames,function<void ()> trigger)28 MultiConditionTrigger::MultiConditionTrigger(const set<string>& conditionNames,
29                                              function<void()> trigger)
30     : mRemainingConditionNames(conditionNames),
31       mTrigger(trigger),
32       mCompleted(mRemainingConditionNames.empty()) {
33     if (mCompleted) {
34         thread executorThread([this] { mTrigger(); });
35         executorThread.detach();
36     }
37 }
38 
markComplete(const string & conditionName)39 void MultiConditionTrigger::markComplete(const string& conditionName) {
40     bool doTrigger = false;
41     {
42         lock_guard<mutex> lg(mMutex);
43         if (mCompleted) {
44             return;
45         }
46         mRemainingConditionNames.erase(conditionName);
47         mCompleted = mRemainingConditionNames.empty();
48         doTrigger = mCompleted;
49     }
50     if (doTrigger) {
51         std::thread executorThread([this] { mTrigger(); });
52         executorThread.detach();
53     }
54 }
55 }  // namespace statsd
56 }  // namespace os
57 }  // namespace android
58