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 #include "scoped_gc_critical_section.h"
18
19 #include "gc/collector_type.h"
20 #include "gc/heap.h"
21 #include "runtime.h"
22 #include "thread-current-inl.h"
23
24 namespace art {
25 namespace gc {
26
Enter(GcCause cause,CollectorType type)27 const char* GCCriticalSection::Enter(GcCause cause, CollectorType type) {
28 Runtime::Current()->GetHeap()->StartGC(self_, cause, type);
29 if (self_ != nullptr) {
30 return self_->StartAssertNoThreadSuspension(section_name_);
31 } else {
32 // Workaround to avoid having to mark the whole function as NO_THREAD_SAFETY_ANALYSIS.
33 auto kludge = []() ACQUIRE(Roles::uninterruptible_) NO_THREAD_SAFETY_ANALYSIS {};
34 kludge();
35 return nullptr;
36 }
37 }
38
Exit(const char * old_cause)39 void GCCriticalSection::Exit(const char* old_cause) {
40 if (self_ != nullptr) {
41 self_->EndAssertNoThreadSuspension(old_cause);
42 } else {
43 // Workaround to avoid having to mark the whole function as NO_THREAD_SAFETY_ANALYSIS.
44 auto kludge = []() RELEASE(Roles::uninterruptible_) NO_THREAD_SAFETY_ANALYSIS {};
45 kludge();
46 }
47 Runtime::Current()->GetHeap()->FinishGC(self_, collector::kGcTypeNone);
48 }
49
ScopedGCCriticalSection(Thread * self,GcCause cause,CollectorType collector_type)50 ScopedGCCriticalSection::ScopedGCCriticalSection(Thread* self,
51 GcCause cause,
52 CollectorType collector_type)
53 : critical_section_(self, "ScopedGCCriticalSection") {
54 old_no_suspend_reason_ = critical_section_.Enter(cause, collector_type);
55 }
56
~ScopedGCCriticalSection()57 ScopedGCCriticalSection::~ScopedGCCriticalSection() {
58 critical_section_.Exit(old_no_suspend_reason_);
59 }
60
ScopedInterruptibleGCCriticalSection(Thread * self,GcCause cause,CollectorType type)61 ScopedInterruptibleGCCriticalSection::ScopedInterruptibleGCCriticalSection(
62 Thread* self,
63 GcCause cause,
64 CollectorType type) : self_(self) {
65 DCHECK(self != nullptr);
66 Runtime::Current()->GetHeap()->StartGC(self_, cause, type);
67 }
68
~ScopedInterruptibleGCCriticalSection()69 ScopedInterruptibleGCCriticalSection::~ScopedInterruptibleGCCriticalSection() {
70 Runtime::Current()->GetHeap()->FinishGC(self_, collector::kGcTypeNone);
71 }
72
73 } // namespace gc
74 } // namespace art
75