xref: /aosp_15_r20/art/runtime/gc/scoped_gc_critical_section.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_GC_SCOPED_GC_CRITICAL_SECTION_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_GC_SCOPED_GC_CRITICAL_SECTION_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include "base/locks.h"
21*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
22*795d594fSAndroid Build Coastguard Worker #include "collector_type.h"
23*795d594fSAndroid Build Coastguard Worker #include "gc_cause.h"
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker class Thread;
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker namespace gc {
30*795d594fSAndroid Build Coastguard Worker 
31*795d594fSAndroid Build Coastguard Worker // The use of ScopedGCCriticalSection should be preferred whenever possible.
32*795d594fSAndroid Build Coastguard Worker class GCCriticalSection {
33*795d594fSAndroid Build Coastguard Worker  public:
GCCriticalSection(Thread * self,const char * name)34*795d594fSAndroid Build Coastguard Worker   GCCriticalSection(Thread* self, const char* name)
35*795d594fSAndroid Build Coastguard Worker       : self_(self), section_name_(name) {}
~GCCriticalSection()36*795d594fSAndroid Build Coastguard Worker   ~GCCriticalSection() {}
37*795d594fSAndroid Build Coastguard Worker 
38*795d594fSAndroid Build Coastguard Worker   // Starts a GCCriticalSection. Returns the previous no-suspension reason.
39*795d594fSAndroid Build Coastguard Worker   EXPORT const char* Enter(GcCause cause, CollectorType type) ACQUIRE(Roles::uninterruptible_);
40*795d594fSAndroid Build Coastguard Worker 
41*795d594fSAndroid Build Coastguard Worker   // Ends a GCCriticalSection. Takes the old no-suspension reason.
42*795d594fSAndroid Build Coastguard Worker   EXPORT void Exit(const char* old_reason) RELEASE(Roles::uninterruptible_);
43*795d594fSAndroid Build Coastguard Worker 
44*795d594fSAndroid Build Coastguard Worker  private:
45*795d594fSAndroid Build Coastguard Worker   Thread* const self_;
46*795d594fSAndroid Build Coastguard Worker   const char* section_name_;
47*795d594fSAndroid Build Coastguard Worker };
48*795d594fSAndroid Build Coastguard Worker 
49*795d594fSAndroid Build Coastguard Worker // Wait until the GC is finished and then prevent the GC from starting until the destructor. Used
50*795d594fSAndroid Build Coastguard Worker // to prevent deadlocks in places where we call ClassLinker::VisitClass with all the threads
51*795d594fSAndroid Build Coastguard Worker // suspended.
52*795d594fSAndroid Build Coastguard Worker class ScopedGCCriticalSection {
53*795d594fSAndroid Build Coastguard Worker  public:
54*795d594fSAndroid Build Coastguard Worker   EXPORT ScopedGCCriticalSection(Thread* self, GcCause cause, CollectorType collector_type)
55*795d594fSAndroid Build Coastguard Worker       ACQUIRE(Roles::uninterruptible_);
56*795d594fSAndroid Build Coastguard Worker   EXPORT ~ScopedGCCriticalSection() RELEASE(Roles::uninterruptible_);
57*795d594fSAndroid Build Coastguard Worker 
58*795d594fSAndroid Build Coastguard Worker  private:
59*795d594fSAndroid Build Coastguard Worker   GCCriticalSection critical_section_;
60*795d594fSAndroid Build Coastguard Worker   const char* old_no_suspend_reason_;
61*795d594fSAndroid Build Coastguard Worker };
62*795d594fSAndroid Build Coastguard Worker 
63*795d594fSAndroid Build Coastguard Worker }  // namespace gc
64*795d594fSAndroid Build Coastguard Worker }  // namespace art
65*795d594fSAndroid Build Coastguard Worker 
66*795d594fSAndroid Build Coastguard Worker #endif  // ART_RUNTIME_GC_SCOPED_GC_CRITICAL_SECTION_H_
67