blob: eaede43e79cc7008c604380f9bb945e5fad1c171 [file] [log] [blame]
Mathieu Chartieraa516822015-10-02 15:53:37 -07001/*
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 Gampeb486a982017-06-01 13:45:54 -070022#include "thread-current-inl.h"
Mathieu Chartieraa516822015-10-02 15:53:37 -070023
24namespace art {
25namespace gc {
26
Alex Light3b8aa772018-08-13 15:55:44 -070027const 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
39void 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 Chartieraa516822015-10-02 15:53:37 -070050ScopedGCCriticalSection::ScopedGCCriticalSection(Thread* self,
51 GcCause cause,
52 CollectorType collector_type)
Alex Light3b8aa772018-08-13 15:55:44 -070053 : critical_section_(self, "ScopedGCCriticalSection") {
54 old_no_suspend_reason_ = critical_section_.Enter(cause, collector_type);
Mathieu Chartieraa516822015-10-02 15:53:37 -070055}
Alex Light3b8aa772018-08-13 15:55:44 -070056
Mathieu Chartieraa516822015-10-02 15:53:37 -070057ScopedGCCriticalSection::~ScopedGCCriticalSection() {
Alex Light3b8aa772018-08-13 15:55:44 -070058 critical_section_.Exit(old_no_suspend_reason_);
Mathieu Chartieraa516822015-10-02 15:53:37 -070059}
60
Mathieu Chartierad390fa2019-10-16 20:03:00 -070061ScopedInterruptibleGCCriticalSection::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
69ScopedInterruptibleGCCriticalSection::~ScopedInterruptibleGCCriticalSection() {
70 Runtime::Current()->GetHeap()->FinishGC(self_, collector::kGcTypeNone);
71}
72
Mathieu Chartieraa516822015-10-02 15:53:37 -070073} // namespace gc
74} // namespace art