Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 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" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 22 | #include "thread-current-inl.h" |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | namespace gc { |
| 26 | |
Alex Light | 3b8aa77 | 2018-08-13 15:55:44 -0700 | [diff] [blame] | 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 | |
| 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 | |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 50 | ScopedGCCriticalSection::ScopedGCCriticalSection(Thread* self, |
| 51 | GcCause cause, |
| 52 | CollectorType collector_type) |
Alex Light | 3b8aa77 | 2018-08-13 15:55:44 -0700 | [diff] [blame] | 53 | : critical_section_(self, "ScopedGCCriticalSection") { |
| 54 | old_no_suspend_reason_ = critical_section_.Enter(cause, collector_type); |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 55 | } |
Alex Light | 3b8aa77 | 2018-08-13 15:55:44 -0700 | [diff] [blame] | 56 | |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 57 | ScopedGCCriticalSection::~ScopedGCCriticalSection() { |
Alex Light | 3b8aa77 | 2018-08-13 15:55:44 -0700 | [diff] [blame] | 58 | critical_section_.Exit(old_no_suspend_reason_); |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Mathieu Chartier | ad390fa | 2019-10-16 20:03:00 -0700 | [diff] [blame] | 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 | |
| 69 | ScopedInterruptibleGCCriticalSection::~ScopedInterruptibleGCCriticalSection() { |
| 70 | Runtime::Current()->GetHeap()->FinishGC(self_, collector::kGcTypeNone); |
| 71 | } |
| 72 | |
Mathieu Chartier | aa51682 | 2015-10-02 15:53:37 -0700 | [diff] [blame] | 73 | } // namespace gc |
| 74 | } // namespace art |