mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 07:14:30 +02:00
8291897: TerminatingThreadLocal(s) not registered from virtual thread(s)
Reviewed-by: alanb
This commit is contained in:
parent
8d88be233b
commit
861cc671e2
9 changed files with 250 additions and 66 deletions
|
@ -27,8 +27,7 @@ package sun.nio.ch;
|
|||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import jdk.internal.access.JavaLangAccess;
|
||||
import jdk.internal.access.SharedSecrets;
|
||||
import jdk.internal.misc.CarrierThreadLocal;
|
||||
import jdk.internal.ref.CleanerFactory;
|
||||
|
||||
/**
|
||||
|
@ -46,7 +45,6 @@ import jdk.internal.ref.CleanerFactory;
|
|||
*/
|
||||
|
||||
class IOVecWrapper {
|
||||
private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
|
||||
|
||||
// Miscellaneous constants
|
||||
private static final int BASE_OFFSET = 0;
|
||||
|
@ -83,8 +81,8 @@ class IOVecWrapper {
|
|||
}
|
||||
}
|
||||
|
||||
// per thread IOVecWrapper
|
||||
private static final ThreadLocal<IOVecWrapper> cached = new ThreadLocal<>();
|
||||
// per carrier-thread IOVecWrapper
|
||||
private static final CarrierThreadLocal<IOVecWrapper> cached = new CarrierThreadLocal<>();
|
||||
|
||||
private IOVecWrapper(int size) {
|
||||
this.size = size;
|
||||
|
@ -97,7 +95,7 @@ class IOVecWrapper {
|
|||
}
|
||||
|
||||
static IOVecWrapper get(int size) {
|
||||
IOVecWrapper wrapper = JLA.getCarrierThreadLocal(cached);
|
||||
IOVecWrapper wrapper = cached.get();
|
||||
if (wrapper != null && wrapper.size < size) {
|
||||
// not big enough; eagerly release memory
|
||||
wrapper.vecArray.free();
|
||||
|
@ -106,7 +104,7 @@ class IOVecWrapper {
|
|||
if (wrapper == null) {
|
||||
wrapper = new IOVecWrapper(size);
|
||||
CleanerFactory.cleaner().register(wrapper, new Deallocator(wrapper.vecArray));
|
||||
JLA.setCarrierThreadLocal(cached, wrapper);
|
||||
cached.set(wrapper);
|
||||
}
|
||||
return wrapper;
|
||||
}
|
||||
|
|
|
@ -38,14 +38,11 @@ import java.util.Collection;
|
|||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
import jdk.internal.access.JavaLangAccess;
|
||||
import jdk.internal.access.SharedSecrets;
|
||||
import jdk.internal.misc.TerminatingThreadLocal;
|
||||
import jdk.internal.misc.Unsafe;
|
||||
import sun.security.action.GetPropertyAction;
|
||||
|
||||
public class Util {
|
||||
private static JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
|
||||
|
||||
// -- Caches --
|
||||
|
||||
|
@ -55,8 +52,8 @@ public class Util {
|
|||
// The max size allowed for a cached temp buffer, in bytes
|
||||
private static final long MAX_CACHED_BUFFER_SIZE = getMaxCachedBufferSize();
|
||||
|
||||
// Per-thread cache of temporary direct buffers
|
||||
private static ThreadLocal<BufferCache> bufferCache = new TerminatingThreadLocal<>() {
|
||||
// Per-carrier-thread cache of temporary direct buffers
|
||||
private static TerminatingThreadLocal<BufferCache> bufferCache = new TerminatingThreadLocal<>() {
|
||||
@Override
|
||||
protected BufferCache initialValue() {
|
||||
return new BufferCache();
|
||||
|
@ -230,7 +227,7 @@ public class Util {
|
|||
return ByteBuffer.allocateDirect(size);
|
||||
}
|
||||
|
||||
BufferCache cache = JLA.getCarrierThreadLocal(bufferCache);
|
||||
BufferCache cache = bufferCache.get();
|
||||
ByteBuffer buf = cache.get(size);
|
||||
if (buf != null) {
|
||||
return buf;
|
||||
|
@ -257,7 +254,7 @@ public class Util {
|
|||
.alignedSlice(alignment);
|
||||
}
|
||||
|
||||
BufferCache cache = JLA.getCarrierThreadLocal(bufferCache);
|
||||
BufferCache cache = bufferCache.get();
|
||||
ByteBuffer buf = cache.get(size);
|
||||
if (buf != null) {
|
||||
if (buf.alignmentOffset(0, alignment) == 0) {
|
||||
|
@ -294,7 +291,7 @@ public class Util {
|
|||
}
|
||||
|
||||
assert buf != null;
|
||||
BufferCache cache = JLA.getCarrierThreadLocal(bufferCache);
|
||||
BufferCache cache = bufferCache.get();
|
||||
if (!cache.offerFirst(buf)) {
|
||||
// cache is full
|
||||
free(buf);
|
||||
|
@ -316,7 +313,7 @@ public class Util {
|
|||
}
|
||||
|
||||
assert buf != null;
|
||||
BufferCache cache = JLA.getCarrierThreadLocal(bufferCache);
|
||||
BufferCache cache = bufferCache.get();
|
||||
if (!cache.offerLast(buf)) {
|
||||
// cache is full
|
||||
free(buf);
|
||||
|
|
|
@ -25,8 +25,6 @@
|
|||
|
||||
package sun.nio.fs;
|
||||
|
||||
import jdk.internal.access.JavaLangAccess;
|
||||
import jdk.internal.access.SharedSecrets;
|
||||
import jdk.internal.misc.TerminatingThreadLocal;
|
||||
import jdk.internal.misc.Unsafe;
|
||||
|
||||
|
@ -35,12 +33,12 @@ import jdk.internal.misc.Unsafe;
|
|||
*/
|
||||
|
||||
class NativeBuffers {
|
||||
private static JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
|
||||
|
||||
private static final Unsafe unsafe = Unsafe.getUnsafe();
|
||||
|
||||
private static final int TEMP_BUF_POOL_SIZE = 3;
|
||||
private static ThreadLocal<NativeBuffer[]> threadLocal = new TerminatingThreadLocal<>() {
|
||||
// per-carrier-thread cache of NativeBuffer(s)
|
||||
private static final TerminatingThreadLocal<NativeBuffer[]> threadLocal = new TerminatingThreadLocal<>() {
|
||||
@Override
|
||||
protected void threadTerminated(NativeBuffer[] buffers) {
|
||||
// threadLocal may be initialized but with initialValue of null
|
||||
|
@ -73,7 +71,7 @@ class NativeBuffers {
|
|||
*/
|
||||
static NativeBuffer getNativeBufferFromCache(int size) {
|
||||
// return from cache if possible
|
||||
NativeBuffer[] buffers = JLA.getCarrierThreadLocal(threadLocal);
|
||||
NativeBuffer[] buffers = threadLocal.get();
|
||||
if (buffers != null) {
|
||||
for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
|
||||
NativeBuffer buffer = buffers[i];
|
||||
|
@ -107,11 +105,11 @@ class NativeBuffers {
|
|||
*/
|
||||
static void releaseNativeBuffer(NativeBuffer buffer) {
|
||||
// create cache if it doesn't exist
|
||||
NativeBuffer[] buffers = JLA.getCarrierThreadLocal(threadLocal);
|
||||
NativeBuffer[] buffers = threadLocal.get();
|
||||
if (buffers == null) {
|
||||
buffers = new NativeBuffer[TEMP_BUF_POOL_SIZE];
|
||||
buffers[0] = buffer;
|
||||
JLA.setCarrierThreadLocal(threadLocal, buffers);
|
||||
threadLocal.set(buffers);
|
||||
return;
|
||||
}
|
||||
// Put it in an empty slot if such exists
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue