ptrQueue.hpp revision 579
342N/A/*
579N/A * Copyright 2001-2009 Sun Microsystems, Inc. All Rights Reserved.
342N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
342N/A *
342N/A * This code is free software; you can redistribute it and/or modify it
342N/A * under the terms of the GNU General Public License version 2 only, as
342N/A * published by the Free Software Foundation.
342N/A *
342N/A * This code is distributed in the hope that it will be useful, but WITHOUT
342N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
342N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
342N/A * version 2 for more details (a copy is included in the LICENSE file that
342N/A * accompanied this code).
342N/A *
342N/A * You should have received a copy of the GNU General Public License version
342N/A * 2 along with this work; if not, write to the Free Software Foundation,
342N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
342N/A *
342N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
342N/A * CA 95054 USA or visit www.sun.com if you need additional information or
342N/A * have any questions.
342N/A *
342N/A */
342N/A
342N/A// There are various techniques that require threads to be able to log
342N/A// addresses. For example, a generational write barrier might log
342N/A// the addresses of modified old-generation objects. This type supports
342N/A// this operation.
342N/A
342N/Aclass PtrQueueSet;
342N/A
549N/Aclass PtrQueue VALUE_OBJ_CLASS_SPEC {
342N/A
342N/Aprotected:
342N/A // The ptr queue set to which this queue belongs.
342N/A PtrQueueSet* _qset;
342N/A
342N/A // Whether updates should be logged.
342N/A bool _active;
342N/A
342N/A // The buffer.
342N/A void** _buf;
342N/A // The index at which an object was last enqueued. Starts at "_sz"
342N/A // (indicating an empty buffer) and goes towards zero.
342N/A size_t _index;
342N/A
342N/A // The size of the buffer.
342N/A size_t _sz;
342N/A
342N/A // If true, the queue is permanent, and doesn't need to deallocate
342N/A // its buffer in the destructor (since that obtains a lock which may not
342N/A // be legally locked by then.
342N/A bool _perm;
342N/A
342N/A // If there is a lock associated with this buffer, this is that lock.
342N/A Mutex* _lock;
342N/A
342N/A PtrQueueSet* qset() { return _qset; }
342N/A
342N/Apublic:
342N/A // Initialize this queue to contain a null buffer, and be part of the
342N/A // given PtrQueueSet.
342N/A PtrQueue(PtrQueueSet*, bool perm = false);
342N/A // Release any contained resources.
441N/A void flush();
441N/A // Calls flush() when destroyed.
441N/A ~PtrQueue() { flush(); }
342N/A
342N/A // Associate a lock with a ptr queue.
342N/A void set_lock(Mutex* lock) { _lock = lock; }
342N/A
342N/A void reset() { if (_buf != NULL) _index = _sz; }
342N/A
342N/A // Enqueues the given "obj".
342N/A void enqueue(void* ptr) {
342N/A if (!_active) return;
342N/A else enqueue_known_active(ptr);
342N/A }
342N/A
342N/A inline void handle_zero_index();
342N/A void locking_enqueue_completed_buffer(void** buf);
342N/A
342N/A void enqueue_known_active(void* ptr);
342N/A
342N/A size_t size() {
342N/A assert(_sz >= _index, "Invariant.");
342N/A return _buf == NULL ? 0 : _sz - _index;
342N/A }
342N/A
342N/A // Set the "active" property of the queue to "b". An enqueue to an
342N/A // inactive thread is a no-op. Setting a queue to inactive resets its
342N/A // log to the empty state.
342N/A void set_active(bool b) {
342N/A _active = b;
342N/A if (!b && _buf != NULL) {
342N/A _index = _sz;
342N/A } else if (b && _buf != NULL) {
342N/A assert(_index == _sz, "invariant: queues are empty when activated.");
342N/A }
342N/A }
342N/A
342N/A static int byte_index_to_index(int ind) {
342N/A assert((ind % oopSize) == 0, "Invariant.");
342N/A return ind / oopSize;
342N/A }
342N/A
342N/A static int index_to_byte_index(int byte_ind) {
342N/A return byte_ind * oopSize;
342N/A }
342N/A
342N/A // To support compiler.
342N/A static ByteSize byte_offset_of_index() {
342N/A return byte_offset_of(PtrQueue, _index);
342N/A }
342N/A static ByteSize byte_width_of_index() { return in_ByteSize(sizeof(size_t)); }
342N/A
342N/A static ByteSize byte_offset_of_buf() {
342N/A return byte_offset_of(PtrQueue, _buf);
342N/A }
342N/A static ByteSize byte_width_of_buf() { return in_ByteSize(sizeof(void*)); }
342N/A
342N/A static ByteSize byte_offset_of_active() {
342N/A return byte_offset_of(PtrQueue, _active);
342N/A }
342N/A static ByteSize byte_width_of_active() { return in_ByteSize(sizeof(bool)); }
342N/A
342N/A};
342N/A
342N/A// A PtrQueueSet represents resources common to a set of pointer queues.
342N/A// In particular, the individual queues allocate buffers from this shared
342N/A// set, and return completed buffers to the set.
342N/A// All these variables are are protected by the TLOQ_CBL_mon. XXX ???
549N/Aclass PtrQueueSet VALUE_OBJ_CLASS_SPEC {
342N/A
342N/Aprotected:
342N/A
342N/A class CompletedBufferNode: public CHeapObj {
342N/A public:
342N/A void** buf;
342N/A size_t index;
342N/A CompletedBufferNode* next;
342N/A CompletedBufferNode() : buf(NULL),
342N/A index(0), next(NULL){ }
342N/A };
342N/A
342N/A Monitor* _cbl_mon; // Protects the fields below.
342N/A CompletedBufferNode* _completed_buffers_head;
342N/A CompletedBufferNode* _completed_buffers_tail;
342N/A size_t _n_completed_buffers;
342N/A size_t _process_completed_threshold;
342N/A volatile bool _process_completed;
342N/A
342N/A // This (and the interpretation of the first element as a "next"
342N/A // pointer) are protected by the TLOQ_FL_lock.
342N/A Mutex* _fl_lock;
342N/A void** _buf_free_list;
342N/A size_t _buf_free_list_sz;
342N/A
342N/A // The size of all buffers in the set.
342N/A size_t _sz;
342N/A
342N/A bool _all_active;
342N/A
342N/A // If true, notify_all on _cbl_mon when the threshold is reached.
342N/A bool _notify_when_complete;
342N/A
342N/A // Maximum number of elements allowed on completed queue: after that,
342N/A // enqueuer does the work itself. Zero indicates no maximum.
342N/A int _max_completed_queue;
342N/A
342N/A int completed_buffers_list_length();
342N/A void assert_completed_buffer_list_len_correct_locked();
342N/A void assert_completed_buffer_list_len_correct();
342N/A
342N/Aprotected:
342N/A // A mutator thread does the the work of processing a buffer.
342N/A // Returns "true" iff the work is complete (and the buffer may be
342N/A // deallocated).
342N/A virtual bool mut_process_buffer(void** buf) {
342N/A ShouldNotReachHere();
342N/A return false;
342N/A }
342N/A
342N/Apublic:
342N/A // Create an empty ptr queue set.
342N/A PtrQueueSet(bool notify_when_complete = false);
342N/A
342N/A // Because of init-order concerns, we can't pass these as constructor
342N/A // arguments.
342N/A void initialize(Monitor* cbl_mon, Mutex* fl_lock,
342N/A int max_completed_queue = 0) {
342N/A _max_completed_queue = max_completed_queue;
342N/A assert(cbl_mon != NULL && fl_lock != NULL, "Init order issue?");
342N/A _cbl_mon = cbl_mon; _fl_lock = fl_lock;
342N/A }
342N/A
342N/A // Return an empty oop array of size _sz (required to be non-zero).
342N/A void** allocate_buffer();
342N/A
342N/A // Return an empty buffer to the free list. The "buf" argument is
342N/A // required to be a pointer to the head of an array of length "_sz".
342N/A void deallocate_buffer(void** buf);
342N/A
342N/A // Declares that "buf" is a complete buffer.
342N/A void enqueue_complete_buffer(void** buf, size_t index = 0,
342N/A bool ignore_max_completed = false);
342N/A
342N/A bool completed_buffers_exist_dirty() {
342N/A return _n_completed_buffers > 0;
342N/A }
342N/A
342N/A bool process_completed_buffers() { return _process_completed; }
342N/A
342N/A bool active() { return _all_active; }
342N/A
342N/A // Set the buffer size. Should be called before any "enqueue" operation
342N/A // can be called. And should only be called once.
342N/A void set_buffer_size(size_t sz);
342N/A
342N/A // Get the buffer size.
342N/A size_t buffer_size() { return _sz; }
342N/A
342N/A // Set the number of completed buffers that triggers log processing.
342N/A void set_process_completed_threshold(size_t sz);
342N/A
342N/A // Must only be called at a safe point. Indicates that the buffer free
342N/A // list size may be reduced, if that is deemed desirable.
342N/A void reduce_free_list();
342N/A
342N/A size_t completed_buffers_num() { return _n_completed_buffers; }
342N/A};