blob: 60105f4e4f542fe8f63cb97582c3206fecf66346 [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;
Hiroshi Yamauchi30493242016-11-03 13:06:52 -070033 // See Runtime::BroadcastForNewSystemWeaks for the broadcast_for_checkpoint definition.
34 virtual void Broadcast(bool broadcast_for_checkpoint) = 0;
Andreas Gampefda57142016-09-08 20:29:18 -070035
36 virtual void Sweep(IsMarkedVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_) = 0;
37};
38
39class 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 Yamauchi30493242016-11-03 13:06:52 -070065 void Broadcast(bool broadcast_for_checkpoint ATTRIBUTE_UNUSED) OVERRIDE
Andreas Gampefda57142016-09-08 20:29:18 -070066 REQUIRES(!allow_disallow_lock_) {
Andreas Gampefda57142016-09-08 20:29:18 -070067 MutexLock mu(Thread::Current(), allow_disallow_lock_);
68 new_weak_condition_.Broadcast(Thread::Current());
69 }
70
Andreas Gampedef4ee62016-11-07 10:10:21 -080071 // WARNING: For lock annotations only.
72 Mutex* GetAllowDisallowLock() const RETURN_CAPABILITY(allow_disallow_lock_) {
73 return nullptr;
74 }
75
Andreas Gampefda57142016-09-08 20:29:18 -070076 protected:
Hiroshi Yamauchi30493242016-11-03 13:06:52 -070077 void Wait(Thread* self)
78 REQUIRES_SHARED(Locks::mutator_lock_)
79 REQUIRES(allow_disallow_lock_) {
Andreas Gampefda57142016-09-08 20:29:18 -070080 // Wait for GC's sweeping to complete and allow new records
81 while (UNLIKELY((!kUseReadBarrier && !allow_new_system_weak_) ||
82 (kUseReadBarrier && !self->GetWeakRefAccessEnabled()))) {
Hiroshi Yamauchi30493242016-11-03 13:06:52 -070083 // 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 Yamauchia2224042017-02-08 16:35:45 -080085 self->CheckEmptyCheckpointFromWeakRefAccess(&allow_disallow_lock_);
Andreas Gampefda57142016-09-08 20:29:18 -070086 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_