1 /*
2 * Copyright (c) 1997, 2019, 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
52 volatile int ParkEvent::ListLock = 0 ;
53 ParkEvent * volatile ParkEvent::FreeList = NULL ;
54
Allocate(Thread * t)55 ParkEvent * ParkEvent::Allocate (Thread * t) {
56 ParkEvent * ev ;
57
58 // Start by trying to recycle an existing but unassociated
59 // ParkEvent from the global free list.
60 // Using a spin lock since we are part of the mutex impl.
61 // 8028280: using concurrent free list without memory management can leak
62 // pretty badly it turns out.
63 Thread::SpinAcquire(&ListLock, "ParkEventFreeListAllocate");
64 {
65 ev = FreeList;
66 if (ev != NULL) {
67 FreeList = ev->FreeNext;
68 }
69 }
70 Thread::SpinRelease(&ListLock);
71
72 if (ev != NULL) {
73 guarantee (ev->AssociatedWith == NULL, "invariant") ;
74 } else {
75 // Do this the hard way -- materialize a new ParkEvent.
76 ev = new ParkEvent () ;
77 guarantee ((intptr_t(ev) & 0xFF) == 0, "invariant") ;
78 }
79 ev->reset() ; // courtesy to caller
80 ev->AssociatedWith = t ; // Associate ev with t
81 ev->FreeNext = NULL ;
82 return ev ;
83 }
84
Release(ParkEvent * ev)85 void ParkEvent::Release (ParkEvent * ev) {
86 if (ev == NULL) return ;
87 guarantee (ev->FreeNext == NULL , "invariant") ;
88 ev->AssociatedWith = NULL ;
89 // Note that if we didn't have the TSM/immortal constraint, then
90 // when reattaching we could trim the list.
91 Thread::SpinAcquire(&ListLock, "ParkEventFreeListRelease");
92 {
93 ev->FreeNext = FreeList;
94 FreeList = ev;
95 }
96 Thread::SpinRelease(&ListLock);
97 }
98
99 // Override operator new and delete so we can ensure that the
100 // least significant byte of ParkEvent addresses is 0.
101 // Beware that excessive address alignment is undesirable
102 // as it can result in D$ index usage imbalance as
103 // well as bank access imbalance on Niagara-like platforms,
104 // although Niagara's hash function should help.
105
operator new(size_t sz)106 void * ParkEvent::operator new (size_t sz) throw() {
107 return (void *) ((intptr_t (AllocateHeap(sz + 256, mtInternal, CALLER_PC)) + 256) & -256) ;
108 }
109
operator delete(void * a)110 void ParkEvent::operator delete (void * a) {
111 // ParkEvents are type-stable and immortal ...
112 ShouldNotReachHere();
113 }
114
115
116 // 6399321 As a temporary measure we copied & modified the ParkEvent::
117 // allocate() and release() code for use by Parkers. The Parker:: forms
118 // will eventually be removed as we consolidate and shift over to ParkEvents
119 // for both builtin synchronization and JSR166 operations.
120
121 volatile int Parker::ListLock = 0 ;
122 Parker * volatile Parker::FreeList = NULL ;
123
Allocate(JavaThread * t)124 Parker * Parker::Allocate (JavaThread * t) {
125 guarantee (t != NULL, "invariant") ;
126 Parker * p ;
127
128 // Start by trying to recycle an existing but unassociated
129 // Parker from the global free list.
130 // 8028280: using concurrent free list without memory management can leak
131 // pretty badly it turns out.
132 Thread::SpinAcquire(&ListLock, "ParkerFreeListAllocate");
133 {
134 p = FreeList;
135 if (p != NULL) {
136 FreeList = p->FreeNext;
137 }
138 }
139 Thread::SpinRelease(&ListLock);
140
141 if (p != NULL) {
142 guarantee (p->AssociatedWith == NULL, "invariant") ;
143 } else {
144 // Do this the hard way -- materialize a new Parker..
145 p = new Parker() ;
146 }
147 p->AssociatedWith = t ; // Associate p with t
148 p->FreeNext = NULL ;
149 return p ;
150 }
151
152
Release(Parker * p)153 void Parker::Release (Parker * p) {
154 if (p == NULL) return ;
155 guarantee (p->AssociatedWith != NULL, "invariant") ;
156 guarantee (p->FreeNext == NULL , "invariant") ;
157 p->AssociatedWith = NULL ;
158
159 Thread::SpinAcquire(&ListLock, "ParkerFreeListRelease");
160 {
161 p->FreeNext = FreeList;
162 FreeList = p;
163 }
164 Thread::SpinRelease(&ListLock);
165 }
166