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; |
Hiroshi Yamauchi | 3049324 | 2016-11-03 13:06:52 -0700 | [diff] [blame] | 33 | // See Runtime::BroadcastForNewSystemWeaks for the broadcast_for_checkpoint definition. |
| 34 | virtual void Broadcast(bool broadcast_for_checkpoint) = 0; |
Andreas Gampe | fda5714 | 2016-09-08 20:29:18 -0700 | [diff] [blame] | 35 | |
| 36 | virtual void Sweep(IsMarkedVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
| 37 | }; |
| 38 | |
| 39 | class SystemWeakHolder : public AbstractSystemWeakHolder { |
| 40 | public: |
| 41 | explicit SystemWeakHolder(LockLevel level) |
| 42 | : allow_disallow_lock_("SystemWeakHolder", level), |
| 43 | new_weak_condition_("SystemWeakHolder new condition", allow_disallow_lock_), |
| 44 | allow_new_system_weak_(true) { |
| 45 | } |
| 46 | virtual ~SystemWeakHolder() {} |
| 47 | |
| 48 | void Allow() OVERRIDE |
| 49 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 50 | REQUIRES(!allow_disallow_lock_) { |
| 51 | CHECK(!kUseReadBarrier); |
| 52 | MutexLock mu(Thread::Current(), allow_disallow_lock_); |
| 53 | allow_new_system_weak_ = true; |
| 54 | new_weak_condition_.Broadcast(Thread::Current()); |
| 55 | } |
| 56 | |
| 57 | void Disallow() OVERRIDE |
| 58 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 59 | REQUIRES(!allow_disallow_lock_) { |
| 60 | CHECK(!kUseReadBarrier); |
| 61 | MutexLock mu(Thread::Current(), allow_disallow_lock_); |
| 62 | allow_new_system_weak_ = false; |
| 63 | } |
| 64 | |
Hiroshi Yamauchi | 3049324 | 2016-11-03 13:06:52 -0700 | [diff] [blame] | 65 | void Broadcast(bool broadcast_for_checkpoint ATTRIBUTE_UNUSED) OVERRIDE |
Andreas Gampe | fda5714 | 2016-09-08 20:29:18 -0700 | [diff] [blame] | 66 | REQUIRES(!allow_disallow_lock_) { |
Andreas Gampe | fda5714 | 2016-09-08 20:29:18 -0700 | [diff] [blame] | 67 | MutexLock mu(Thread::Current(), allow_disallow_lock_); |
| 68 | new_weak_condition_.Broadcast(Thread::Current()); |
| 69 | } |
| 70 | |
Andreas Gampe | def4ee6 | 2016-11-07 10:10:21 -0800 | [diff] [blame] | 71 | // WARNING: For lock annotations only. |
| 72 | Mutex* GetAllowDisallowLock() const RETURN_CAPABILITY(allow_disallow_lock_) { |
| 73 | return nullptr; |
| 74 | } |
| 75 | |
Andreas Gampe | fda5714 | 2016-09-08 20:29:18 -0700 | [diff] [blame] | 76 | protected: |
Hiroshi Yamauchi | 3049324 | 2016-11-03 13:06:52 -0700 | [diff] [blame] | 77 | void Wait(Thread* self) |
| 78 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 79 | REQUIRES(allow_disallow_lock_) { |
Andreas Gampe | fda5714 | 2016-09-08 20:29:18 -0700 | [diff] [blame] | 80 | // Wait for GC's sweeping to complete and allow new records |
| 81 | while (UNLIKELY((!kUseReadBarrier && !allow_new_system_weak_) || |
| 82 | (kUseReadBarrier && !self->GetWeakRefAccessEnabled()))) { |
Hiroshi Yamauchi | 3049324 | 2016-11-03 13:06:52 -0700 | [diff] [blame] | 83 | // Check and run the empty checkpoint before blocking so the empty checkpoint will work in the |
| 84 | // presence of threads blocking for weak ref access. |
Hiroshi Yamauchi | a222404 | 2017-02-08 16:35:45 -0800 | [diff] [blame^] | 85 | self->CheckEmptyCheckpointFromWeakRefAccess(&allow_disallow_lock_); |
Andreas Gampe | fda5714 | 2016-09-08 20:29:18 -0700 | [diff] [blame] | 86 | new_weak_condition_.WaitHoldingLocks(self); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | Mutex allow_disallow_lock_; |
| 91 | ConditionVariable new_weak_condition_ GUARDED_BY(allow_disallow_lock_); |
| 92 | bool allow_new_system_weak_ GUARDED_BY(allow_disallow_lock_); |
| 93 | }; |
| 94 | |
| 95 | } // namespace gc |
| 96 | } // namespace art |
| 97 | |
| 98 | #endif // ART_RUNTIME_GC_SYSTEM_WEAK_H_ |