1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/runtime/park.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,168 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "runtime/thread.hpp" 1.30 + 1.31 + 1.32 + 1.33 +// Lifecycle management for TSM ParkEvents. 1.34 +// ParkEvents are type-stable (TSM). 1.35 +// In our particular implementation they happen to be immortal. 1.36 +// 1.37 +// We manage concurrency on the FreeList with a CAS-based 1.38 +// detach-modify-reattach idiom that avoids the ABA problems 1.39 +// that would otherwise be present in a simple CAS-based 1.40 +// push-pop implementation. (push-one and pop-all) 1.41 +// 1.42 +// Caveat: Allocate() and Release() may be called from threads 1.43 +// other than the thread associated with the Event! 1.44 +// If we need to call Allocate() when running as the thread in 1.45 +// question then look for the PD calls to initialize native TLS. 1.46 +// Native TLS (Win32/Linux/Solaris) can only be initialized or 1.47 +// accessed by the associated thread. 1.48 +// See also pd_initialize(). 1.49 +// 1.50 +// Note that we could defer associating a ParkEvent with a thread 1.51 +// until the 1st time the thread calls park(). unpark() calls to 1.52 +// an unprovisioned thread would be ignored. The first park() call 1.53 +// for a thread would allocate and associate a ParkEvent and return 1.54 +// immediately. 1.55 + 1.56 +volatile int ParkEvent::ListLock = 0 ; 1.57 +ParkEvent * volatile ParkEvent::FreeList = NULL ; 1.58 + 1.59 +ParkEvent * ParkEvent::Allocate (Thread * t) { 1.60 + // In rare cases -- JVM_RawMonitor* operations -- we can find t == null. 1.61 + ParkEvent * ev ; 1.62 + 1.63 + // Start by trying to recycle an existing but unassociated 1.64 + // ParkEvent from the global free list. 1.65 + // Using a spin lock since we are part of the mutex impl. 1.66 + // 8028280: using concurrent free list without memory management can leak 1.67 + // pretty badly it turns out. 1.68 + Thread::SpinAcquire(&ListLock, "ParkEventFreeListAllocate"); 1.69 + { 1.70 + ev = FreeList; 1.71 + if (ev != NULL) { 1.72 + FreeList = ev->FreeNext; 1.73 + } 1.74 + } 1.75 + Thread::SpinRelease(&ListLock); 1.76 + 1.77 + if (ev != NULL) { 1.78 + guarantee (ev->AssociatedWith == NULL, "invariant") ; 1.79 + } else { 1.80 + // Do this the hard way -- materialize a new ParkEvent. 1.81 + ev = new ParkEvent () ; 1.82 + guarantee ((intptr_t(ev) & 0xFF) == 0, "invariant") ; 1.83 + } 1.84 + ev->reset() ; // courtesy to caller 1.85 + ev->AssociatedWith = t ; // Associate ev with t 1.86 + ev->FreeNext = NULL ; 1.87 + return ev ; 1.88 +} 1.89 + 1.90 +void ParkEvent::Release (ParkEvent * ev) { 1.91 + if (ev == NULL) return ; 1.92 + guarantee (ev->FreeNext == NULL , "invariant") ; 1.93 + ev->AssociatedWith = NULL ; 1.94 + // Note that if we didn't have the TSM/immortal constraint, then 1.95 + // when reattaching we could trim the list. 1.96 + Thread::SpinAcquire(&ListLock, "ParkEventFreeListRelease"); 1.97 + { 1.98 + ev->FreeNext = FreeList; 1.99 + FreeList = ev; 1.100 + } 1.101 + Thread::SpinRelease(&ListLock); 1.102 +} 1.103 + 1.104 +// Override operator new and delete so we can ensure that the 1.105 +// least significant byte of ParkEvent addresses is 0. 1.106 +// Beware that excessive address alignment is undesirable 1.107 +// as it can result in D$ index usage imbalance as 1.108 +// well as bank access imbalance on Niagara-like platforms, 1.109 +// although Niagara's hash function should help. 1.110 + 1.111 +void * ParkEvent::operator new (size_t sz) throw() { 1.112 + return (void *) ((intptr_t (AllocateHeap(sz + 256, mtInternal, CALLER_PC)) + 256) & -256) ; 1.113 +} 1.114 + 1.115 +void ParkEvent::operator delete (void * a) { 1.116 + // ParkEvents are type-stable and immortal ... 1.117 + ShouldNotReachHere(); 1.118 +} 1.119 + 1.120 + 1.121 +// 6399321 As a temporary measure we copied & modified the ParkEvent:: 1.122 +// allocate() and release() code for use by Parkers. The Parker:: forms 1.123 +// will eventually be removed as we consolide and shift over to ParkEvents 1.124 +// for both builtin synchronization and JSR166 operations. 1.125 + 1.126 +volatile int Parker::ListLock = 0 ; 1.127 +Parker * volatile Parker::FreeList = NULL ; 1.128 + 1.129 +Parker * Parker::Allocate (JavaThread * t) { 1.130 + guarantee (t != NULL, "invariant") ; 1.131 + Parker * p ; 1.132 + 1.133 + // Start by trying to recycle an existing but unassociated 1.134 + // Parker from the global free list. 1.135 + // 8028280: using concurrent free list without memory management can leak 1.136 + // pretty badly it turns out. 1.137 + Thread::SpinAcquire(&ListLock, "ParkerFreeListAllocate"); 1.138 + { 1.139 + p = FreeList; 1.140 + if (p != NULL) { 1.141 + FreeList = p->FreeNext; 1.142 + } 1.143 + } 1.144 + Thread::SpinRelease(&ListLock); 1.145 + 1.146 + if (p != NULL) { 1.147 + guarantee (p->AssociatedWith == NULL, "invariant") ; 1.148 + } else { 1.149 + // Do this the hard way -- materialize a new Parker.. 1.150 + p = new Parker() ; 1.151 + } 1.152 + p->AssociatedWith = t ; // Associate p with t 1.153 + p->FreeNext = NULL ; 1.154 + return p ; 1.155 +} 1.156 + 1.157 + 1.158 +void Parker::Release (Parker * p) { 1.159 + if (p == NULL) return ; 1.160 + guarantee (p->AssociatedWith != NULL, "invariant") ; 1.161 + guarantee (p->FreeNext == NULL , "invariant") ; 1.162 + p->AssociatedWith = NULL ; 1.163 + 1.164 + Thread::SpinAcquire(&ListLock, "ParkerFreeListRelease"); 1.165 + { 1.166 + p->FreeNext = FreeList; 1.167 + FreeList = p; 1.168 + } 1.169 + Thread::SpinRelease(&ListLock); 1.170 +} 1.171 +