OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/store_buffer.h" | 5 #include "vm/store_buffer.h" |
6 | 6 |
7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
8 #include "vm/lockers.h" | 8 #include "vm/lockers.h" |
9 #include "vm/runtime_entry.h" | 9 #include "vm/runtime_entry.h" |
10 | 10 |
11 namespace dart { | 11 namespace dart { |
12 | 12 |
13 DEFINE_LEAF_RUNTIME_ENTRY(void, StoreBufferBlockProcess, 1, Thread* thread) { | 13 DEFINE_LEAF_RUNTIME_ENTRY(void, StoreBufferBlockProcess, 1, Thread* thread) { |
14 thread->StoreBufferBlockProcess(StoreBuffer::kCheckThreshold); | 14 thread->StoreBufferBlockProcess(StoreBuffer::kCheckThreshold); |
15 } | 15 } |
16 END_LEAF_RUNTIME_ENTRY | 16 END_LEAF_RUNTIME_ENTRY |
17 | 17 |
18 template <int BlockSize> | 18 template <int BlockSize> |
19 typename BlockStack<BlockSize>::List* BlockStack<BlockSize>::global_empty_ = | 19 typename BlockStack<BlockSize>::List* BlockStack<BlockSize>::global_empty_ = |
20 NULL; | 20 NULL; |
21 template <int BlockSize> | 21 template <int BlockSize> |
22 Mutex* BlockStack<BlockSize>::global_mutex_ = NULL; | 22 Mutex* BlockStack<BlockSize>::global_mutex_ = NULL; |
23 | 23 |
24 | |
25 template <int BlockSize> | 24 template <int BlockSize> |
26 void BlockStack<BlockSize>::InitOnce() { | 25 void BlockStack<BlockSize>::InitOnce() { |
27 global_empty_ = new List(); | 26 global_empty_ = new List(); |
28 global_mutex_ = new Mutex(); | 27 global_mutex_ = new Mutex(); |
29 } | 28 } |
30 | 29 |
31 | |
32 template <int BlockSize> | 30 template <int BlockSize> |
33 void BlockStack<BlockSize>::ShutDown() { | 31 void BlockStack<BlockSize>::ShutDown() { |
34 delete global_empty_; | 32 delete global_empty_; |
35 delete global_mutex_; | 33 delete global_mutex_; |
36 } | 34 } |
37 | 35 |
38 | |
39 template <int BlockSize> | 36 template <int BlockSize> |
40 BlockStack<BlockSize>::BlockStack() : mutex_(new Mutex()) {} | 37 BlockStack<BlockSize>::BlockStack() : mutex_(new Mutex()) {} |
41 | 38 |
42 | |
43 template <int BlockSize> | 39 template <int BlockSize> |
44 BlockStack<BlockSize>::~BlockStack() { | 40 BlockStack<BlockSize>::~BlockStack() { |
45 Reset(); | 41 Reset(); |
46 delete mutex_; | 42 delete mutex_; |
47 } | 43 } |
48 | 44 |
49 | |
50 template <int BlockSize> | 45 template <int BlockSize> |
51 void BlockStack<BlockSize>::Reset() { | 46 void BlockStack<BlockSize>::Reset() { |
52 MutexLocker local_mutex_locker(mutex_); | 47 MutexLocker local_mutex_locker(mutex_); |
53 { | 48 { |
54 // Empty all blocks and move them to the global cache. | 49 // Empty all blocks and move them to the global cache. |
55 MutexLocker global_mutex_locker(global_mutex_); | 50 MutexLocker global_mutex_locker(global_mutex_); |
56 while (!full_.IsEmpty()) { | 51 while (!full_.IsEmpty()) { |
57 Block* block = full_.Pop(); | 52 Block* block = full_.Pop(); |
58 block->Reset(); | 53 block->Reset(); |
59 global_empty_->Push(block); | 54 global_empty_->Push(block); |
60 } | 55 } |
61 while (!partial_.IsEmpty()) { | 56 while (!partial_.IsEmpty()) { |
62 Block* block = partial_.Pop(); | 57 Block* block = partial_.Pop(); |
63 block->Reset(); | 58 block->Reset(); |
64 global_empty_->Push(block); | 59 global_empty_->Push(block); |
65 } | 60 } |
66 TrimGlobalEmpty(); | 61 TrimGlobalEmpty(); |
67 } | 62 } |
68 } | 63 } |
69 | 64 |
70 | |
71 template <int BlockSize> | 65 template <int BlockSize> |
72 typename BlockStack<BlockSize>::Block* BlockStack<BlockSize>::Blocks() { | 66 typename BlockStack<BlockSize>::Block* BlockStack<BlockSize>::Blocks() { |
73 MutexLocker ml(mutex_); | 67 MutexLocker ml(mutex_); |
74 while (!partial_.IsEmpty()) { | 68 while (!partial_.IsEmpty()) { |
75 full_.Push(partial_.Pop()); | 69 full_.Push(partial_.Pop()); |
76 } | 70 } |
77 return full_.PopAll(); | 71 return full_.PopAll(); |
78 } | 72 } |
79 | 73 |
80 | |
81 template <int BlockSize> | 74 template <int BlockSize> |
82 void BlockStack<BlockSize>::PushBlockImpl(Block* block) { | 75 void BlockStack<BlockSize>::PushBlockImpl(Block* block) { |
83 ASSERT(block->next() == NULL); // Should be just a single block. | 76 ASSERT(block->next() == NULL); // Should be just a single block. |
84 if (block->IsFull()) { | 77 if (block->IsFull()) { |
85 MutexLocker ml(mutex_); | 78 MutexLocker ml(mutex_); |
86 full_.Push(block); | 79 full_.Push(block); |
87 } else if (block->IsEmpty()) { | 80 } else if (block->IsEmpty()) { |
88 MutexLocker ml(global_mutex_); | 81 MutexLocker ml(global_mutex_); |
89 global_empty_->Push(block); | 82 global_empty_->Push(block); |
90 TrimGlobalEmpty(); | 83 TrimGlobalEmpty(); |
91 } else { | 84 } else { |
92 MutexLocker ml(mutex_); | 85 MutexLocker ml(mutex_); |
93 partial_.Push(block); | 86 partial_.Push(block); |
94 } | 87 } |
95 } | 88 } |
96 | 89 |
97 | |
98 void StoreBuffer::PushBlock(Block* block, ThresholdPolicy policy) { | 90 void StoreBuffer::PushBlock(Block* block, ThresholdPolicy policy) { |
99 BlockStack<Block::kSize>::PushBlockImpl(block); | 91 BlockStack<Block::kSize>::PushBlockImpl(block); |
100 if ((policy == kCheckThreshold) && Overflowed()) { | 92 if ((policy == kCheckThreshold) && Overflowed()) { |
101 MutexLocker ml(mutex_); | 93 MutexLocker ml(mutex_); |
102 Thread* thread = Thread::Current(); | 94 Thread* thread = Thread::Current(); |
103 // Sanity check: it makes no sense to schedule the GC in another isolate. | 95 // Sanity check: it makes no sense to schedule the GC in another isolate. |
104 // (If Isolate ever gets multiple store buffers, we should avoid this | 96 // (If Isolate ever gets multiple store buffers, we should avoid this |
105 // coupling by passing in an explicit callback+parameter at construction.) | 97 // coupling by passing in an explicit callback+parameter at construction.) |
106 ASSERT(thread->isolate()->store_buffer() == this); | 98 ASSERT(thread->isolate()->store_buffer() == this); |
107 thread->ScheduleInterrupts(Thread::kVMInterrupt); | 99 thread->ScheduleInterrupts(Thread::kVMInterrupt); |
108 } | 100 } |
109 } | 101 } |
110 | 102 |
111 | |
112 template <int BlockSize> | 103 template <int BlockSize> |
113 typename BlockStack<BlockSize>::Block* | 104 typename BlockStack<BlockSize>::Block* |
114 BlockStack<BlockSize>::PopNonFullBlock() { | 105 BlockStack<BlockSize>::PopNonFullBlock() { |
115 { | 106 { |
116 MutexLocker ml(mutex_); | 107 MutexLocker ml(mutex_); |
117 if (!partial_.IsEmpty()) { | 108 if (!partial_.IsEmpty()) { |
118 return partial_.Pop(); | 109 return partial_.Pop(); |
119 } | 110 } |
120 } | 111 } |
121 return PopEmptyBlock(); | 112 return PopEmptyBlock(); |
122 } | 113 } |
123 | 114 |
124 | |
125 template <int BlockSize> | 115 template <int BlockSize> |
126 typename BlockStack<BlockSize>::Block* BlockStack<BlockSize>::PopEmptyBlock() { | 116 typename BlockStack<BlockSize>::Block* BlockStack<BlockSize>::PopEmptyBlock() { |
127 { | 117 { |
128 MutexLocker ml(global_mutex_); | 118 MutexLocker ml(global_mutex_); |
129 if (!global_empty_->IsEmpty()) { | 119 if (!global_empty_->IsEmpty()) { |
130 return global_empty_->Pop(); | 120 return global_empty_->Pop(); |
131 } | 121 } |
132 } | 122 } |
133 return new Block(); | 123 return new Block(); |
134 } | 124 } |
135 | 125 |
136 | |
137 template <int BlockSize> | 126 template <int BlockSize> |
138 typename BlockStack<BlockSize>::Block* | 127 typename BlockStack<BlockSize>::Block* |
139 BlockStack<BlockSize>::PopNonEmptyBlock() { | 128 BlockStack<BlockSize>::PopNonEmptyBlock() { |
140 MutexLocker ml(mutex_); | 129 MutexLocker ml(mutex_); |
141 if (!full_.IsEmpty()) { | 130 if (!full_.IsEmpty()) { |
142 return full_.Pop(); | 131 return full_.Pop(); |
143 } else if (!partial_.IsEmpty()) { | 132 } else if (!partial_.IsEmpty()) { |
144 return partial_.Pop(); | 133 return partial_.Pop(); |
145 } else { | 134 } else { |
146 return NULL; | 135 return NULL; |
147 } | 136 } |
148 } | 137 } |
149 | 138 |
150 | |
151 template <int BlockSize> | 139 template <int BlockSize> |
152 bool BlockStack<BlockSize>::IsEmpty() { | 140 bool BlockStack<BlockSize>::IsEmpty() { |
153 MutexLocker ml(mutex_); | 141 MutexLocker ml(mutex_); |
154 return full_.IsEmpty() && partial_.IsEmpty(); | 142 return full_.IsEmpty() && partial_.IsEmpty(); |
155 } | 143 } |
156 | 144 |
157 | |
158 template <int BlockSize> | 145 template <int BlockSize> |
159 BlockStack<BlockSize>::List::~List() { | 146 BlockStack<BlockSize>::List::~List() { |
160 while (!IsEmpty()) { | 147 while (!IsEmpty()) { |
161 delete Pop(); | 148 delete Pop(); |
162 } | 149 } |
163 } | 150 } |
164 | 151 |
165 | |
166 template <int BlockSize> | 152 template <int BlockSize> |
167 typename BlockStack<BlockSize>::Block* BlockStack<BlockSize>::List::Pop() { | 153 typename BlockStack<BlockSize>::Block* BlockStack<BlockSize>::List::Pop() { |
168 Block* result = head_; | 154 Block* result = head_; |
169 head_ = head_->next_; | 155 head_ = head_->next_; |
170 --length_; | 156 --length_; |
171 result->next_ = NULL; | 157 result->next_ = NULL; |
172 return result; | 158 return result; |
173 } | 159 } |
174 | 160 |
175 | |
176 template <int BlockSize> | 161 template <int BlockSize> |
177 typename BlockStack<BlockSize>::Block* BlockStack<BlockSize>::List::PopAll() { | 162 typename BlockStack<BlockSize>::Block* BlockStack<BlockSize>::List::PopAll() { |
178 Block* result = head_; | 163 Block* result = head_; |
179 head_ = NULL; | 164 head_ = NULL; |
180 length_ = 0; | 165 length_ = 0; |
181 return result; | 166 return result; |
182 } | 167 } |
183 | 168 |
184 | |
185 template <int BlockSize> | 169 template <int BlockSize> |
186 void BlockStack<BlockSize>::List::Push(Block* block) { | 170 void BlockStack<BlockSize>::List::Push(Block* block) { |
187 ASSERT(block->next_ == NULL); | 171 ASSERT(block->next_ == NULL); |
188 block->next_ = head_; | 172 block->next_ = head_; |
189 head_ = block; | 173 head_ = block; |
190 ++length_; | 174 ++length_; |
191 } | 175 } |
192 | 176 |
193 | |
194 bool StoreBuffer::Overflowed() { | 177 bool StoreBuffer::Overflowed() { |
195 MutexLocker ml(mutex_); | 178 MutexLocker ml(mutex_); |
196 return (full_.length() + partial_.length()) > kMaxNonEmpty; | 179 return (full_.length() + partial_.length()) > kMaxNonEmpty; |
197 } | 180 } |
198 | 181 |
199 | |
200 template <int BlockSize> | 182 template <int BlockSize> |
201 void BlockStack<BlockSize>::TrimGlobalEmpty() { | 183 void BlockStack<BlockSize>::TrimGlobalEmpty() { |
202 DEBUG_ASSERT(global_mutex_->IsOwnedByCurrentThread()); | 184 DEBUG_ASSERT(global_mutex_->IsOwnedByCurrentThread()); |
203 while (global_empty_->length() > kMaxGlobalEmpty) { | 185 while (global_empty_->length() > kMaxGlobalEmpty) { |
204 delete global_empty_->Pop(); | 186 delete global_empty_->Pop(); |
205 } | 187 } |
206 } | 188 } |
207 | 189 |
208 | |
209 template class BlockStack<kStoreBufferBlockSize>; | 190 template class BlockStack<kStoreBufferBlockSize>; |
210 template class BlockStack<kMarkingStackBlockSize>; | 191 template class BlockStack<kMarkingStackBlockSize>; |
211 | 192 |
212 } // namespace dart | 193 } // namespace dart |
OLD | NEW |