Mathieu Chartier | 78f7b4c | 2014-05-06 10:57:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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_REFERENCE_PROCESSOR_H_ |
| 18 | #define ART_RUNTIME_GC_REFERENCE_PROCESSOR_H_ |
| 19 | |
| 20 | #include "base/mutex.h" |
| 21 | #include "globals.h" |
| 22 | #include "jni.h" |
| 23 | #include "object_callbacks.h" |
| 24 | #include "reference_queue.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | class TimingLogger; |
| 29 | |
| 30 | namespace mirror { |
| 31 | class Object; |
| 32 | class Reference; |
| 33 | } // namespace mirror |
| 34 | |
| 35 | namespace gc { |
| 36 | |
| 37 | class Heap; |
| 38 | |
| 39 | // Used to process java.lang.References concurrently or paused. |
| 40 | class ReferenceProcessor { |
| 41 | public: |
| 42 | explicit ReferenceProcessor(); |
| 43 | static mirror::Object* PreserveSoftReferenceCallback(mirror::Object* obj, void* arg); |
| 44 | void ProcessReferences(bool concurrent, TimingLogger* timings, bool clear_soft_references, |
| 45 | IsMarkedCallback* is_marked_callback, |
| 46 | MarkObjectCallback* mark_object_callback, |
| 47 | ProcessMarkStackCallback* process_mark_stack_callback, void* arg) |
| 48 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 49 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) |
| 50 | LOCKS_EXCLUDED(lock_); |
| 51 | // Only allow setting this with mutators suspended so that we can avoid using a lock in the |
| 52 | // GetReferent fast path as an optimization. |
| 53 | void EnableSlowPath() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 54 | // Decode the referent, may block if references are being processed. |
| 55 | mirror::Object* GetReferent(Thread* self, mirror::Reference* reference) |
| 56 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) LOCKS_EXCLUDED(lock_); |
| 57 | void EnqueueClearedReferences() LOCKS_EXCLUDED(Locks::mutator_lock_); |
| 58 | void DelayReferenceReferent(mirror::Class* klass, mirror::Reference* ref, |
| 59 | IsMarkedCallback is_marked_callback, void* arg) |
| 60 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 61 | |
| 62 | private: |
| 63 | class ProcessReferencesArgs { |
| 64 | public: |
| 65 | ProcessReferencesArgs(IsMarkedCallback* is_marked_callback, |
| 66 | MarkObjectCallback* mark_callback, void* arg) |
| 67 | : is_marked_callback_(is_marked_callback), mark_callback_(mark_callback), arg_(arg) { |
| 68 | } |
| 69 | |
| 70 | // The is marked callback is null when the args aren't set up. |
| 71 | IsMarkedCallback* is_marked_callback_; |
| 72 | MarkObjectCallback* mark_callback_; |
| 73 | void* arg_; |
| 74 | }; |
| 75 | // Called by ProcessReferences. |
| 76 | void DisableSlowPath(Thread* self) EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 77 | // If we are preserving references it means that some dead objects may become live, we use start |
| 78 | // and stop preserving to block mutators using GetReferrent from getting access to these |
| 79 | // referents. |
| 80 | void StartPreservingReferences(Thread* self) LOCKS_EXCLUDED(lock_); |
| 81 | void StopPreservingReferences(Thread* self) LOCKS_EXCLUDED(lock_); |
| 82 | // Process args, used by the GetReferent to return referents which are already marked. |
| 83 | ProcessReferencesArgs process_references_args_ GUARDED_BY(lock_); |
| 84 | // Boolean for whether or not we need to go slow path in GetReferent. |
| 85 | volatile bool slow_path_enabled_; |
| 86 | // Boolean for whether or not we are preserving references (either soft references or finalizers). |
| 87 | // If this is true, then we cannot return a referent (see comment in GetReferent). |
| 88 | bool preserving_references_ GUARDED_BY(lock_); |
| 89 | // Lock that guards the reference processing. |
| 90 | Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 91 | // Condition that people wait on if they attempt to get the referent of a reference while |
| 92 | // processing is in progress. |
| 93 | ConditionVariable condition_ GUARDED_BY(lock_); |
| 94 | // Reference queues used by the GC. |
| 95 | ReferenceQueue soft_reference_queue_; |
| 96 | ReferenceQueue weak_reference_queue_; |
| 97 | ReferenceQueue finalizer_reference_queue_; |
| 98 | ReferenceQueue phantom_reference_queue_; |
| 99 | ReferenceQueue cleared_references_; |
| 100 | }; |
| 101 | |
| 102 | } // namespace gc |
| 103 | } // namespace art |
| 104 | |
| 105 | #endif // ART_RUNTIME_GC_REFERENCE_PROCESSOR_H_ |