ptrQueue.hpp 9.5 KB
Newer Older
1
/*
X
xdono 已提交
2
 * Copyright 2001-2009 Sun Microsystems, Inc.  All Rights Reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

// There are various techniques that require threads to be able to log
// addresses.  For example, a generational write barrier might log
// the addresses of modified old-generation objects.  This type supports
// this operation.

30 31
// The definition of placement operator new(size_t, void*) in the <new>.
#include <new>
32

33
class PtrQueueSet;
34
class PtrQueue VALUE_OBJ_CLASS_SPEC {
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

protected:
  // The ptr queue set to which this queue belongs.
  PtrQueueSet* _qset;

  // Whether updates should be logged.
  bool _active;

  // The buffer.
  void** _buf;
  // The index at which an object was last enqueued.  Starts at "_sz"
  // (indicating an empty buffer) and goes towards zero.
  size_t _index;

  // The size of the buffer.
  size_t _sz;

  // If true, the queue is permanent, and doesn't need to deallocate
  // its buffer in the destructor (since that obtains a lock which may not
  // be legally locked by then.
  bool _perm;

  // If there is a lock associated with this buffer, this is that lock.
  Mutex* _lock;

  PtrQueueSet* qset() { return _qset; }

public:
  // Initialize this queue to contain a null buffer, and be part of the
  // given PtrQueueSet.
65
  PtrQueue(PtrQueueSet*, bool perm = false, bool active = false);
66
  // Release any contained resources.
67 68 69
  void flush();
  // Calls flush() when destroyed.
  ~PtrQueue() { flush(); }
70 71 72 73 74 75 76 77 78 79 80 81

  // Associate a lock with a ptr queue.
  void set_lock(Mutex* lock) { _lock = lock; }

  void reset() { if (_buf != NULL) _index = _sz; }

  // Enqueues the given "obj".
  void enqueue(void* ptr) {
    if (!_active) return;
    else enqueue_known_active(ptr);
  }

82
  void handle_zero_index();
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  void locking_enqueue_completed_buffer(void** buf);

  void enqueue_known_active(void* ptr);

  size_t size() {
    assert(_sz >= _index, "Invariant.");
    return _buf == NULL ? 0 : _sz - _index;
  }

  // Set the "active" property of the queue to "b".  An enqueue to an
  // inactive thread is a no-op.  Setting a queue to inactive resets its
  // log to the empty state.
  void set_active(bool b) {
    _active = b;
    if (!b && _buf != NULL) {
      _index = _sz;
    } else if (b && _buf != NULL) {
      assert(_index == _sz, "invariant: queues are empty when activated.");
    }
  }

104 105
  bool is_active() { return _active; }

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
  static int byte_index_to_index(int ind) {
    assert((ind % oopSize) == 0, "Invariant.");
    return ind / oopSize;
  }

  static int index_to_byte_index(int byte_ind) {
    return byte_ind * oopSize;
  }

  // To support compiler.
  static ByteSize byte_offset_of_index() {
    return byte_offset_of(PtrQueue, _index);
  }
  static ByteSize byte_width_of_index() { return in_ByteSize(sizeof(size_t)); }

  static ByteSize byte_offset_of_buf() {
    return byte_offset_of(PtrQueue, _buf);
  }
  static ByteSize byte_width_of_buf() { return in_ByteSize(sizeof(void*)); }

  static ByteSize byte_offset_of_active() {
    return byte_offset_of(PtrQueue, _active);
  }
  static ByteSize byte_width_of_active() { return in_ByteSize(sizeof(bool)); }

};

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
class BufferNode {
  size_t _index;
  BufferNode* _next;
public:
  BufferNode() : _index(0), _next(NULL) { }
  BufferNode* next() const     { return _next;  }
  void set_next(BufferNode* n) { _next = n;     }
  size_t index() const         { return _index; }
  void set_index(size_t i)     { _index = i;    }

  // Align the size of the structure to the size of the pointer
  static size_t aligned_size() {
    static const size_t alignment = round_to(sizeof(BufferNode), sizeof(void*));
    return alignment;
  }

  // BufferNode is allocated before the buffer.
  // The chunk of memory that holds both of them is a block.

  // Produce a new BufferNode given a buffer.
  static BufferNode* new_from_buffer(void** buf) {
    return new (make_block_from_buffer(buf)) BufferNode;
  }

  // The following are the required conversion routines:
  static BufferNode* make_node_from_buffer(void** buf) {
    return (BufferNode*)make_block_from_buffer(buf);
  }
  static void** make_buffer_from_node(BufferNode *node) {
    return make_buffer_from_block(node);
  }
  static void* make_block_from_node(BufferNode *node) {
    return (void*)node;
  }
  static void** make_buffer_from_block(void* p) {
    return (void**)((char*)p + aligned_size());
  }
  static void* make_block_from_buffer(void** p) {
    return (void*)((char*)p - aligned_size());
  }
};

175 176 177 178
// A PtrQueueSet represents resources common to a set of pointer queues.
// In particular, the individual queues allocate buffers from this shared
// set, and return completed buffers to the set.
// All these variables are are protected by the TLOQ_CBL_mon. XXX ???
179
class PtrQueueSet VALUE_OBJ_CLASS_SPEC {
180 181
protected:
  Monitor* _cbl_mon;  // Protects the fields below.
182 183 184 185
  BufferNode* _completed_buffers_head;
  BufferNode* _completed_buffers_tail;
  int _n_completed_buffers;
  int _process_completed_threshold;
186 187 188 189 190
  volatile bool _process_completed;

  // This (and the interpretation of the first element as a "next"
  // pointer) are protected by the TLOQ_FL_lock.
  Mutex* _fl_lock;
191
  BufferNode* _buf_free_list;
192
  size_t _buf_free_list_sz;
193 194 195
  // Queue set can share a freelist. The _fl_owner variable
  // specifies the owner. It is set to "this" by default.
  PtrQueueSet* _fl_owner;
196 197 198 199 200 201 202 203 204 205 206 207

  // The size of all buffers in the set.
  size_t _sz;

  bool _all_active;

  // If true, notify_all on _cbl_mon when the threshold is reached.
  bool _notify_when_complete;

  // Maximum number of elements allowed on completed queue: after that,
  // enqueuer does the work itself.  Zero indicates no maximum.
  int _max_completed_queue;
208
  int _completed_queue_padding;
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

  int completed_buffers_list_length();
  void assert_completed_buffer_list_len_correct_locked();
  void assert_completed_buffer_list_len_correct();

protected:
  // A mutator thread does the the work of processing a buffer.
  // Returns "true" iff the work is complete (and the buffer may be
  // deallocated).
  virtual bool mut_process_buffer(void** buf) {
    ShouldNotReachHere();
    return false;
  }

public:
  // Create an empty ptr queue set.
  PtrQueueSet(bool notify_when_complete = false);

  // Because of init-order concerns, we can't pass these as constructor
  // arguments.
  void initialize(Monitor* cbl_mon, Mutex* fl_lock,
230 231
                  int process_completed_threshold,
                  int max_completed_queue,
232
                  PtrQueueSet *fl_owner = NULL) {
233
    _max_completed_queue = max_completed_queue;
234 235
    _process_completed_threshold = process_completed_threshold;
    _completed_queue_padding = 0;
236
    assert(cbl_mon != NULL && fl_lock != NULL, "Init order issue?");
237 238 239
    _cbl_mon = cbl_mon;
    _fl_lock = fl_lock;
    _fl_owner = (fl_owner != NULL) ? fl_owner : this;
240 241 242 243 244 245 246 247 248 249
  }

  // Return an empty oop array of size _sz (required to be non-zero).
  void** allocate_buffer();

  // Return an empty buffer to the free list.  The "buf" argument is
  // required to be a pointer to the head of an array of length "_sz".
  void deallocate_buffer(void** buf);

  // Declares that "buf" is a complete buffer.
250 251 252 253
  void enqueue_complete_buffer(void** buf, size_t index = 0);

  // To be invoked by the mutator.
  bool process_or_enqueue_complete_buffer(void** buf);
254 255 256 257 258 259

  bool completed_buffers_exist_dirty() {
    return _n_completed_buffers > 0;
  }

  bool process_completed_buffers() { return _process_completed; }
260
  void set_process_completed(bool x) { _process_completed = x; }
261

262
  bool is_active() { return _all_active; }
263 264 265 266 267 268 269 270

  // Set the buffer size.  Should be called before any "enqueue" operation
  // can be called.  And should only be called once.
  void set_buffer_size(size_t sz);

  // Get the buffer size.
  size_t buffer_size() { return _sz; }

271 272 273
  // Get/Set the number of completed buffers that triggers log processing.
  void set_process_completed_threshold(int sz) { _process_completed_threshold = sz; }
  int process_completed_threshold() const { return _process_completed_threshold; }
274 275 276 277 278

  // Must only be called at a safe point.  Indicates that the buffer free
  // list size may be reduced, if that is deemed desirable.
  void reduce_free_list();

279
  int completed_buffers_num() { return _n_completed_buffers; }
280 281

  void merge_bufferlists(PtrQueueSet* src);
282 283 284 285 286 287 288 289 290

  void set_max_completed_queue(int m) { _max_completed_queue = m; }
  int max_completed_queue() { return _max_completed_queue; }

  void set_completed_queue_padding(int padding) { _completed_queue_padding = padding; }
  int completed_queue_padding() { return _completed_queue_padding; }

  // Notify the consumer if the number of buffers crossed the threshold
  void notify_if_necessary();
291
};