hotspot/src/share/vm/utilities/taskqueue.hpp
author jcoomes
Tue, 30 Sep 2008 12:20:22 -0700
changeset 1407 9006b01ba3fd
parent 1388 3677f5f3d66b
child 2005 42075507972b
permissions -rw-r--r--
6725697: par compact - rename class ChunkData to RegionData Reviewed-by: iveresov, tonyp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
670
ddf3e9583f2f 6719955: Update copyright year
xdono
parents: 360
diff changeset
     2
 * Copyright 2001-2008 Sun Microsystems, Inc.  All Rights Reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
class TaskQueueSuper: public CHeapObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
  // The first free element after the last one pushed (mod _n).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
  // (For now we'll assume only 32-bit CAS).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
  volatile juint _bottom;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
  // log2 of the size of the queue.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  enum SomeProtectedConstants {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
    Log_n = 14
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  // Size of the queue.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  juint n() { return (1 << Log_n); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  // For computing "x mod n" efficiently.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  juint n_mod_mask() { return n() - 1; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  struct Age {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
    jushort _top;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
    jushort _tag;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
    jushort tag() const { return _tag; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
    jushort top() const { return _top; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
    Age() { _tag = 0; _top = 0; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
    friend bool operator ==(const Age& a1, const Age& a2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
      return a1.tag() == a2.tag() && a1.top() == a2.top();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  Age _age;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  // These make sure we do single atomic reads and writes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  Age get_age() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
    jint res = *(volatile jint*)(&_age);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
    return *(Age*)(&res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  void set_age(Age a) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
    *(volatile jint*)(&_age) = *(int*)(&a);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  jushort get_top() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
    return get_age().top();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  // These both operate mod _n.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  juint increment_index(juint ind) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
    return (ind + 1) & n_mod_mask();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  juint decrement_index(juint ind) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
    return (ind - 1) & n_mod_mask();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  // Returns a number in the range [0.._n).  If the result is "n-1", it
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  // should be interpreted as 0.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  juint dirty_size(juint bot, juint top) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
    return ((jint)bot - (jint)top) & n_mod_mask();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  // Returns the size corresponding to the given "bot" and "top".
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  juint size(juint bot, juint top) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
    juint sz = dirty_size(bot, top);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
    // Has the queue "wrapped", so that bottom is less than top?
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
    // There's a complicated special case here.  A pair of threads could
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
    // perform pop_local and pop_global operations concurrently, starting
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
    // from a state in which _bottom == _top+1.  The pop_local could
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
    // succeed in decrementing _bottom, and the pop_global in incrementing
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
    // _top (in which case the pop_global will be awarded the contested
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
    // queue element.)  The resulting state must be interpreted as an empty
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
    // queue.  (We only need to worry about one such event: only the queue
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
    // owner performs pop_local's, and several concurrent threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
    // attempting to perform the pop_global will all perform the same CAS,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
    // and only one can succeed.  Any stealing thread that reads after
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
    // either the increment or decrement will seen an empty queue, and will
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
    // not join the competitors.  The "sz == -1 || sz == _n-1" state will
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
    // not be modified  by concurrent queues, so the owner thread can reset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
    // the state to _bottom == top so subsequent pushes will be performed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
    // normally.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
    if (sz == (n()-1)) return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
    else return sz;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  TaskQueueSuper() : _bottom(0), _age() {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  // Return "true" if the TaskQueue contains any tasks.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  bool peek();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  // Return an estimate of the number of elements in the queue.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  // The "careful" version admits the possibility of pop_local/pop_global
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  // races.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  juint size() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
    return size(_bottom, get_top());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  juint dirty_size() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
    return dirty_size(_bottom, get_top());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   123
  void set_empty() {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   124
    _bottom = 0;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   125
    _age = Age();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   126
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   127
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  // Maximum number of elements allowed in the queue.  This is two less
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  // than the actual queue size, for somewhat complicated reasons.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  juint max_elems() { return n() - 2; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
template<class E> class GenericTaskQueue: public TaskQueueSuper {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  // Slow paths for push, pop_local.  (pop_global has no fast path.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  bool push_slow(E t, juint dirty_n_elems);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  bool pop_local_slow(juint localBot, Age oldAge);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  // Initializes the queue to empty.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  GenericTaskQueue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  void initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  // Push the task "t" on the queue.  Returns "false" iff the queue is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  // full.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  inline bool push(E t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  // If succeeds in claiming a task (from the 'local' end, that is, the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  // most recently pushed task), returns "true" and sets "t" to that task.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  // Otherwise, the queue is empty and returns false.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  inline bool pop_local(E& t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
  // If succeeds in claiming a task (from the 'global' end, that is, the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  // least recently pushed task), returns "true" and sets "t" to that task.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  // Otherwise, the queue is empty and returns false.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  bool pop_global(E& t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  // Delete any resource associated with the queue.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  ~GenericTaskQueue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   163
  // apply the closure to all elements in the task queue
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   164
  void oops_do(OopClosure* f);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   165
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  // Element array.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  volatile E* _elems;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
GenericTaskQueue<E>::GenericTaskQueue():TaskQueueSuper() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  assert(sizeof(Age) == sizeof(jint), "Depends on this.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
void GenericTaskQueue<E>::initialize() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
  _elems = NEW_C_HEAP_ARRAY(E, n());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  guarantee(_elems != NULL, "Allocation failed.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
template<class E>
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   183
void GenericTaskQueue<E>::oops_do(OopClosure* f) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   184
  // tty->print_cr("START OopTaskQueue::oops_do");
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   185
  int iters = size();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   186
  juint index = _bottom;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   187
  for (int i = 0; i < iters; ++i) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   188
    index = decrement_index(index);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   189
    // tty->print_cr("  doing entry %d," INTPTR_T " -> " INTPTR_T,
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   190
    //            index, &_elems[index], _elems[index]);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   191
    E* t = (E*)&_elems[index];      // cast away volatility
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   192
    oop* p = (oop*)t;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   193
    assert((*t)->is_oop_or_null(), "Not an oop or null");
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   194
    f->do_oop(p);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   195
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   196
  // tty->print_cr("END OopTaskQueue::oops_do");
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   197
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   198
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   199
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   200
template<class E>
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
bool GenericTaskQueue<E>::push_slow(E t, juint dirty_n_elems) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  if (dirty_n_elems == n() - 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
    // Actually means 0, so do the push.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
    juint localBot = _bottom;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
    _elems[localBot] = t;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
    _bottom = increment_index(localBot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
  } else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
bool GenericTaskQueue<E>::
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
pop_local_slow(juint localBot, Age oldAge) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
  // This queue was observed to contain exactly one element; either this
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  // thread will claim it, or a competing "pop_global".  In either case,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  // the queue will be logically empty afterwards.  Create a new Age value
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
  // that represents the empty queue for the given value of "_bottom".  (We
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  // must also increment "tag" because of the case where "bottom == 1",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  // "top == 0".  A pop_global could read the queue element in that case,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
  // then have the owner thread do a pop followed by another push.  Without
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
  // the incrementing of "tag", the pop_global's CAS could succeed,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  // allowing it to believe it has claimed the stale element.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
  Age newAge;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
  newAge._top = localBot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  newAge._tag = oldAge.tag() + 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  // Perhaps a competing pop_global has already incremented "top", in which
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  // case it wins the element.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
  if (localBot == oldAge.top()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
    Age tempAge;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
    // No competing pop_global has yet incremented "top"; we'll try to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
    // install new_age, thus claiming the element.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
    assert(sizeof(Age) == sizeof(jint) && sizeof(jint) == sizeof(juint),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
           "Assumption about CAS unit.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
    *(jint*)&tempAge = Atomic::cmpxchg(*(jint*)&newAge, (volatile jint*)&_age, *(jint*)&oldAge);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
    if (tempAge == oldAge) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
      // We win.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
      assert(dirty_size(localBot, get_top()) != n() - 1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
             "Shouldn't be possible...");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
  // We fail; a completing pop_global gets the element.  But the queue is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
  // empty (and top is greater than bottom.)  Fix this representation of
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
  // the empty queue to become the canonical one.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
  set_age(newAge);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  assert(dirty_size(localBot, get_top()) != n() - 1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
         "Shouldn't be possible...");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
bool GenericTaskQueue<E>::pop_global(E& t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
  Age newAge;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
  Age oldAge = get_age();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
  juint localBot = _bottom;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  juint n_elems = size(localBot, oldAge.top());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
  if (n_elems == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
  t = _elems[oldAge.top()];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
  newAge = oldAge;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
  newAge._top = increment_index(newAge.top());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
  if ( newAge._top == 0 ) newAge._tag++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
  Age resAge;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
  *(jint*)&resAge = Atomic::cmpxchg(*(jint*)&newAge, (volatile jint*)&_age, *(jint*)&oldAge);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  // Note that using "_bottom" here might fail, since a pop_local might
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
  // have decremented it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
  assert(dirty_size(localBot, newAge._top) != n() - 1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
         "Shouldn't be possible...");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
  return (resAge == oldAge);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
GenericTaskQueue<E>::~GenericTaskQueue() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
  FREE_C_HEAP_ARRAY(E, _elems);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
// Inherits the typedef of "Task" from above.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
class TaskQueueSetSuper: public CHeapObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  static int randomParkAndMiller(int* seed0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  // Returns "true" if some TaskQueue in the set contains a task.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
  virtual bool peek() = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
template<class E> class GenericTaskQueueSet: public TaskQueueSetSuper {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  int _n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  GenericTaskQueue<E>** _queues;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
  GenericTaskQueueSet(int n) : _n(n) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
    typedef GenericTaskQueue<E>* GenericTaskQueuePtr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
    _queues = NEW_C_HEAP_ARRAY(GenericTaskQueuePtr, n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
    guarantee(_queues != NULL, "Allocation failure.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
    for (int i = 0; i < n; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
      _queues[i] = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
  bool steal_1_random(int queue_num, int* seed, E& t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
  bool steal_best_of_2(int queue_num, int* seed, E& t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
  bool steal_best_of_all(int queue_num, int* seed, E& t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
  void register_queue(int i, GenericTaskQueue<E>* q);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
  GenericTaskQueue<E>* queue(int n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
  // The thread with queue number "queue_num" (and whose random number seed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
  // is at "seed") is trying to steal a task from some other queue.  (It
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
  // may try several queues, according to some configuration parameter.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
  // If some steal succeeds, returns "true" and sets "t" the stolen task,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
  // otherwise returns false.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  bool steal(int queue_num, int* seed, E& t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
  bool peek();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
void GenericTaskQueueSet<E>::register_queue(int i, GenericTaskQueue<E>* q) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
  assert(0 <= i && i < _n, "index out of range.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
  _queues[i] = q;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
GenericTaskQueue<E>* GenericTaskQueueSet<E>::queue(int i) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
  return _queues[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
bool GenericTaskQueueSet<E>::steal(int queue_num, int* seed, E& t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
  for (int i = 0; i < 2 * _n; i++)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
    if (steal_best_of_2(queue_num, seed, t))
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
bool GenericTaskQueueSet<E>::steal_best_of_all(int queue_num, int* seed, E& t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
  if (_n > 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
    int best_k;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
    jint best_sz = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
    for (int k = 0; k < _n; k++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
      if (k == queue_num) continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
      jint sz = _queues[k]->size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
      if (sz > best_sz) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
        best_sz = sz;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
        best_k = k;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
    return best_sz > 0 && _queues[best_k]->pop_global(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
  } else if (_n == 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
    // Just try the other one.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
    int k = (queue_num + 1) % 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
    return _queues[k]->pop_global(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
    assert(_n == 1, "can't be zero.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
bool GenericTaskQueueSet<E>::steal_1_random(int queue_num, int* seed, E& t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
  if (_n > 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
    int k = queue_num;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
    while (k == queue_num) k = randomParkAndMiller(seed) % _n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
    return _queues[2]->pop_global(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
  } else if (_n == 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
    // Just try the other one.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
    int k = (queue_num + 1) % 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
    return _queues[k]->pop_global(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
    assert(_n == 1, "can't be zero.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
bool GenericTaskQueueSet<E>::steal_best_of_2(int queue_num, int* seed, E& t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
  if (_n > 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
    int k1 = queue_num;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
    while (k1 == queue_num) k1 = randomParkAndMiller(seed) % _n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
    int k2 = queue_num;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
    while (k2 == queue_num || k2 == k1) k2 = randomParkAndMiller(seed) % _n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
    // Sample both and try the larger.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
    juint sz1 = _queues[k1]->size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
    juint sz2 = _queues[k2]->size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
    if (sz2 > sz1) return _queues[k2]->pop_global(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
    else return _queues[k1]->pop_global(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
  } else if (_n == 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
    // Just try the other one.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
    int k = (queue_num + 1) % 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
    return _queues[k]->pop_global(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
    assert(_n == 1, "can't be zero.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
bool GenericTaskQueueSet<E>::peek() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
  // Try all the queues.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
  for (int j = 0; j < _n; j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
    if (_queues[j]->peek())
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   412
// When to terminate from the termination protocol.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   413
class TerminatorTerminator: public CHeapObj {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   414
public:
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   415
  virtual bool should_exit_termination() = 0;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   416
};
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   417
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
// A class to aid in the termination of a set of parallel tasks using
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
// TaskQueueSet's for work stealing.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
class ParallelTaskTerminator: public StackObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
  int _n_threads;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
  TaskQueueSetSuper* _queue_set;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
  jint _offered_termination;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
  bool peek_in_queue_set();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
  virtual void yield();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
  void sleep(uint millis);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
  // "n_threads" is the number of threads to be terminated.  "queue_set" is a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
  // queue sets of work queues of other threads.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
  ParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
  // The current thread has no work, and is ready to terminate if everyone
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
  // else is.  If returns "true", all threads are terminated.  If returns
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
  // "false", available work has been observed in one of the task queues,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
  // so the global task is not complete.
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   442
  bool offer_termination() {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   443
    return offer_termination(NULL);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   444
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   445
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   446
  // As above, but it also terminates of the should_exit_termination()
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   447
  // method of the terminator parameter returns true. If terminator is
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   448
  // NULL, then it is ignored.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   449
  bool offer_termination(TerminatorTerminator* terminator);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
  // Reset the terminator, so that it may be reused again.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
  // The caller is responsible for ensuring that this is done
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
  // in an MT-safe manner, once the previous round of use of
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
  // the terminator is finished.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
  void reset_for_reuse();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
#define SIMPLE_STACK 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
template<class E> inline bool GenericTaskQueue<E>::push(E t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
#if SIMPLE_STACK
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
  juint localBot = _bottom;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
  if (_bottom < max_elems()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
    _elems[localBot] = t;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
    _bottom = localBot + 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
  juint localBot = _bottom;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
  assert((localBot >= 0) && (localBot < n()), "_bottom out of range.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
  jushort top = get_top();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
  juint dirty_n_elems = dirty_size(localBot, top);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
  assert((dirty_n_elems >= 0) && (dirty_n_elems < n()),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
         "n_elems out of range.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
  if (dirty_n_elems < max_elems()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
    _elems[localBot] = t;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
    _bottom = increment_index(localBot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
    return push_slow(t, dirty_n_elems);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
template<class E> inline bool GenericTaskQueue<E>::pop_local(E& t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
#if SIMPLE_STACK
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
  juint localBot = _bottom;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
  assert(localBot > 0, "precondition.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
  localBot--;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
  t = _elems[localBot];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
  _bottom = localBot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
  juint localBot = _bottom;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
  // This value cannot be n-1.  That can only occur as a result of
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
  // the assignment to bottom in this method.  If it does, this method
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
  // resets the size( to 0 before the next call (which is sequential,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
  // since this is pop_local.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
  juint dirty_n_elems = dirty_size(localBot, get_top());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
  assert(dirty_n_elems != n() - 1, "Shouldn't be possible...");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
  if (dirty_n_elems == 0) return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
  localBot = decrement_index(localBot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
  _bottom = localBot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
  // This is necessary to prevent any read below from being reordered
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
  // before the store just above.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
  OrderAccess::fence();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
  t = _elems[localBot];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
  // This is a second read of "age"; the "size()" above is the first.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
  // If there's still at least one element in the queue, based on the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
  // "_bottom" and "age" we've read, then there can be no interference with
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
  // a "pop_global" operation, and we're done.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
  juint tp = get_top();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
  if (size(localBot, tp) > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
    assert(dirty_size(localBot, tp) != n() - 1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
           "Shouldn't be possible...");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
    // Otherwise, the queue contained exactly one element; we take the slow
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
    // path.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
    return pop_local_slow(localBot, get_age());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
typedef oop Task;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
typedef GenericTaskQueue<Task>         OopTaskQueue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
typedef GenericTaskQueueSet<Task>      OopTaskQueueSet;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
360
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   532
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   533
#define COMPRESSED_OOP_MASK  1
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   534
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   535
// This is a container class for either an oop* or a narrowOop*.
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   536
// Both are pushed onto a task queue and the consumer will test is_narrow()
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   537
// to determine which should be processed.
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   538
class StarTask {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   539
  void*  _holder;        // either union oop* or narrowOop*
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   540
 public:
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   541
  StarTask(narrowOop *p) { _holder = (void *)((uintptr_t)p | COMPRESSED_OOP_MASK); }
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   542
  StarTask(oop *p)       { _holder = (void*)p; }
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   543
  StarTask()             { _holder = NULL; }
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   544
  operator oop*()        { return (oop*)_holder; }
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   545
  operator narrowOop*()  {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   546
    return (narrowOop*)((uintptr_t)_holder & ~COMPRESSED_OOP_MASK);
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   547
  }
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   548
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   549
  // Operators to preserve const/volatile in assignments required by gcc
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   550
  void operator=(const volatile StarTask& t) volatile { _holder = t._holder; }
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   551
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   552
  bool is_narrow() const {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   553
    return (((uintptr_t)_holder & COMPRESSED_OOP_MASK) != 0);
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   554
  }
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   555
};
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   556
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
typedef GenericTaskQueue<StarTask>     OopStarTaskQueue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
typedef GenericTaskQueueSet<StarTask>  OopStarTaskQueueSet;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   560
typedef size_t RegionTask;  // index for region
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   561
typedef GenericTaskQueue<RegionTask>    RegionTaskQueue;
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   562
typedef GenericTaskQueueSet<RegionTask> RegionTaskQueueSet;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   564
class RegionTaskQueueWithOverflow: public CHeapObj {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
 protected:
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   566
  RegionTaskQueue              _region_queue;
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   567
  GrowableArray<RegionTask>*   _overflow_stack;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
 public:
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   570
  RegionTaskQueueWithOverflow() : _overflow_stack(NULL) {}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
  // Initialize both stealable queue and overflow
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
  void initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
  // Save first to stealable queue and then to overflow
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   574
  void save(RegionTask t);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
  // Retrieve first from overflow and then from stealable queue
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   576
  bool retrieve(RegionTask& region_index);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
  // Retrieve from stealable queue
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   578
  bool retrieve_from_stealable_queue(RegionTask& region_index);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
  // Retrieve from overflow
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   580
  bool retrieve_from_overflow(RegionTask& region_index);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
  bool is_empty();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
  bool stealable_is_empty();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
  bool overflow_is_empty();
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   584
  juint stealable_size() { return _region_queue.size(); }
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   585
  RegionTaskQueue* task_queue() { return &_region_queue; }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   588
#define USE_RegionTaskQueueWithOverflow