< prev index next > src/java.base/share/classes/jdk/internal/misc/ThreadFlock.java
Print this page
import java.time.Duration;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeoutException;
+ import java.util.concurrent.StructureViolationException;
import java.util.concurrent.locks.LockSupport;
import java.util.stream.Stream;
import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.vm.ScopedValueContainer;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
THREAD_COUNT = l.findVarHandle(ThreadFlock.class, "threadCount", int.class);
PERMIT = l.findVarHandle(ThreadFlock.class, "permit", boolean.class);
- Unsafe.getUnsafe().ensureClassInitialized(StructureViolationExceptions.class);
} catch (Exception e) {
throw new InternalError(e);
}
}
* @return the thread, started
* @throws IllegalStateException if this flock is shutdown or closed
* @throws IllegalThreadStateException if the given thread was already started
* @throws WrongThreadException if the current thread is not the owner or a thread
* contained in the flock
! * @throws jdk.incubator.concurrent.StructureViolationException if the current
! * scoped value bindings are not the same as when the flock was created
*/
public Thread start(Thread thread) {
ensureOwnerOrContainsThread();
JLA.start(thread, container);
return thread;
* @return the thread, started
* @throws IllegalStateException if this flock is shutdown or closed
* @throws IllegalThreadStateException if the given thread was already started
* @throws WrongThreadException if the current thread is not the owner or a thread
* contained in the flock
! * @throws StructureViolationException if the current scoped value bindings are
! * not the same as when the flock was created
*/
public Thread start(Thread thread) {
ensureOwnerOrContainsThread();
JLA.start(thread, container);
return thread;
* scoped value bindings, and the thread flock was created before the scoped values
* were bound, then {@code StructureViolationException} is thrown after closing the
* thread flock.
*
* @throws WrongThreadException if invoked by a thread that is not the owner
! * @throws jdk.incubator.concurrent.StructureViolationException if a structure
- * violation was detected
*/
public void close() {
ensureOwner();
if (closed)
return;
* scoped value bindings, and the thread flock was created before the scoped values
* were bound, then {@code StructureViolationException} is thrown after closing the
* thread flock.
*
* @throws WrongThreadException if invoked by a thread that is not the owner
! * @throws StructureViolationException if a structure violation was detected
*/
public void close() {
ensureOwner();
if (closed)
return;
this.flock = flock;
}
@Override
public ThreadContainerImpl push() {
! // Virtual threads in the root containers are not tracked so need
// to register container to ensure that it is found
! Thread thread = Thread.currentThread();
! if (thread.isVirtual()
! && JLA.threadContainer(thread) == ThreadContainers.root()) {
! this.key = ThreadContainers.registerContainer(this);
}
-
super.push();
return this;
}
/**
this.flock = flock;
}
@Override
public ThreadContainerImpl push() {
! // Virtual threads in the root containers may not be tracked so need
// to register container to ensure that it is found
! if (!ThreadContainers.trackAllThreads()) {
! Thread thread = Thread.currentThread();
! if (thread.isVirtual()
! && JLA.threadContainer(thread) == ThreadContainers.root()) {
+ this.key = ThreadContainers.registerContainer(this);
+ }
}
super.push();
return this;
}
/**
boolean atTop = popForcefully(); // may block
Object key = this.key;
if (key != null)
ThreadContainers.deregisterContainer(key);
if (!atTop)
! StructureViolationExceptions.throwException();
}
}
/**
* Invoked when an enclosing scope is closing. Invokes ThreadFlock.close to
boolean atTop = popForcefully(); // may block
Object key = this.key;
if (key != null)
ThreadContainers.deregisterContainer(key);
if (!atTop)
! throw new StructureViolationException();
}
}
/**
* Invoked when an enclosing scope is closing. Invokes ThreadFlock.close to
assert false : "Should not get there";
return false;
}
}
+ @Override
+ public String name() {
+ return flock.name();
+ }
@Override
public long threadCount() {
return flock.threadCount();
}
@Override
@Override
public void onExit(Thread thread) {
flock.onExit(thread);
}
@Override
- public String toString() {
- return flock.toString();
- }
- @Override
public ScopedValueContainer.BindingsSnapshot scopedValueBindings() {
return flock.scopedValueBindings();
}
}
}
< prev index next >