2014-06-09 02:18:46 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2014 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2014-06-17 11:37:58 +02:00
|
|
|
package io.netty.util.concurrent;
|
2014-06-09 02:18:46 +02:00
|
|
|
|
2014-06-17 11:37:58 +02:00
|
|
|
import io.netty.util.internal.InternalThreadLocalMap;
|
2017-11-27 13:53:12 +01:00
|
|
|
import io.netty.util.internal.UnstableApi;
|
2014-06-09 02:18:46 +02:00
|
|
|
|
|
|
|
/**
|
2014-06-17 11:37:58 +02:00
|
|
|
* A special {@link Thread} that provides fast access to {@link FastThreadLocal} variables.
|
2014-06-09 02:18:46 +02:00
|
|
|
*/
|
|
|
|
public class FastThreadLocalThread extends Thread {
|
2017-11-27 13:53:12 +01:00
|
|
|
// This will be set to true if we have a chance to wrap the Runnable.
|
|
|
|
private final boolean cleanupFastThreadLocals;
|
2014-06-09 02:18:46 +02:00
|
|
|
|
2014-06-17 11:37:58 +02:00
|
|
|
private InternalThreadLocalMap threadLocalMap;
|
2014-06-09 02:18:46 +02:00
|
|
|
|
2017-11-27 13:53:12 +01:00
|
|
|
public FastThreadLocalThread() {
|
|
|
|
cleanupFastThreadLocals = false;
|
|
|
|
}
|
2014-06-09 02:18:46 +02:00
|
|
|
|
|
|
|
public FastThreadLocalThread(Runnable target) {
|
2017-11-27 13:53:12 +01:00
|
|
|
super(FastThreadLocalRunnable.wrap(target));
|
|
|
|
cleanupFastThreadLocals = true;
|
2014-06-09 02:18:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public FastThreadLocalThread(ThreadGroup group, Runnable target) {
|
2017-11-27 13:53:12 +01:00
|
|
|
super(group, FastThreadLocalRunnable.wrap(target));
|
|
|
|
cleanupFastThreadLocals = true;
|
2014-06-09 02:18:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public FastThreadLocalThread(String name) {
|
|
|
|
super(name);
|
2017-11-27 13:53:12 +01:00
|
|
|
cleanupFastThreadLocals = false;
|
2014-06-09 02:18:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public FastThreadLocalThread(ThreadGroup group, String name) {
|
|
|
|
super(group, name);
|
2017-11-27 13:53:12 +01:00
|
|
|
cleanupFastThreadLocals = false;
|
2014-06-09 02:18:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public FastThreadLocalThread(Runnable target, String name) {
|
2017-11-27 13:53:12 +01:00
|
|
|
super(FastThreadLocalRunnable.wrap(target), name);
|
|
|
|
cleanupFastThreadLocals = true;
|
2014-06-09 02:18:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public FastThreadLocalThread(ThreadGroup group, Runnable target, String name) {
|
2017-11-27 13:53:12 +01:00
|
|
|
super(group, FastThreadLocalRunnable.wrap(target), name);
|
|
|
|
cleanupFastThreadLocals = true;
|
2014-06-09 02:18:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public FastThreadLocalThread(ThreadGroup group, Runnable target, String name, long stackSize) {
|
2017-11-27 13:53:12 +01:00
|
|
|
super(group, FastThreadLocalRunnable.wrap(target), name, stackSize);
|
|
|
|
cleanupFastThreadLocals = true;
|
2014-06-09 02:18:46 +02:00
|
|
|
}
|
|
|
|
|
2014-06-17 11:37:58 +02:00
|
|
|
/**
|
|
|
|
* Returns the internal data structure that keeps the thread-local variables bound to this thread.
|
|
|
|
* Note that this method is for internal use only, and thus is subject to change at any time.
|
|
|
|
*/
|
|
|
|
public final InternalThreadLocalMap threadLocalMap() {
|
|
|
|
return threadLocalMap;
|
2014-06-09 02:18:46 +02:00
|
|
|
}
|
|
|
|
|
2014-06-17 11:37:58 +02:00
|
|
|
/**
|
|
|
|
* Sets the internal data structure that keeps the thread-local variables bound to this thread.
|
|
|
|
* Note that this method is for internal use only, and thus is subject to change at any time.
|
|
|
|
*/
|
|
|
|
public final void setThreadLocalMap(InternalThreadLocalMap threadLocalMap) {
|
|
|
|
this.threadLocalMap = threadLocalMap;
|
2014-06-09 02:18:46 +02:00
|
|
|
}
|
2017-11-27 13:53:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns {@code true} if {@link FastThreadLocal#removeAll()} will be called once {@link #run()} completes.
|
|
|
|
*/
|
|
|
|
@UnstableApi
|
|
|
|
public boolean willCleanupFastThreadLocals() {
|
|
|
|
return cleanupFastThreadLocals;
|
|
|
|
}
|
2017-12-01 16:37:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns {@code true} if {@link FastThreadLocal#removeAll()} will be called once {@link Thread#run()} completes.
|
|
|
|
*/
|
|
|
|
@UnstableApi
|
|
|
|
public static boolean willCleanupFastThreadLocals(Thread thread) {
|
|
|
|
return thread instanceof FastThreadLocalThread &&
|
|
|
|
((FastThreadLocalThread) thread).willCleanupFastThreadLocals();
|
|
|
|
}
|
2014-06-09 02:18:46 +02:00
|
|
|
}
|