ysr@777: /* xdono@905: * Copyright 2001-2008 Sun Microsystems, Inc. All Rights Reserved. ysr@777: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ysr@777: * ysr@777: * This code is free software; you can redistribute it and/or modify it ysr@777: * under the terms of the GNU General Public License version 2 only, as ysr@777: * published by the Free Software Foundation. ysr@777: * ysr@777: * This code is distributed in the hope that it will be useful, but WITHOUT ysr@777: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ysr@777: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ysr@777: * version 2 for more details (a copy is included in the LICENSE file that ysr@777: * accompanied this code). ysr@777: * ysr@777: * You should have received a copy of the GNU General Public License version ysr@777: * 2 along with this work; if not, write to the Free Software Foundation, ysr@777: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ysr@777: * ysr@777: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, ysr@777: * CA 95054 USA or visit www.sun.com if you need additional information or ysr@777: * have any questions. ysr@777: * ysr@777: */ ysr@777: ysr@777: # include "incls/_precompiled.incl" ysr@777: # include "incls/_ptrQueue.cpp.incl" ysr@777: ysr@777: PtrQueue::PtrQueue(PtrQueueSet* qset_, bool perm) : ysr@777: _qset(qset_), _buf(NULL), _index(0), _active(false), ysr@777: _perm(perm), _lock(NULL) ysr@777: {} ysr@777: iveresov@876: void PtrQueue::flush() { ysr@777: if (!_perm && _buf != NULL) { ysr@777: if (_index == _sz) { ysr@777: // No work to do. ysr@777: qset()->deallocate_buffer(_buf); ysr@777: } else { ysr@777: // We must NULL out the unused entries, then enqueue. ysr@777: for (size_t i = 0; i < _index; i += oopSize) { ysr@777: _buf[byte_index_to_index((int)i)] = NULL; ysr@777: } ysr@777: qset()->enqueue_complete_buffer(_buf); ysr@777: } iveresov@876: _buf = NULL; iveresov@876: _index = 0; ysr@777: } ysr@777: } ysr@777: ysr@777: ysr@777: static int byte_index_to_index(int ind) { ysr@777: assert((ind % oopSize) == 0, "Invariant."); ysr@777: return ind / oopSize; ysr@777: } ysr@777: ysr@777: static int index_to_byte_index(int byte_ind) { ysr@777: return byte_ind * oopSize; ysr@777: } ysr@777: ysr@777: void PtrQueue::enqueue_known_active(void* ptr) { ysr@777: assert(0 <= _index && _index <= _sz, "Invariant."); ysr@777: assert(_index == 0 || _buf != NULL, "invariant"); ysr@777: ysr@777: while (_index == 0) { ysr@777: handle_zero_index(); ysr@777: } ysr@777: assert(_index > 0, "postcondition"); ysr@777: ysr@777: _index -= oopSize; ysr@777: _buf[byte_index_to_index((int)_index)] = ptr; ysr@777: assert(0 <= _index && _index <= _sz, "Invariant."); ysr@777: } ysr@777: ysr@777: void PtrQueue::locking_enqueue_completed_buffer(void** buf) { ysr@777: assert(_lock->owned_by_self(), "Required."); ysr@777: _lock->unlock(); ysr@777: qset()->enqueue_complete_buffer(buf); ysr@777: // We must relock only because the caller will unlock, for the normal ysr@777: // case. ysr@777: _lock->lock_without_safepoint_check(); ysr@777: } ysr@777: ysr@777: ysr@777: PtrQueueSet::PtrQueueSet(bool notify_when_complete) : ysr@777: _max_completed_queue(0), ysr@777: _cbl_mon(NULL), _fl_lock(NULL), ysr@777: _notify_when_complete(notify_when_complete), ysr@777: _sz(0), ysr@777: _completed_buffers_head(NULL), ysr@777: _completed_buffers_tail(NULL), ysr@777: _n_completed_buffers(0), ysr@777: _process_completed_threshold(0), _process_completed(false), ysr@777: _buf_free_list(NULL), _buf_free_list_sz(0) iveresov@1051: { iveresov@1051: _fl_owner = this; iveresov@1051: } ysr@777: ysr@777: void** PtrQueueSet::allocate_buffer() { ysr@777: assert(_sz > 0, "Didn't set a buffer size."); iveresov@1051: MutexLockerEx x(_fl_owner->_fl_lock, Mutex::_no_safepoint_check_flag); iveresov@1051: if (_fl_owner->_buf_free_list != NULL) { iveresov@1051: void** res = _fl_owner->_buf_free_list; iveresov@1051: _fl_owner->_buf_free_list = (void**)_fl_owner->_buf_free_list[0]; iveresov@1051: _fl_owner->_buf_free_list_sz--; ysr@777: // Just override the next pointer with NULL, just in case we scan this part ysr@777: // of the buffer. ysr@777: res[0] = NULL; ysr@777: return res; ysr@777: } else { ysr@777: return NEW_C_HEAP_ARRAY(void*, _sz); ysr@777: } ysr@777: } ysr@777: ysr@777: void PtrQueueSet::deallocate_buffer(void** buf) { ysr@777: assert(_sz > 0, "Didn't set a buffer size."); iveresov@1051: MutexLockerEx x(_fl_owner->_fl_lock, Mutex::_no_safepoint_check_flag); iveresov@1051: buf[0] = (void*)_fl_owner->_buf_free_list; iveresov@1051: _fl_owner->_buf_free_list = buf; iveresov@1051: _fl_owner->_buf_free_list_sz++; ysr@777: } ysr@777: ysr@777: void PtrQueueSet::reduce_free_list() { ysr@777: // For now we'll adopt the strategy of deleting half. ysr@777: MutexLockerEx x(_fl_lock, Mutex::_no_safepoint_check_flag); ysr@777: size_t n = _buf_free_list_sz / 2; ysr@777: while (n > 0) { ysr@777: assert(_buf_free_list != NULL, "_buf_free_list_sz must be wrong."); ysr@777: void** head = _buf_free_list; ysr@777: _buf_free_list = (void**)_buf_free_list[0]; ysr@777: FREE_C_HEAP_ARRAY(void*,head); ysr@777: n--; ysr@777: } ysr@777: } ysr@777: ysr@777: void PtrQueueSet::enqueue_complete_buffer(void** buf, size_t index, bool ignore_max_completed) { ysr@777: // I use explicit locking here because there's a bailout in the middle. ysr@777: _cbl_mon->lock_without_safepoint_check(); ysr@777: ysr@777: Thread* thread = Thread::current(); ysr@777: assert( ignore_max_completed || ysr@777: thread->is_Java_thread() || ysr@777: SafepointSynchronize::is_at_safepoint(), ysr@777: "invariant" ); ysr@777: ignore_max_completed = ignore_max_completed || !thread->is_Java_thread(); ysr@777: ysr@777: if (!ignore_max_completed && _max_completed_queue > 0 && ysr@777: _n_completed_buffers >= (size_t) _max_completed_queue) { ysr@777: _cbl_mon->unlock(); ysr@777: bool b = mut_process_buffer(buf); ysr@777: if (b) { ysr@777: deallocate_buffer(buf); ysr@777: return; ysr@777: } ysr@777: ysr@777: // Otherwise, go ahead and enqueue the buffer. Must reaquire the lock. ysr@777: _cbl_mon->lock_without_safepoint_check(); ysr@777: } ysr@777: ysr@777: // Here we still hold the _cbl_mon. ysr@777: CompletedBufferNode* cbn = new CompletedBufferNode; ysr@777: cbn->buf = buf; ysr@777: cbn->next = NULL; ysr@777: cbn->index = index; ysr@777: if (_completed_buffers_tail == NULL) { ysr@777: assert(_completed_buffers_head == NULL, "Well-formedness"); ysr@777: _completed_buffers_head = cbn; ysr@777: _completed_buffers_tail = cbn; ysr@777: } else { ysr@777: _completed_buffers_tail->next = cbn; ysr@777: _completed_buffers_tail = cbn; ysr@777: } ysr@777: _n_completed_buffers++; ysr@777: ysr@777: if (!_process_completed && iveresov@1229: _n_completed_buffers >= _process_completed_threshold) { ysr@777: _process_completed = true; ysr@777: if (_notify_when_complete) ysr@777: _cbl_mon->notify_all(); ysr@777: } ysr@777: debug_only(assert_completed_buffer_list_len_correct_locked()); ysr@777: _cbl_mon->unlock(); ysr@777: } ysr@777: ysr@777: int PtrQueueSet::completed_buffers_list_length() { ysr@777: int n = 0; ysr@777: CompletedBufferNode* cbn = _completed_buffers_head; ysr@777: while (cbn != NULL) { ysr@777: n++; ysr@777: cbn = cbn->next; ysr@777: } ysr@777: return n; ysr@777: } ysr@777: ysr@777: void PtrQueueSet::assert_completed_buffer_list_len_correct() { ysr@777: MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag); ysr@777: assert_completed_buffer_list_len_correct_locked(); ysr@777: } ysr@777: ysr@777: void PtrQueueSet::assert_completed_buffer_list_len_correct_locked() { ysr@777: guarantee((size_t)completed_buffers_list_length() == _n_completed_buffers, ysr@777: "Completed buffer length is wrong."); ysr@777: } ysr@777: ysr@777: void PtrQueueSet::set_buffer_size(size_t sz) { ysr@777: assert(_sz == 0 && sz > 0, "Should be called only once."); ysr@777: _sz = sz * oopSize; ysr@777: } ysr@777: ysr@777: void PtrQueueSet::set_process_completed_threshold(size_t sz) { ysr@777: _process_completed_threshold = sz; ysr@777: } iveresov@1051: iveresov@1051: // Merge lists of buffers. Notify waiting threads if the length of the list iveresov@1051: // exceeds threshold. The source queue is emptied as a result. The queues iveresov@1051: // must share the monitor. iveresov@1051: void PtrQueueSet::merge_bufferlists(PtrQueueSet *src) { iveresov@1051: assert(_cbl_mon == src->_cbl_mon, "Should share the same lock"); iveresov@1051: MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag); iveresov@1051: if (_completed_buffers_tail == NULL) { iveresov@1051: assert(_completed_buffers_head == NULL, "Well-formedness"); iveresov@1051: _completed_buffers_head = src->_completed_buffers_head; iveresov@1051: _completed_buffers_tail = src->_completed_buffers_tail; iveresov@1051: } else { iveresov@1051: assert(_completed_buffers_head != NULL, "Well formedness"); iveresov@1051: if (src->_completed_buffers_head != NULL) { iveresov@1051: _completed_buffers_tail->next = src->_completed_buffers_head; iveresov@1051: _completed_buffers_tail = src->_completed_buffers_tail; iveresov@1051: } iveresov@1051: } iveresov@1051: _n_completed_buffers += src->_n_completed_buffers; iveresov@1051: iveresov@1051: src->_n_completed_buffers = 0; iveresov@1051: src->_completed_buffers_head = NULL; iveresov@1051: src->_completed_buffers_tail = NULL; iveresov@1051: iveresov@1051: assert(_completed_buffers_head == NULL && _completed_buffers_tail == NULL || iveresov@1051: _completed_buffers_head != NULL && _completed_buffers_tail != NULL, iveresov@1051: "Sanity"); iveresov@1051: iveresov@1051: if (!_process_completed && iveresov@1051: _n_completed_buffers >= _process_completed_threshold) { iveresov@1051: _process_completed = true; iveresov@1051: if (_notify_when_complete) iveresov@1051: _cbl_mon->notify_all(); iveresov@1051: } iveresov@1051: } iveresov@1051: iveresov@1051: // Merge free lists of the two queues. The free list of the source iveresov@1051: // queue is emptied as a result. The queues must share the same iveresov@1051: // mutex that guards free lists. iveresov@1051: void PtrQueueSet::merge_freelists(PtrQueueSet* src) { iveresov@1051: assert(_fl_lock == src->_fl_lock, "Should share the same lock"); iveresov@1051: MutexLockerEx x(_fl_lock, Mutex::_no_safepoint_check_flag); iveresov@1051: if (_buf_free_list != NULL) { iveresov@1051: void **p = _buf_free_list; iveresov@1051: while (*p != NULL) { iveresov@1051: p = (void**)*p; iveresov@1051: } iveresov@1051: *p = src->_buf_free_list; iveresov@1051: } else { iveresov@1051: _buf_free_list = src->_buf_free_list; iveresov@1051: } iveresov@1051: _buf_free_list_sz += src->_buf_free_list_sz; iveresov@1051: src->_buf_free_list = NULL; iveresov@1051: src->_buf_free_list_sz = 0; iveresov@1051: }