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 "base/macros.h" 22 #include "collector_type.h" 23 #include "gc_cause.h" 24 25 namespace art HIDDEN { 26 27 class Thread; 28 29 namespace gc { 30 31 // The use of ScopedGCCriticalSection should be preferred whenever possible. 32 class GCCriticalSection { 33 public: GCCriticalSection(Thread * self,const char * name)34 GCCriticalSection(Thread* self, const char* name) 35 : self_(self), section_name_(name) {} ~GCCriticalSection()36 ~GCCriticalSection() {} 37 38 // Starts a GCCriticalSection. Returns the previous no-suspension reason. 39 EXPORT const char* Enter(GcCause cause, CollectorType type) ACQUIRE(Roles::uninterruptible_); 40 41 // Ends a GCCriticalSection. Takes the old no-suspension reason. 42 EXPORT void Exit(const char* old_reason) RELEASE(Roles::uninterruptible_); 43 44 private: 45 Thread* const self_; 46 const char* section_name_; 47 }; 48 49 // Wait until the GC is finished and then prevent the GC from starting until the destructor. Used 50 // to prevent deadlocks in places where we call ClassLinker::VisitClass with all the threads 51 // suspended. 52 class ScopedGCCriticalSection { 53 public: 54 EXPORT ScopedGCCriticalSection(Thread* self, GcCause cause, CollectorType collector_type) 55 ACQUIRE(Roles::uninterruptible_); 56 EXPORT ~ScopedGCCriticalSection() RELEASE(Roles::uninterruptible_); 57 58 private: 59 GCCriticalSection critical_section_; 60 const char* old_no_suspend_reason_; 61 }; 62 63 } // namespace gc 64 } // namespace art 65 66 #endif // ART_RUNTIME_GC_SCOPED_GC_CRITICAL_SECTION_H_ 67