1374
|
1 |
/*
|
|
2 |
* Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
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 |
|
|
33 |
PtrQueue::~PtrQueue() {
|
|
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 |
_buf = NULL;
|
|
45 |
}
|
|
46 |
}
|
|
47 |
}
|
|
48 |
|
|
49 |
|
|
50 |
static int byte_index_to_index(int ind) {
|
|
51 |
assert((ind % oopSize) == 0, "Invariant.");
|
|
52 |
return ind / oopSize;
|
|
53 |
}
|
|
54 |
|
|
55 |
static int index_to_byte_index(int byte_ind) {
|
|
56 |
return byte_ind * oopSize;
|
|
57 |
}
|
|
58 |
|
|
59 |
void PtrQueue::enqueue_known_active(void* ptr) {
|
|
60 |
assert(0 <= _index && _index <= _sz, "Invariant.");
|
|
61 |
assert(_index == 0 || _buf != NULL, "invariant");
|
|
62 |
|
|
63 |
while (_index == 0) {
|
|
64 |
handle_zero_index();
|
|
65 |
}
|
|
66 |
assert(_index > 0, "postcondition");
|
|
67 |
|
|
68 |
_index -= oopSize;
|
|
69 |
_buf[byte_index_to_index((int)_index)] = ptr;
|
|
70 |
assert(0 <= _index && _index <= _sz, "Invariant.");
|
|
71 |
}
|
|
72 |
|
|
73 |
void PtrQueue::locking_enqueue_completed_buffer(void** buf) {
|
|
74 |
assert(_lock->owned_by_self(), "Required.");
|
|
75 |
_lock->unlock();
|
|
76 |
qset()->enqueue_complete_buffer(buf);
|
|
77 |
// We must relock only because the caller will unlock, for the normal
|
|
78 |
// case.
|
|
79 |
_lock->lock_without_safepoint_check();
|
|
80 |
}
|
|
81 |
|
|
82 |
|
|
83 |
PtrQueueSet::PtrQueueSet(bool notify_when_complete) :
|
|
84 |
_max_completed_queue(0),
|
|
85 |
_cbl_mon(NULL), _fl_lock(NULL),
|
|
86 |
_notify_when_complete(notify_when_complete),
|
|
87 |
_sz(0),
|
|
88 |
_completed_buffers_head(NULL),
|
|
89 |
_completed_buffers_tail(NULL),
|
|
90 |
_n_completed_buffers(0),
|
|
91 |
_process_completed_threshold(0), _process_completed(false),
|
|
92 |
_buf_free_list(NULL), _buf_free_list_sz(0)
|
|
93 |
{}
|
|
94 |
|
|
95 |
void** PtrQueueSet::allocate_buffer() {
|
|
96 |
assert(_sz > 0, "Didn't set a buffer size.");
|
|
97 |
MutexLockerEx x(_fl_lock, Mutex::_no_safepoint_check_flag);
|
|
98 |
if (_buf_free_list != NULL) {
|
|
99 |
void** res = _buf_free_list;
|
|
100 |
_buf_free_list = (void**)_buf_free_list[0];
|
|
101 |
_buf_free_list_sz--;
|
|
102 |
// Just override the next pointer with NULL, just in case we scan this part
|
|
103 |
// of the buffer.
|
|
104 |
res[0] = NULL;
|
|
105 |
return res;
|
|
106 |
} else {
|
|
107 |
return NEW_C_HEAP_ARRAY(void*, _sz);
|
|
108 |
}
|
|
109 |
}
|
|
110 |
|
|
111 |
void PtrQueueSet::deallocate_buffer(void** buf) {
|
|
112 |
assert(_sz > 0, "Didn't set a buffer size.");
|
|
113 |
MutexLockerEx x(_fl_lock, Mutex::_no_safepoint_check_flag);
|
|
114 |
buf[0] = (void*)_buf_free_list;
|
|
115 |
_buf_free_list = buf;
|
|
116 |
_buf_free_list_sz++;
|
|
117 |
}
|
|
118 |
|
|
119 |
void PtrQueueSet::reduce_free_list() {
|
|
120 |
// For now we'll adopt the strategy of deleting half.
|
|
121 |
MutexLockerEx x(_fl_lock, Mutex::_no_safepoint_check_flag);
|
|
122 |
size_t n = _buf_free_list_sz / 2;
|
|
123 |
while (n > 0) {
|
|
124 |
assert(_buf_free_list != NULL, "_buf_free_list_sz must be wrong.");
|
|
125 |
void** head = _buf_free_list;
|
|
126 |
_buf_free_list = (void**)_buf_free_list[0];
|
|
127 |
FREE_C_HEAP_ARRAY(void*,head);
|
|
128 |
n--;
|
|
129 |
}
|
|
130 |
}
|
|
131 |
|
|
132 |
void PtrQueueSet::enqueue_complete_buffer(void** buf, size_t index, bool ignore_max_completed) {
|
|
133 |
// I use explicit locking here because there's a bailout in the middle.
|
|
134 |
_cbl_mon->lock_without_safepoint_check();
|
|
135 |
|
|
136 |
Thread* thread = Thread::current();
|
|
137 |
assert( ignore_max_completed ||
|
|
138 |
thread->is_Java_thread() ||
|
|
139 |
SafepointSynchronize::is_at_safepoint(),
|
|
140 |
"invariant" );
|
|
141 |
ignore_max_completed = ignore_max_completed || !thread->is_Java_thread();
|
|
142 |
|
|
143 |
if (!ignore_max_completed && _max_completed_queue > 0 &&
|
|
144 |
_n_completed_buffers >= (size_t) _max_completed_queue) {
|
|
145 |
_cbl_mon->unlock();
|
|
146 |
bool b = mut_process_buffer(buf);
|
|
147 |
if (b) {
|
|
148 |
deallocate_buffer(buf);
|
|
149 |
return;
|
|
150 |
}
|
|
151 |
|
|
152 |
// Otherwise, go ahead and enqueue the buffer. Must reaquire the lock.
|
|
153 |
_cbl_mon->lock_without_safepoint_check();
|
|
154 |
}
|
|
155 |
|
|
156 |
// Here we still hold the _cbl_mon.
|
|
157 |
CompletedBufferNode* cbn = new CompletedBufferNode;
|
|
158 |
cbn->buf = buf;
|
|
159 |
cbn->next = NULL;
|
|
160 |
cbn->index = index;
|
|
161 |
if (_completed_buffers_tail == NULL) {
|
|
162 |
assert(_completed_buffers_head == NULL, "Well-formedness");
|
|
163 |
_completed_buffers_head = cbn;
|
|
164 |
_completed_buffers_tail = cbn;
|
|
165 |
} else {
|
|
166 |
_completed_buffers_tail->next = cbn;
|
|
167 |
_completed_buffers_tail = cbn;
|
|
168 |
}
|
|
169 |
_n_completed_buffers++;
|
|
170 |
|
|
171 |
if (!_process_completed &&
|
|
172 |
_n_completed_buffers == _process_completed_threshold) {
|
|
173 |
_process_completed = true;
|
|
174 |
if (_notify_when_complete)
|
|
175 |
_cbl_mon->notify_all();
|
|
176 |
}
|
|
177 |
debug_only(assert_completed_buffer_list_len_correct_locked());
|
|
178 |
_cbl_mon->unlock();
|
|
179 |
}
|
|
180 |
|
|
181 |
int PtrQueueSet::completed_buffers_list_length() {
|
|
182 |
int n = 0;
|
|
183 |
CompletedBufferNode* cbn = _completed_buffers_head;
|
|
184 |
while (cbn != NULL) {
|
|
185 |
n++;
|
|
186 |
cbn = cbn->next;
|
|
187 |
}
|
|
188 |
return n;
|
|
189 |
}
|
|
190 |
|
|
191 |
void PtrQueueSet::assert_completed_buffer_list_len_correct() {
|
|
192 |
MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
|
|
193 |
assert_completed_buffer_list_len_correct_locked();
|
|
194 |
}
|
|
195 |
|
|
196 |
void PtrQueueSet::assert_completed_buffer_list_len_correct_locked() {
|
|
197 |
guarantee((size_t)completed_buffers_list_length() == _n_completed_buffers,
|
|
198 |
"Completed buffer length is wrong.");
|
|
199 |
}
|
|
200 |
|
|
201 |
void PtrQueueSet::set_buffer_size(size_t sz) {
|
|
202 |
assert(_sz == 0 && sz > 0, "Should be called only once.");
|
|
203 |
_sz = sz * oopSize;
|
|
204 |
}
|
|
205 |
|
|
206 |
void PtrQueueSet::set_process_completed_threshold(size_t sz) {
|
|
207 |
_process_completed_threshold = sz;
|
|
208 |
}
|