Andreas Gampe | fda5714 | 2016-09-08 20:29:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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_SYSTEM_WEAK_H_ |
| 18 | #define ART_RUNTIME_GC_SYSTEM_WEAK_H_ |
| 19 | |
| 20 | #include "base/mutex.h" |
| 21 | #include "object_callbacks.h" |
| 22 | #include "thread-inl.h" |
| 23 | |
| 24 | namespace art { |
| 25 | namespace gc { |
| 26 | |
| 27 | class AbstractSystemWeakHolder { |
| 28 | public: |
| 29 | virtual ~AbstractSystemWeakHolder() {} |
| 30 | |
| 31 | virtual void Allow() REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
| 32 | virtual void Disallow() REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
| 33 | virtual void Broadcast() REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
| 34 | |
| 35 | virtual void Sweep(IsMarkedVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
| 36 | }; |
| 37 | |
| 38 | class SystemWeakHolder : public AbstractSystemWeakHolder { |
| 39 | public: |
| 40 | explicit SystemWeakHolder(LockLevel level) |
| 41 | : allow_disallow_lock_("SystemWeakHolder", level), |
| 42 | new_weak_condition_("SystemWeakHolder new condition", allow_disallow_lock_), |
| 43 | allow_new_system_weak_(true) { |
| 44 | } |
| 45 | virtual ~SystemWeakHolder() {} |
| 46 | |
| 47 | void Allow() OVERRIDE |
| 48 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 49 | REQUIRES(!allow_disallow_lock_) { |
| 50 | CHECK(!kUseReadBarrier); |
| 51 | MutexLock mu(Thread::Current(), allow_disallow_lock_); |
| 52 | allow_new_system_weak_ = true; |
| 53 | new_weak_condition_.Broadcast(Thread::Current()); |
| 54 | } |
| 55 | |
| 56 | void Disallow() OVERRIDE |
| 57 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 58 | REQUIRES(!allow_disallow_lock_) { |
| 59 | CHECK(!kUseReadBarrier); |
| 60 | MutexLock mu(Thread::Current(), allow_disallow_lock_); |
| 61 | allow_new_system_weak_ = false; |
| 62 | } |
| 63 | |
| 64 | void Broadcast() OVERRIDE |
| 65 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 66 | REQUIRES(!allow_disallow_lock_) { |
| 67 | CHECK(kUseReadBarrier); |
| 68 | MutexLock mu(Thread::Current(), allow_disallow_lock_); |
| 69 | new_weak_condition_.Broadcast(Thread::Current()); |
| 70 | } |
| 71 | |
Andreas Gampe | def4ee6 | 2016-11-07 10:10:21 -0800 | [diff] [blame^] | 72 | // WARNING: For lock annotations only. |
| 73 | Mutex* GetAllowDisallowLock() const RETURN_CAPABILITY(allow_disallow_lock_) { |
| 74 | return nullptr; |
| 75 | } |
| 76 | |
Andreas Gampe | fda5714 | 2016-09-08 20:29:18 -0700 | [diff] [blame] | 77 | protected: |
| 78 | void Wait(Thread* self) REQUIRES_SHARED(allow_disallow_lock_) { |
| 79 | // Wait for GC's sweeping to complete and allow new records |
| 80 | while (UNLIKELY((!kUseReadBarrier && !allow_new_system_weak_) || |
| 81 | (kUseReadBarrier && !self->GetWeakRefAccessEnabled()))) { |
| 82 | new_weak_condition_.WaitHoldingLocks(self); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | Mutex allow_disallow_lock_; |
| 87 | ConditionVariable new_weak_condition_ GUARDED_BY(allow_disallow_lock_); |
| 88 | bool allow_new_system_weak_ GUARDED_BY(allow_disallow_lock_); |
| 89 | }; |
| 90 | |
| 91 | } // namespace gc |
| 92 | } // namespace art |
| 93 | |
| 94 | #endif // ART_RUNTIME_GC_SYSTEM_WEAK_H_ |