src/hotspot/share/runtime/park.cpp
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 22556 hotspot/src/share/vm/runtime/park.cpp@a17351e8c2c2
child 48157 7c4d43c26352
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
     1
/*
19696
bd5a0131bde1 8021954: VM SIGSEGV during classloading on MacOS; hs_err_pid file produced
coleenp
parents: 13963
diff changeset
     2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
6975
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.
22533
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
    62
  // Using a spin lock since we are part of the mutex impl.
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
    63
  // 8028280: using concurrent free list without memory management can leak
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
    64
  // pretty badly it turns out.
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
    65
  Thread::SpinAcquire(&ListLock, "ParkEventFreeListAllocate");
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
    66
  {
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
    67
    ev = FreeList;
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
    68
    if (ev != NULL) {
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
    69
      FreeList = ev->FreeNext;
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    70
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    71
  }
22533
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
    72
  Thread::SpinRelease(&ListLock);
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    73
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    74
  if (ev != NULL) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    75
    guarantee (ev->AssociatedWith == NULL, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    76
  } else {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    77
    // Do this the hard way -- materialize a new ParkEvent.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    78
    ev = new ParkEvent () ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    79
    guarantee ((intptr_t(ev) & 0xFF) == 0, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    80
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    81
  ev->reset() ;                     // courtesy to caller
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    82
  ev->AssociatedWith = t ;          // Associate ev with t
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    83
  ev->FreeNext       = NULL ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    84
  return ev ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    85
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    86
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    87
void ParkEvent::Release (ParkEvent * ev) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    88
  if (ev == NULL) return ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    89
  guarantee (ev->FreeNext == NULL      , "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    90
  ev->AssociatedWith = NULL ;
22533
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
    91
  // Note that if we didn't have the TSM/immortal constraint, then
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
    92
  // when reattaching we could trim the list.
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
    93
  Thread::SpinAcquire(&ListLock, "ParkEventFreeListRelease");
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
    94
  {
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
    95
    ev->FreeNext = FreeList;
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
    96
    FreeList = ev;
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    97
  }
22533
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
    98
  Thread::SpinRelease(&ListLock);
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
    99
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   100
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   101
// Override operator new and delete so we can ensure that the
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   102
// least significant byte of ParkEvent addresses is 0.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   103
// Beware that excessive address alignment is undesirable
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   104
// as it can result in D$ index usage imbalance as
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   105
// well as bank access imbalance on Niagara-like platforms,
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   106
// although Niagara's hash function should help.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   107
19696
bd5a0131bde1 8021954: VM SIGSEGV during classloading on MacOS; hs_err_pid file produced
coleenp
parents: 13963
diff changeset
   108
void * ParkEvent::operator new (size_t sz) throw() {
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
   109
  return (void *) ((intptr_t (AllocateHeap(sz + 256, mtInternal, CALLER_PC)) + 256) & -256) ;
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   110
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   111
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   112
void ParkEvent::operator delete (void * a) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   113
  // ParkEvents are type-stable and immortal ...
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   114
  ShouldNotReachHere();
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   115
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   116
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   117
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   118
// 6399321 As a temporary measure we copied & modified the ParkEvent::
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   119
// allocate() and release() code for use by Parkers.  The Parker:: forms
22551
9bf46d16dcc6 8025856: Fix typos in the GC code
jwilhelm
parents: 19696
diff changeset
   120
// will eventually be removed as we consolidate and shift over to ParkEvents
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   121
// for both builtin synchronization and JSR166 operations.
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   122
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   123
volatile int Parker::ListLock = 0 ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   124
Parker * volatile Parker::FreeList = NULL ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   125
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   126
Parker * Parker::Allocate (JavaThread * t) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   127
  guarantee (t != NULL, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   128
  Parker * p ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   129
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   130
  // Start by trying to recycle an existing but unassociated
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   131
  // Parker from the global free list.
22533
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
   132
  // 8028280: using concurrent free list without memory management can leak
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
   133
  // pretty badly it turns out.
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
   134
  Thread::SpinAcquire(&ListLock, "ParkerFreeListAllocate");
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
   135
  {
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
   136
    p = FreeList;
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
   137
    if (p != NULL) {
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
   138
      FreeList = p->FreeNext;
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   139
    }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   140
  }
22533
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
   141
  Thread::SpinRelease(&ListLock);
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   142
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   143
  if (p != NULL) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   144
    guarantee (p->AssociatedWith == NULL, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   145
  } else {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   146
    // Do this the hard way -- materialize a new Parker..
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   147
    p = new Parker() ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   148
  }
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   149
  p->AssociatedWith = t ;          // Associate p with t
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   150
  p->FreeNext       = NULL ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   151
  return p ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   152
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   153
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   154
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   155
void Parker::Release (Parker * p) {
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   156
  if (p == NULL) return ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   157
  guarantee (p->AssociatedWith != NULL, "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   158
  guarantee (p->FreeNext == NULL      , "invariant") ;
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   159
  p->AssociatedWith = NULL ;
22533
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
   160
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
   161
  Thread::SpinAcquire(&ListLock, "ParkerFreeListRelease");
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
   162
  {
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
   163
    p->FreeNext = FreeList;
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
   164
    FreeList = p;
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   165
  }
22533
76088853a2eb 8028280: ParkEvent leak when running modified runThese which only loads classes
dsimms
parents: 19696
diff changeset
   166
  Thread::SpinRelease(&ListLock);
6975
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   167
}
dc9b63952682 6988353: refactor contended sync subsystem
acorn
parents:
diff changeset
   168