author | twisti |
Fri, 27 Feb 2009 13:27:09 -0800 | |
changeset 2131 | 98f9cef66a34 |
parent 1623 | a0dd9009e992 |
child 2142 | 032f4652700c |
permissions | -rw-r--r-- |
1374 | 1 |
/* |
1623 | 2 |
* Copyright 2001-2008 Sun Microsystems, Inc. All Rights Reserved. |
1374 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 |
* have any questions. |
|
22 |
* |
|
23 |
*/ |
|
24 |
||
25 |
# include "incls/_precompiled.incl" |
|
26 |
# include "incls/_ptrQueue.cpp.incl" |
|
27 |
||
28 |
PtrQueue::PtrQueue(PtrQueueSet* qset_, bool perm) : |
|
29 |
_qset(qset_), _buf(NULL), _index(0), _active(false), |
|
30 |
_perm(perm), _lock(NULL) |
|
31 |
{} |
|
32 |
||
1560
1b328492b7f8
6770608: G1: Mutator thread can flush barrier and satb queues during safepoint
iveresov
parents:
1374
diff
changeset
|
33 |
void PtrQueue::flush() { |
1374 | 34 |
if (!_perm && _buf != NULL) { |
35 |
if (_index == _sz) { |
|
36 |
// No work to do. |
|
37 |
qset()->deallocate_buffer(_buf); |
|
38 |
} else { |
|
39 |
// We must NULL out the unused entries, then enqueue. |
|
40 |
for (size_t i = 0; i < _index; i += oopSize) { |
|
41 |
_buf[byte_index_to_index((int)i)] = NULL; |
|
42 |
} |
|
43 |
qset()->enqueue_complete_buffer(_buf); |
|
44 |
} |
|
1560
1b328492b7f8
6770608: G1: Mutator thread can flush barrier and satb queues during safepoint
iveresov
parents:
1374
diff
changeset
|
45 |
_buf = NULL; |
1b328492b7f8
6770608: G1: Mutator thread can flush barrier and satb queues during safepoint
iveresov
parents:
1374
diff
changeset
|
46 |
_index = 0; |
1374 | 47 |
} |
48 |
} |
|
49 |
||
50 |
||
51 |
static int byte_index_to_index(int ind) { |
|
52 |
assert((ind % oopSize) == 0, "Invariant."); |
|
53 |
return ind / oopSize; |
|
54 |
} |
|
55 |
||
56 |
static int index_to_byte_index(int byte_ind) { |
|
57 |
return byte_ind * oopSize; |
|
58 |
} |
|
59 |
||
60 |
void PtrQueue::enqueue_known_active(void* ptr) { |
|
61 |
assert(0 <= _index && _index <= _sz, "Invariant."); |
|
62 |
assert(_index == 0 || _buf != NULL, "invariant"); |
|
63 |
||
64 |
while (_index == 0) { |
|
65 |
handle_zero_index(); |
|
66 |
} |
|
67 |
assert(_index > 0, "postcondition"); |
|
68 |
||
69 |
_index -= oopSize; |
|
70 |
_buf[byte_index_to_index((int)_index)] = ptr; |
|
71 |
assert(0 <= _index && _index <= _sz, "Invariant."); |
|
72 |
} |
|
73 |
||
74 |
void PtrQueue::locking_enqueue_completed_buffer(void** buf) { |
|
75 |
assert(_lock->owned_by_self(), "Required."); |
|
76 |
_lock->unlock(); |
|
77 |
qset()->enqueue_complete_buffer(buf); |
|
78 |
// We must relock only because the caller will unlock, for the normal |
|
79 |
// case. |
|
80 |
_lock->lock_without_safepoint_check(); |
|
81 |
} |
|
82 |
||
83 |
||
84 |
PtrQueueSet::PtrQueueSet(bool notify_when_complete) : |
|
85 |
_max_completed_queue(0), |
|
86 |
_cbl_mon(NULL), _fl_lock(NULL), |
|
87 |
_notify_when_complete(notify_when_complete), |
|
88 |
_sz(0), |
|
89 |
_completed_buffers_head(NULL), |
|
90 |
_completed_buffers_tail(NULL), |
|
91 |
_n_completed_buffers(0), |
|
92 |
_process_completed_threshold(0), _process_completed(false), |
|
93 |
_buf_free_list(NULL), _buf_free_list_sz(0) |
|
94 |
{} |
|
95 |
||
96 |
void** PtrQueueSet::allocate_buffer() { |
|
97 |
assert(_sz > 0, "Didn't set a buffer size."); |
|
98 |
MutexLockerEx x(_fl_lock, Mutex::_no_safepoint_check_flag); |
|
99 |
if (_buf_free_list != NULL) { |
|
100 |
void** res = _buf_free_list; |
|
101 |
_buf_free_list = (void**)_buf_free_list[0]; |
|
102 |
_buf_free_list_sz--; |
|
103 |
// Just override the next pointer with NULL, just in case we scan this part |
|
104 |
// of the buffer. |
|
105 |
res[0] = NULL; |
|
106 |
return res; |
|
107 |
} else { |
|
108 |
return NEW_C_HEAP_ARRAY(void*, _sz); |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
void PtrQueueSet::deallocate_buffer(void** buf) { |
|
113 |
assert(_sz > 0, "Didn't set a buffer size."); |
|
114 |
MutexLockerEx x(_fl_lock, Mutex::_no_safepoint_check_flag); |
|
115 |
buf[0] = (void*)_buf_free_list; |
|
116 |
_buf_free_list = buf; |
|
117 |
_buf_free_list_sz++; |
|
118 |
} |
|
119 |
||
120 |
void PtrQueueSet::reduce_free_list() { |
|
121 |
// For now we'll adopt the strategy of deleting half. |
|
122 |
MutexLockerEx x(_fl_lock, Mutex::_no_safepoint_check_flag); |
|
123 |
size_t n = _buf_free_list_sz / 2; |
|
124 |
while (n > 0) { |
|
125 |
assert(_buf_free_list != NULL, "_buf_free_list_sz must be wrong."); |
|
126 |
void** head = _buf_free_list; |
|
127 |
_buf_free_list = (void**)_buf_free_list[0]; |
|
128 |
FREE_C_HEAP_ARRAY(void*,head); |
|
129 |
n--; |
|
130 |
} |
|
131 |
} |
|
132 |
||
133 |
void PtrQueueSet::enqueue_complete_buffer(void** buf, size_t index, bool ignore_max_completed) { |
|
134 |
// I use explicit locking here because there's a bailout in the middle. |
|
135 |
_cbl_mon->lock_without_safepoint_check(); |
|
136 |
||
137 |
Thread* thread = Thread::current(); |
|
138 |
assert( ignore_max_completed || |
|
139 |
thread->is_Java_thread() || |
|
140 |
SafepointSynchronize::is_at_safepoint(), |
|
141 |
"invariant" ); |
|
142 |
ignore_max_completed = ignore_max_completed || !thread->is_Java_thread(); |
|
143 |
||
144 |
if (!ignore_max_completed && _max_completed_queue > 0 && |
|
145 |
_n_completed_buffers >= (size_t) _max_completed_queue) { |
|
146 |
_cbl_mon->unlock(); |
|
147 |
bool b = mut_process_buffer(buf); |
|
148 |
if (b) { |
|
149 |
deallocate_buffer(buf); |
|
150 |
return; |
|
151 |
} |
|
152 |
||
153 |
// Otherwise, go ahead and enqueue the buffer. Must reaquire the lock. |
|
154 |
_cbl_mon->lock_without_safepoint_check(); |
|
155 |
} |
|
156 |
||
157 |
// Here we still hold the _cbl_mon. |
|
158 |
CompletedBufferNode* cbn = new CompletedBufferNode; |
|
159 |
cbn->buf = buf; |
|
160 |
cbn->next = NULL; |
|
161 |
cbn->index = index; |
|
162 |
if (_completed_buffers_tail == NULL) { |
|
163 |
assert(_completed_buffers_head == NULL, "Well-formedness"); |
|
164 |
_completed_buffers_head = cbn; |
|
165 |
_completed_buffers_tail = cbn; |
|
166 |
} else { |
|
167 |
_completed_buffers_tail->next = cbn; |
|
168 |
_completed_buffers_tail = cbn; |
|
169 |
} |
|
170 |
_n_completed_buffers++; |
|
171 |
||
172 |
if (!_process_completed && |
|
173 |
_n_completed_buffers == _process_completed_threshold) { |
|
174 |
_process_completed = true; |
|
175 |
if (_notify_when_complete) |
|
176 |
_cbl_mon->notify_all(); |
|
177 |
} |
|
178 |
debug_only(assert_completed_buffer_list_len_correct_locked()); |
|
179 |
_cbl_mon->unlock(); |
|
180 |
} |
|
181 |
||
182 |
int PtrQueueSet::completed_buffers_list_length() { |
|
183 |
int n = 0; |
|
184 |
CompletedBufferNode* cbn = _completed_buffers_head; |
|
185 |
while (cbn != NULL) { |
|
186 |
n++; |
|
187 |
cbn = cbn->next; |
|
188 |
} |
|
189 |
return n; |
|
190 |
} |
|
191 |
||
192 |
void PtrQueueSet::assert_completed_buffer_list_len_correct() { |
|
193 |
MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag); |
|
194 |
assert_completed_buffer_list_len_correct_locked(); |
|
195 |
} |
|
196 |
||
197 |
void PtrQueueSet::assert_completed_buffer_list_len_correct_locked() { |
|
198 |
guarantee((size_t)completed_buffers_list_length() == _n_completed_buffers, |
|
199 |
"Completed buffer length is wrong."); |
|
200 |
} |
|
201 |
||
202 |
void PtrQueueSet::set_buffer_size(size_t sz) { |
|
203 |
assert(_sz == 0 && sz > 0, "Should be called only once."); |
|
204 |
_sz = sz * oopSize; |
|
205 |
} |
|
206 |
||
207 |
void PtrQueueSet::set_process_completed_threshold(size_t sz) { |
|
208 |
_process_completed_threshold = sz; |
|
209 |
} |