hotspot/src/share/vm/utilities/taskqueue.hpp
author coleenp
Sun, 13 Apr 2008 17:43:42 -0400
changeset 360 21d113ecbf6a
parent 1 489c9b5090e2
child 670 ddf3e9583f2f
child 1374 4c24294029a9
permissions -rw-r--r--
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes Summary: Compressed oops in instances, arrays, and headers. Code contributors are coleenp, phh, never, swamyv Reviewed-by: jmasa, kamg, acorn, tbell, kvn, rasbold
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2001-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  // Maximum number of elements allowed in the queue.  This is two less
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  // than the actual queue size, for somewhat complicated reasons.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  juint max_elems() { return n() - 2; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
template<class E> class GenericTaskQueue: public TaskQueueSuper {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  // Slow paths for push, pop_local.  (pop_global has no fast path.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  bool push_slow(E t, juint dirty_n_elems);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  bool pop_local_slow(juint localBot, Age oldAge);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  // Initializes the queue to empty.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  GenericTaskQueue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  void initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  // Push the task "t" on the queue.  Returns "false" iff the queue is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  // full.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  inline bool push(E t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  // If succeeds in claiming a task (from the 'local' end, that is, the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  // most recently pushed task), returns "true" and sets "t" to that task.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  // Otherwise, the queue is empty and returns false.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  inline bool pop_local(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 'global' end, that is, the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  // least 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
  bool pop_global(E& t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
  // Delete any resource associated with the queue.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  ~GenericTaskQueue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  // Element array.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  volatile E* _elems;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
GenericTaskQueue<E>::GenericTaskQueue():TaskQueueSuper() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  assert(sizeof(Age) == sizeof(jint), "Depends on this.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
void GenericTaskQueue<E>::initialize() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
  _elems = NEW_C_HEAP_ARRAY(E, n());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  guarantee(_elems != NULL, "Allocation failed.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
bool GenericTaskQueue<E>::push_slow(E t, juint dirty_n_elems) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  if (dirty_n_elems == n() - 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
    // Actually means 0, so do the push.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
    juint localBot = _bottom;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
    _elems[localBot] = t;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
    _bottom = increment_index(localBot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
  } else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
bool GenericTaskQueue<E>::
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
pop_local_slow(juint localBot, Age oldAge) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  // This queue was observed to contain exactly one element; either this
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  // thread will claim it, or a competing "pop_global".  In either case,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  // the queue will be logically empty afterwards.  Create a new Age value
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
  // that represents the empty queue for the given value of "_bottom".  (We
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  // must also increment "tag" because of the case where "bottom == 1",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
  // "top == 0".  A pop_global could read the queue element in that case,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
  // then have the owner thread do a pop followed by another push.  Without
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
  // the incrementing of "tag", the pop_global's CAS could succeed,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  // allowing it to believe it has claimed the stale element.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
  Age newAge;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
  newAge._top = localBot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
  newAge._tag = oldAge.tag() + 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  // Perhaps a competing pop_global has already incremented "top", in which
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  // case it wins the element.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  if (localBot == oldAge.top()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
    Age tempAge;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
    // No competing pop_global has yet incremented "top"; we'll try to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
    // install new_age, thus claiming the element.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
    assert(sizeof(Age) == sizeof(jint) && sizeof(jint) == sizeof(juint),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
           "Assumption about CAS unit.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
    *(jint*)&tempAge = Atomic::cmpxchg(*(jint*)&newAge, (volatile jint*)&_age, *(jint*)&oldAge);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
    if (tempAge == oldAge) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
      // We win.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
      assert(dirty_size(localBot, get_top()) != n() - 1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
             "Shouldn't be possible...");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  // We fail; a completing pop_global gets the element.  But the queue is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
  // empty (and top is greater than bottom.)  Fix this representation of
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  // the empty queue to become the canonical one.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  set_age(newAge);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
  assert(dirty_size(localBot, get_top()) != n() - 1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
         "Shouldn't be possible...");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
bool GenericTaskQueue<E>::pop_global(E& t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  Age newAge;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
  Age oldAge = get_age();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
  juint localBot = _bottom;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
  juint n_elems = size(localBot, oldAge.top());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
  if (n_elems == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
  t = _elems[oldAge.top()];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
  newAge = oldAge;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
  newAge._top = increment_index(newAge.top());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
  if ( newAge._top == 0 ) newAge._tag++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
  Age resAge;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
  *(jint*)&resAge = Atomic::cmpxchg(*(jint*)&newAge, (volatile jint*)&_age, *(jint*)&oldAge);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
  // Note that using "_bottom" here might fail, since a pop_local might
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
  // have decremented it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
  assert(dirty_size(localBot, newAge._top) != n() - 1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
         "Shouldn't be possible...");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
  return (resAge == oldAge);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
GenericTaskQueue<E>::~GenericTaskQueue() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
  FREE_C_HEAP_ARRAY(E, _elems);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
// Inherits the typedef of "Task" from above.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
class TaskQueueSetSuper: public CHeapObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
  static int randomParkAndMiller(int* seed0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
  // Returns "true" if some TaskQueue in the set contains a task.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
  virtual bool peek() = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
template<class E> class GenericTaskQueueSet: public TaskQueueSetSuper {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
  int _n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
  GenericTaskQueue<E>** _queues;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
  GenericTaskQueueSet(int n) : _n(n) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
    typedef GenericTaskQueue<E>* GenericTaskQueuePtr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
    _queues = NEW_C_HEAP_ARRAY(GenericTaskQueuePtr, n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
    guarantee(_queues != NULL, "Allocation failure.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
    for (int i = 0; i < n; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
      _queues[i] = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  bool steal_1_random(int queue_num, int* seed, E& t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
  bool steal_best_of_2(int queue_num, int* seed, E& t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
  bool steal_best_of_all(int queue_num, int* seed, E& t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  void register_queue(int i, GenericTaskQueue<E>* q);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
  GenericTaskQueue<E>* queue(int n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
  // The thread with queue number "queue_num" (and whose random number seed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
  // is at "seed") is trying to steal a task from some other queue.  (It
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
  // may try several queues, according to some configuration parameter.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
  // If some steal succeeds, returns "true" and sets "t" the stolen task,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
  // otherwise returns false.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  bool steal(int queue_num, int* seed, E& t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
  bool peek();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
void GenericTaskQueueSet<E>::register_queue(int i, GenericTaskQueue<E>* q) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
  assert(0 <= i && i < _n, "index out of range.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
  _queues[i] = q;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
GenericTaskQueue<E>* GenericTaskQueueSet<E>::queue(int i) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
  return _queues[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
bool GenericTaskQueueSet<E>::steal(int queue_num, int* seed, E& t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
  for (int i = 0; i < 2 * _n; i++)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
    if (steal_best_of_2(queue_num, seed, t))
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
bool GenericTaskQueueSet<E>::steal_best_of_all(int queue_num, int* seed, E& t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  if (_n > 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
    int best_k;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
    jint best_sz = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
    for (int k = 0; k < _n; k++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
      if (k == queue_num) continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
      jint sz = _queues[k]->size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
      if (sz > best_sz) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
        best_sz = sz;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
        best_k = k;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
    return best_sz > 0 && _queues[best_k]->pop_global(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
  } else if (_n == 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
    // Just try the other one.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
    int k = (queue_num + 1) % 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
    return _queues[k]->pop_global(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
    assert(_n == 1, "can't be zero.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
bool GenericTaskQueueSet<E>::steal_1_random(int queue_num, int* seed, E& t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
  if (_n > 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
    int k = queue_num;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
    while (k == queue_num) k = randomParkAndMiller(seed) % _n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
    return _queues[2]->pop_global(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
  } else if (_n == 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
    // Just try the other one.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
    int k = (queue_num + 1) % 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
    return _queues[k]->pop_global(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
    assert(_n == 1, "can't be zero.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
bool GenericTaskQueueSet<E>::steal_best_of_2(int queue_num, int* seed, E& t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
  if (_n > 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
    int k1 = queue_num;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
    while (k1 == queue_num) k1 = randomParkAndMiller(seed) % _n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
    int k2 = queue_num;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
    while (k2 == queue_num || k2 == k1) k2 = randomParkAndMiller(seed) % _n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
    // Sample both and try the larger.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
    juint sz1 = _queues[k1]->size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
    juint sz2 = _queues[k2]->size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
    if (sz2 > sz1) return _queues[k2]->pop_global(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
    else return _queues[k1]->pop_global(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
  } else if (_n == 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
    // Just try the other one.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
    int k = (queue_num + 1) % 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
    return _queues[k]->pop_global(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
    assert(_n == 1, "can't be zero.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
bool GenericTaskQueueSet<E>::peek() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
  // Try all the queues.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
  for (int j = 0; j < _n; j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
    if (_queues[j]->peek())
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
// A class to aid in the termination of a set of parallel tasks using
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
// TaskQueueSet's for work stealing.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
class ParallelTaskTerminator: public StackObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
  int _n_threads;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
  TaskQueueSetSuper* _queue_set;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
  jint _offered_termination;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
  bool peek_in_queue_set();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
  virtual void yield();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
  void sleep(uint millis);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
  // "n_threads" is the number of threads to be terminated.  "queue_set" is a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
  // queue sets of work queues of other threads.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
  ParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
  // The current thread has no work, and is ready to terminate if everyone
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
  // else is.  If returns "true", all threads are terminated.  If returns
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
  // "false", available work has been observed in one of the task queues,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
  // so the global task is not complete.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
  bool offer_termination();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
  // Reset the terminator, so that it may be reused again.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
  // The caller is responsible for ensuring that this is done
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
  // in an MT-safe manner, once the previous round of use of
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
  // the terminator is finished.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
  void reset_for_reuse();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
#define SIMPLE_STACK 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
template<class E> inline bool GenericTaskQueue<E>::push(E t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
#if SIMPLE_STACK
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
  juint localBot = _bottom;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
  if (_bottom < max_elems()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
    _elems[localBot] = t;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
    _bottom = localBot + 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
  juint localBot = _bottom;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
  assert((localBot >= 0) && (localBot < n()), "_bottom out of range.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
  jushort top = get_top();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
  juint dirty_n_elems = dirty_size(localBot, top);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
  assert((dirty_n_elems >= 0) && (dirty_n_elems < n()),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
         "n_elems out of range.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
  if (dirty_n_elems < max_elems()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
    _elems[localBot] = t;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
    _bottom = increment_index(localBot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
    return push_slow(t, dirty_n_elems);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
template<class E> inline bool GenericTaskQueue<E>::pop_local(E& t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
#if SIMPLE_STACK
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
  juint localBot = _bottom;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
  assert(localBot > 0, "precondition.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
  localBot--;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
  t = _elems[localBot];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
  _bottom = localBot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
  juint localBot = _bottom;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
  // This value cannot be n-1.  That can only occur as a result of
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
  // the assignment to bottom in this method.  If it does, this method
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
  // resets the size( to 0 before the next call (which is sequential,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
  // since this is pop_local.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
  juint dirty_n_elems = dirty_size(localBot, get_top());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
  assert(dirty_n_elems != n() - 1, "Shouldn't be possible...");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
  if (dirty_n_elems == 0) return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
  localBot = decrement_index(localBot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
  _bottom = localBot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
  // This is necessary to prevent any read below from being reordered
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
  // before the store just above.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
  OrderAccess::fence();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
  t = _elems[localBot];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
  // This is a second read of "age"; the "size()" above is the first.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
  // If there's still at least one element in the queue, based on the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
  // "_bottom" and "age" we've read, then there can be no interference with
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
  // a "pop_global" operation, and we're done.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
  juint tp = get_top();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
  if (size(localBot, tp) > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
    assert(dirty_size(localBot, tp) != n() - 1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
           "Shouldn't be possible...");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
    // Otherwise, the queue contained exactly one element; we take the slow
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
    // path.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
    return pop_local_slow(localBot, get_age());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
typedef oop Task;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
typedef GenericTaskQueue<Task>         OopTaskQueue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
typedef GenericTaskQueueSet<Task>      OopTaskQueueSet;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
360
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   493
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   494
#define COMPRESSED_OOP_MASK  1
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   495
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   496
// 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
   497
// 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
   498
// 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
   499
class StarTask {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   500
  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
   501
 public:
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   502
  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
   503
  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
   504
  StarTask()             { _holder = NULL; }
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   505
  operator oop*()        { return (oop*)_holder; }
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   506
  operator narrowOop*()  {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   507
    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
   508
  }
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   509
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   510
  // 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
   511
  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
   512
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   513
  bool is_narrow() const {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   514
    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
   515
  }
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   516
};
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   517
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
typedef GenericTaskQueue<StarTask>     OopStarTaskQueue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
typedef GenericTaskQueueSet<StarTask>  OopStarTaskQueueSet;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
typedef size_t ChunkTask;  // index for chunk
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
typedef GenericTaskQueue<ChunkTask>    ChunkTaskQueue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
typedef GenericTaskQueueSet<ChunkTask> ChunkTaskQueueSet;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
class ChunkTaskQueueWithOverflow: public CHeapObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
  ChunkTaskQueue              _chunk_queue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
  GrowableArray<ChunkTask>*   _overflow_stack;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
  ChunkTaskQueueWithOverflow() : _overflow_stack(NULL) {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
  // Initialize both stealable queue and overflow
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
  void initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
  // Save first to stealable queue and then to overflow
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
  void save(ChunkTask t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
  // Retrieve first from overflow and then from stealable queue
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
  bool retrieve(ChunkTask& chunk_index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
  // Retrieve from stealable queue
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
  bool retrieve_from_stealable_queue(ChunkTask& chunk_index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
  // Retrieve from overflow
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
  bool retrieve_from_overflow(ChunkTask& chunk_index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
  bool is_empty();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
  bool stealable_is_empty();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
  bool overflow_is_empty();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
  juint stealable_size() { return _chunk_queue.size(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
  ChunkTaskQueue* task_queue() { return &_chunk_queue; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
#define USE_ChunkTaskQueueWithOverflow