hotspot/src/share/vm/runtime/park.cpp
author mgerdin
Thu, 23 Feb 2012 14:58:35 +0100
changeset 12095 cc3d6f08a4c4
parent 7397 5b173b4ca846
child 13195 be27e1b6a4b9
permissions -rw-r--r--
7148152: Add whitebox testing API to HotSpot Summary: Add an internal testing API to HotSpot to enable more targeted testing of vm functionality Reviewed-by: phh, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
     1
/*
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
     2
 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
     4
 *
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
     7
 * published by the Free Software Foundation.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
     8
 *
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    13
 * accompanied this code).
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    14
 *
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    18
 *
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    21
 * questions.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    22
 *
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    23
 */
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    25
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6975
diff changeset
    26
#include "runtime/thread.hpp"
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    27
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    28
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    29
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    30
// Lifecycle management for TSM ParkEvents.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    31
// ParkEvents are type-stable (TSM).
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    32
// In our particular implementation they happen to be immortal.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    33
//
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    34
// We manage concurrency on the FreeList with a CAS-based
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    35
// detach-modify-reattach idiom that avoids the ABA problems
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    36
// that would otherwise be present in a simple CAS-based
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    37
// push-pop implementation.   (push-one and pop-all)
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    38
//
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    39
// Caveat: Allocate() and Release() may be called from threads
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    40
// other than the thread associated with the Event!
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    41
// If we need to call Allocate() when running as the thread in
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    42
// question then look for the PD calls to initialize native TLS.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    43
// Native TLS (Win32/Linux/Solaris) can only be initialized or
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    44
// accessed by the associated thread.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    45
// See also pd_initialize().
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    46
//
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    47
// Note that we could defer associating a ParkEvent with a thread
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    48
// until the 1st time the thread calls park().  unpark() calls to
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    49
// an unprovisioned thread would be ignored.  The first park() call
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    50
// for a thread would allocate and associate a ParkEvent and return
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    51
// immediately.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    52
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    53
volatile int ParkEvent::ListLock = 0 ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    54
ParkEvent * volatile ParkEvent::FreeList = NULL ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    55
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    56
ParkEvent * ParkEvent::Allocate (Thread * t) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    57
  // In rare cases -- JVM_RawMonitor* operations -- we can find t == null.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    58
  ParkEvent * ev ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    59
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    60
  // Start by trying to recycle an existing but unassociated
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    61
  // ParkEvent from the global free list.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    62
  for (;;) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    63
    ev = FreeList ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    64
    if (ev == NULL) break ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    65
    // 1: Detach - sequester or privatize the list
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    66
    // Tantamount to ev = Swap (&FreeList, NULL)
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    67
    if (Atomic::cmpxchg_ptr (NULL, &FreeList, ev) != ev) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    68
       continue ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    69
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    70
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    71
    // We've detached the list.  The list in-hand is now
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    72
    // local to this thread.   This thread can operate on the
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    73
    // list without risk of interference from other threads.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    74
    // 2: Extract -- pop the 1st element from the list.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    75
    ParkEvent * List = ev->FreeNext ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    76
    if (List == NULL) break ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    77
    for (;;) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    78
        // 3: Try to reattach the residual list
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    79
        guarantee (List != NULL, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    80
        ParkEvent * Arv =  (ParkEvent *) Atomic::cmpxchg_ptr (List, &FreeList, NULL) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    81
        if (Arv == NULL) break ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    82
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    83
        // New nodes arrived.  Try to detach the recent arrivals.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    84
        if (Atomic::cmpxchg_ptr (NULL, &FreeList, Arv) != Arv) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    85
            continue ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    86
        }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    87
        guarantee (Arv != NULL, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    88
        // 4: Merge Arv into List
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    89
        ParkEvent * Tail = List ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    90
        while (Tail->FreeNext != NULL) Tail = Tail->FreeNext ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    91
        Tail->FreeNext = Arv ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    92
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    93
    break ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    94
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    95
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    96
  if (ev != NULL) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    97
    guarantee (ev->AssociatedWith == NULL, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    98
  } else {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    99
    // Do this the hard way -- materialize a new ParkEvent.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   100
    // In rare cases an allocating thread might detach a long list --
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   101
    // installing null into FreeList -- and then stall or be obstructed.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   102
    // A 2nd thread calling Allocate() would see FreeList == null.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   103
    // The list held privately by the 1st thread is unavailable to the 2nd thread.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   104
    // In that case the 2nd thread would have to materialize a new ParkEvent,
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   105
    // even though free ParkEvents existed in the system.  In this case we end up
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   106
    // with more ParkEvents in circulation than we need, but the race is
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   107
    // rare and the outcome is benign.  Ideally, the # of extant ParkEvents
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   108
    // is equal to the maximum # of threads that existed at any one time.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   109
    // Because of the race mentioned above, segments of the freelist
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   110
    // can be transiently inaccessible.  At worst we may end up with the
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   111
    // # of ParkEvents in circulation slightly above the ideal.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   112
    // Note that if we didn't have the TSM/immortal constraint, then
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   113
    // when reattaching, above, we could trim the list.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   114
    ev = new ParkEvent () ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   115
    guarantee ((intptr_t(ev) & 0xFF) == 0, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   116
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   117
  ev->reset() ;                     // courtesy to caller
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   118
  ev->AssociatedWith = t ;          // Associate ev with t
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   119
  ev->FreeNext       = NULL ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   120
  return ev ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   121
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   122
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   123
void ParkEvent::Release (ParkEvent * ev) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   124
  if (ev == NULL) return ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   125
  guarantee (ev->FreeNext == NULL      , "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   126
  ev->AssociatedWith = NULL ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   127
  for (;;) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   128
    // Push ev onto FreeList
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   129
    // The mechanism is "half" lock-free.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   130
    ParkEvent * List = FreeList ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   131
    ev->FreeNext = List ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   132
    if (Atomic::cmpxchg_ptr (ev, &FreeList, List) == List) break ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   133
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   134
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   135
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   136
// Override operator new and delete so we can ensure that the
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   137
// least significant byte of ParkEvent addresses is 0.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   138
// Beware that excessive address alignment is undesirable
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   139
// as it can result in D$ index usage imbalance as
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   140
// well as bank access imbalance on Niagara-like platforms,
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   141
// although Niagara's hash function should help.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   142
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   143
void * ParkEvent::operator new (size_t sz) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   144
  return (void *) ((intptr_t (CHeapObj::operator new (sz + 256)) + 256) & -256) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   145
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   146
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   147
void ParkEvent::operator delete (void * a) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   148
  // ParkEvents are type-stable and immortal ...
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   149
  ShouldNotReachHere();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   150
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   151
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   152
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   153
// 6399321 As a temporary measure we copied & modified the ParkEvent::
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   154
// allocate() and release() code for use by Parkers.  The Parker:: forms
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   155
// will eventually be removed as we consolide and shift over to ParkEvents
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   156
// for both builtin synchronization and JSR166 operations.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   157
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   158
volatile int Parker::ListLock = 0 ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   159
Parker * volatile Parker::FreeList = NULL ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   160
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   161
Parker * Parker::Allocate (JavaThread * t) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   162
  guarantee (t != NULL, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   163
  Parker * p ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   164
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   165
  // Start by trying to recycle an existing but unassociated
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   166
  // Parker from the global free list.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   167
  for (;;) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   168
    p = FreeList ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   169
    if (p  == NULL) break ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   170
    // 1: Detach
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   171
    // Tantamount to p = Swap (&FreeList, NULL)
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   172
    if (Atomic::cmpxchg_ptr (NULL, &FreeList, p) != p) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   173
       continue ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   174
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   175
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   176
    // We've detached the list.  The list in-hand is now
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   177
    // local to this thread.   This thread can operate on the
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   178
    // list without risk of interference from other threads.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   179
    // 2: Extract -- pop the 1st element from the list.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   180
    Parker * List = p->FreeNext ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   181
    if (List == NULL) break ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   182
    for (;;) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   183
        // 3: Try to reattach the residual list
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   184
        guarantee (List != NULL, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   185
        Parker * Arv =  (Parker *) Atomic::cmpxchg_ptr (List, &FreeList, NULL) ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   186
        if (Arv == NULL) break ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   187
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   188
        // New nodes arrived.  Try to detach the recent arrivals.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   189
        if (Atomic::cmpxchg_ptr (NULL, &FreeList, Arv) != Arv) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   190
            continue ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   191
        }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   192
        guarantee (Arv != NULL, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   193
        // 4: Merge Arv into List
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   194
        Parker * Tail = List ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   195
        while (Tail->FreeNext != NULL) Tail = Tail->FreeNext ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   196
        Tail->FreeNext = Arv ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   197
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   198
    break ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   199
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   200
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   201
  if (p != NULL) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   202
    guarantee (p->AssociatedWith == NULL, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   203
  } else {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   204
    // Do this the hard way -- materialize a new Parker..
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   205
    // In rare cases an allocating thread might detach
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   206
    // a long list -- installing null into FreeList --and
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   207
    // then stall.  Another thread calling Allocate() would see
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   208
    // FreeList == null and then invoke the ctor.  In this case we
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   209
    // end up with more Parkers in circulation than we need, but
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   210
    // the race is rare and the outcome is benign.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   211
    // Ideally, the # of extant Parkers is equal to the
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   212
    // maximum # of threads that existed at any one time.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   213
    // Because of the race mentioned above, segments of the
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   214
    // freelist can be transiently inaccessible.  At worst
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   215
    // we may end up with the # of Parkers in circulation
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   216
    // slightly above the ideal.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   217
    p = new Parker() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   218
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   219
  p->AssociatedWith = t ;          // Associate p with t
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   220
  p->FreeNext       = NULL ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   221
  return p ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   222
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   223
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   224
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   225
void Parker::Release (Parker * p) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   226
  if (p == NULL) return ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   227
  guarantee (p->AssociatedWith != NULL, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   228
  guarantee (p->FreeNext == NULL      , "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   229
  p->AssociatedWith = NULL ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   230
  for (;;) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   231
    // Push p onto FreeList
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   232
    Parker * List = FreeList ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   233
    p->FreeNext = List ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   234
    if (Atomic::cmpxchg_ptr (p, &FreeList, List) == List) break ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   235
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   236
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   237