• 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_INTERFACES_THREAD_MUTATOR_BASE_INL_H
17 #define COMMON_INTERFACES_THREAD_MUTATOR_BASE_INL_H
18 
19 #include "thread/mutator_base.h"
20 
21 #include "base/common.h"
22 
23 namespace common {
DoEnterSaferegion()24 inline void MutatorBase::DoEnterSaferegion()
25 {
26     // set current mutator in saferegion.
27     SetInSaferegion(SAFE_REGION_TRUE);
28 }
29 
EnterSaferegion(bool updateUnwindContext)30 inline bool MutatorBase::EnterSaferegion(bool updateUnwindContext) noexcept
31 {
32     if (LIKELY_CC(!InSaferegion())) {
33         DoEnterSaferegion();
34         return true;
35     }
36     return false;
37 }
38 
LeaveSaferegion()39 inline bool MutatorBase::LeaveSaferegion() noexcept
40 {
41     if (LIKELY_CC(InSaferegion())) {
42         DoLeaveSaferegion();
43         return true;
44     }
45     return false;
46 }
47 
48 // Ensure that mutator is changed only once by mutator itself or Profile
TransitionToCpuProfile(bool bySelf)49 bool MutatorBase::TransitionToCpuProfile(bool bySelf)
50 {
51     do {
52         CpuProfileState state = cpuProfileState_.load();
53         // If this mutator profile has finished, just return
54         if (state == FINISH_CPUPROFILE) {
55             return true;
56         }
57         // If this mutator is executing profile by other thread, mutator should wait but profile just return
58         if (state == IN_CPUPROFILING) {
59             if (bySelf) {
60                 WaitForCpuProfiling();
61                 return true;
62             } else {
63                 return false;
64             }
65         }
66         if (!bySelf && state == NO_CPUPROFILE) {
67             return true;
68         }
69         // Current thread set atomic variable to ensure atomicity of phase transition
70         CHECK_CC(state == NEED_CPUPROFILE);
71         if (cpuProfileState_.compare_exchange_weak(state, IN_CPUPROFILING)) {
72             TransitionToCpuProfileExclusive();
73             cpuProfileState_.store(FINISH_CPUPROFILE, std::memory_order_release);
74             return true;
75         }
76     } while (true);
77 }
78 }  // namespace common
79 #endif  // COMMON_INTERFACES_THREAD_MUTATOR_BASE_INL_H