• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef COMMON_COMPONENTS_MUTATOR_MUTATOR_MANAGER_INL_H
17 #define COMMON_COMPONENTS_MUTATOR_MUTATOR_MANAGER_INL_H
18 
19 #include "common_components/mutator/mutator_manager.h"
20 
21 namespace common {
22 template<class STWFunction>
FlipMutators(STWParam & param,STWFunction && stwFunction,FlipFunction * flipFunction)23 void MutatorManager::FlipMutators(STWParam& param, STWFunction&& stwFunction, FlipFunction *flipFunction)
24 {
25     std::list<Mutator*> undoneMutators;
26     {
27         OHOS_HITRACE(HITRACE_LEVEL_COMMERCIAL, "Waiting-STW", "");
28         ScopedStopTheWorld stw(param);
29         OHOS_HITRACE(HITRACE_LEVEL_COMMERCIAL, param.stwReason, "");
30 
31         stwFunction();
32         bool ignoreFinalizer = true;
33         // Hope process ui thread's flipFunction at last, so add the ui mutator at the end of undoeMuators list.
34         VisitAllMutators([&undoneMutators, flipFunction](Mutator& mutator) {
35                 mutator.SetFlipFunction(flipFunction);
36                 mutator.SetSuspensionFlag(MutatorBase::SuspensionType::SUSPENSION_FOR_PENDING_CALLBACK);
37                 undoneMutators.push_front(&mutator);
38             }, ignoreFinalizer);
39     }
40     // Resume all mutator threads.
41     AcquireMutatorManagementWLock();
42     for (auto it = undoneMutators.begin(); it != undoneMutators.end();) {
43         bool hasDead = std::find(allMutatorList_.begin(), allMutatorList_.end(), *it) == allMutatorList_.end();
44         if (UNLIKELY(hasDead)) {
45             it = undoneMutators.erase(it);
46             continue;
47         }
48         Mutator* mutator = *it;
49         if (mutator->TryRunFlipFunction()) {
50             it = undoneMutators.erase(it);
51             continue;
52         }
53         it++;
54     }
55     // Wait for flipFunction running finish.
56     for (auto it = undoneMutators.begin(); it != undoneMutators.end(); it++) {
57         Mutator* mutator = *it;
58         mutator->WaitFlipFunctionFinish();
59     }
60     MutatorManagementWUnlock();
61 }
62 } // namespace common
63 
64 #endif  // COMMON_COMPONENTS_MUTATOR_MUTATOR_MANAGER_INL_H
65