blob: 887059b57f78a1e8e797d9009bde60fe0b63aa75 [file] [log] [blame]
Andreas Gampefda57142016-09-08 20:29:18 -07001/*
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
24namespace art {
25namespace gc {
26
27class 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
38class 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 Gampedef4ee62016-11-07 10:10:21 -080072 // WARNING: For lock annotations only.
73 Mutex* GetAllowDisallowLock() const RETURN_CAPABILITY(allow_disallow_lock_) {
74 return nullptr;
75 }
76
Andreas Gampefda57142016-09-08 20:29:18 -070077 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_