1aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier/*
2aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier * Copyright (C) 2015 The Android Open Source Project
3aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier *
4aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier * Licensed under the Apache License, Version 2.0 (the "License");
5aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier * you may not use this file except in compliance with the License.
6aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier * You may obtain a copy of the License at
7aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier *
8aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier *      http://www.apache.org/licenses/LICENSE-2.0
9aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier *
10aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier * Unless required by applicable law or agreed to in writing, software
11aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier * distributed under the License is distributed on an "AS IS" BASIS,
12aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier * See the License for the specific language governing permissions and
14aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier * limitations under the License.
15aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier */
16aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier
17aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier#ifndef ART_RUNTIME_GC_SCOPED_GC_CRITICAL_SECTION_H_
18aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier#define ART_RUNTIME_GC_SCOPED_GC_CRITICAL_SECTION_H_
19aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier
20aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier#include "base/mutex.h"
21aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier#include "collector_type.h"
22aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier#include "gc_cause.h"
23aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier
24aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartiernamespace art {
25aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier
26aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartierclass Thread;
27aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier
28aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartiernamespace gc {
29aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier
30aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier// Wait until the GC is finished and then prevent GC from starting until the destructor. Used
31aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier// to prevent deadlocks in places where we call ClassLinker::VisitClass with all th threads
32aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier// suspended.
33aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartierclass ScopedGCCriticalSection {
34aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier public:
35aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier  ScopedGCCriticalSection(Thread* self, GcCause cause, CollectorType collector_type)
36aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier      ACQUIRE(Roles::uninterruptible_);
37aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier  ~ScopedGCCriticalSection() RELEASE(Roles::uninterruptible_);
38aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier
39aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier private:
40aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier  Thread* const self_;
41aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier  const char* old_cause_;
42aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier};
43aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier
44aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier}  // namespace gc
45aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier}  // namespace art
46aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier
47aa5168291c46f9b418d989bccf2d8e09338a83e6Mathieu Chartier#endif  // ART_RUNTIME_GC_SCOPED_GC_CRITICAL_SECTION_H_
48