1/*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25#include "precompiled.hpp"
26#include "memory/allocation.inline.hpp"
27#include "runtime/thread.hpp"
28
29// Lifecycle management for TSM ParkEvents.
30// ParkEvents are type-stable (TSM).
31// In our particular implementation they happen to be immortal.
32//
33// We manage concurrency on the FreeList with a CAS-based
34// detach-modify-reattach idiom that avoids the ABA problems
35// that would otherwise be present in a simple CAS-based
36// push-pop implementation. (push-one and pop-all)
37//
38// Caveat: Allocate() and Release() may be called from threads
39// other than the thread associated with the Event!
40// If we need to call Allocate() when running as the thread in
41// question then look for the PD calls to initialize native TLS.
42// Native TLS (Win32/Linux/Solaris) can only be initialized or
43// accessed by the associated thread.
44// See also pd_initialize().
45//
46// Note that we could defer associating a ParkEvent with a thread
47// until the 1st time the thread calls park(). unpark() calls to
48// an unprovisioned thread would be ignored. The first park() call
49// for a thread would allocate and associate a ParkEvent and return
50// immediately.
51
52volatile int ParkEvent::ListLock = 0 ;
53ParkEvent * volatile ParkEvent::FreeList = NULL ;
54
55ParkEvent * ParkEvent::Allocate (Thread * t) {
56 // In rare cases -- JVM_RawMonitor* operations -- we can find t == null.
57 ParkEvent * ev ;
58
59 // Start by trying to recycle an existing but unassociated
60 // ParkEvent from the global free list.
61 // Using a spin lock since we are part of the mutex impl.
62 // 8028280: using concurrent free list without memory management can leak
63 // pretty badly it turns out.
64 Thread::SpinAcquire(&ListLock, "ParkEventFreeListAllocate");
65 {
66 ev = FreeList;
67 if (ev != NULL) {
68 FreeList = ev->FreeNext;
69 }
70 }
71 Thread::SpinRelease(&ListLock);
72
73 if (ev != NULL) {
74 guarantee (ev->AssociatedWith == NULL, "invariant") ;
75 } else {
76 // Do this the hard way -- materialize a new ParkEvent.
77 ev = new ParkEvent () ;
78 guarantee ((intptr_t(ev) & 0xFF) == 0, "invariant") ;
79 }
80 ev->reset() ; // courtesy to caller
81 ev->AssociatedWith = t ; // Associate ev with t
82 ev->FreeNext = NULL ;
83 return ev ;
84}
85
86void ParkEvent::Release (ParkEvent * ev) {
87 if (ev == NULL) return ;
88 guarantee (ev->FreeNext == NULL , "invariant") ;
89 ev->AssociatedWith = NULL ;
90 // Note that if we didn't have the TSM/immortal constraint, then
91 // when reattaching we could trim the list.
92 Thread::SpinAcquire(&ListLock, "ParkEventFreeListRelease");
93 {
94 ev->FreeNext = FreeList;
95 FreeList = ev;
96 }
97 Thread::SpinRelease(&ListLock);
98}
99
100// Override operator new and delete so we can ensure that the
101// least significant byte of ParkEvent addresses is 0.
102// Beware that excessive address alignment is undesirable
103// as it can result in D$ index usage imbalance as
104// well as bank access imbalance on Niagara-like platforms,
105// although Niagara's hash function should help.
106
107void * ParkEvent::operator new (size_t sz) throw() {
108 return (void *) ((intptr_t (AllocateHeap(sz + 256, mtInternal, CALLER_PC)) + 256) & -256) ;
109}
110
111void ParkEvent::operator delete (void * a) {
112 // ParkEvents are type-stable and immortal ...
113 ShouldNotReachHere();
114}
115
116
117// 6399321 As a temporary measure we copied & modified the ParkEvent::
118// allocate() and release() code for use by Parkers. The Parker:: forms
119// will eventually be removed as we consolidate and shift over to ParkEvents
120// for both builtin synchronization and JSR166 operations.
121
122volatile int Parker::ListLock = 0 ;
123Parker * volatile Parker::FreeList = NULL ;
124
125Parker * Parker::Allocate (JavaThread * t) {
126 guarantee (t != NULL, "invariant") ;
127 Parker * p ;
128
129 // Start by trying to recycle an existing but unassociated
130 // Parker from the global free list.
131 // 8028280: using concurrent free list without memory management can leak
132 // pretty badly it turns out.
133 Thread::SpinAcquire(&ListLock, "ParkerFreeListAllocate");
134 {
135 p = FreeList;
136 if (p != NULL) {
137 FreeList = p->FreeNext;
138 }
139 }
140 Thread::SpinRelease(&ListLock);
141
142 if (p != NULL) {
143 guarantee (p->AssociatedWith == NULL, "invariant") ;
144 } else {
145 // Do this the hard way -- materialize a new Parker..
146 p = new Parker() ;
147 }
148 p->AssociatedWith = t ; // Associate p with t
149 p->FreeNext = NULL ;
150 return p ;
151}
152
153
154void Parker::Release (Parker * p) {
155 if (p == NULL) return ;
156 guarantee (p->AssociatedWith != NULL, "invariant") ;
157 guarantee (p->FreeNext == NULL , "invariant") ;
158 p->AssociatedWith = NULL ;
159
160 Thread::SpinAcquire(&ListLock, "ParkerFreeListRelease");
161 {
162 p->FreeNext = FreeList;
163 FreeList = p;
164 }
165 Thread::SpinRelease(&ListLock);
166}
167
168