1 /*
2  * Copyright (C) 2015 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 
17 #ifndef ART_RUNTIME_GC_SCOPED_GC_CRITICAL_SECTION_H_
18 #define ART_RUNTIME_GC_SCOPED_GC_CRITICAL_SECTION_H_
19 
20 #include "base/locks.h"
21 #include "collector_type.h"
22 #include "gc_cause.h"
23 
24 namespace art {
25 
26 class Thread;
27 
28 namespace gc {
29 
30 // The use of ScopedGCCriticalSection should be preferred whenever possible.
31 class GCCriticalSection {
32  public:
GCCriticalSection(Thread * self,const char * name)33   GCCriticalSection(Thread* self, const char* name)
34       : self_(self), section_name_(name) {}
~GCCriticalSection()35   ~GCCriticalSection() {}
36 
37   // Starts a GCCriticalSection. Returns the previous no-suspension reason.
38   const char* Enter(GcCause cause, CollectorType type) ACQUIRE(Roles::uninterruptible_);
39 
40   // Ends a GCCriticalSection. Takes the old no-suspension reason.
41   void Exit(const char* old_reason) RELEASE(Roles::uninterruptible_);
42 
43  private:
44   Thread* const self_;
45   const char* section_name_;
46 };
47 
48 // Wait until the GC is finished and then prevent the GC from starting until the destructor. Used
49 // to prevent deadlocks in places where we call ClassLinker::VisitClass with all the threads
50 // suspended.
51 class ScopedGCCriticalSection {
52  public:
53   ScopedGCCriticalSection(Thread* self, GcCause cause, CollectorType collector_type)
54       ACQUIRE(Roles::uninterruptible_);
55   ~ScopedGCCriticalSection() RELEASE(Roles::uninterruptible_);
56 
57  private:
58   GCCriticalSection critical_section_;
59   const char* old_no_suspend_reason_;
60 };
61 
62 // The use of ScopedGCCriticalSection should be preferred whenever possible.
63 // This class allows thread suspension but should never be used with allocations because of the
64 // deadlock risk. TODO: Add a new thread role for "no allocations" that still allows suspension.
65 class ScopedInterruptibleGCCriticalSection {
66  public:
67   ScopedInterruptibleGCCriticalSection(Thread* self, GcCause cause, CollectorType type);
68   ~ScopedInterruptibleGCCriticalSection();
69 
70  private:
71   Thread* const self_;
72 };
73 
74 
75 }  // namespace gc
76 }  // namespace art
77 
78 #endif  // ART_RUNTIME_GC_SCOPED_GC_CRITICAL_SECTION_H_
79