APIdia Launch Partner
Top Description Inners Fields Constructors Methods
jdk.internal.foreign

public abstract sealed Class MemorySessionImpl

extends Object
implements Scope
permits ConfinedSession, GlobalSession, SharedSession
Class Inheritance
All Implemented Interfaces
java.lang.foreign.MemorySegment.Scope
Known Direct Subclasses
jdk.internal.foreign.SharedSession, jdk.internal.foreign.ConfinedSession, jdk.internal.foreign.GlobalSession
Imports
jdk.internal.foreign.GlobalSession.HeapSession, jdk.internal.invoke.MhUtil, jdk.internal.misc.ScopedMemoryAccess, jdk.internal.vm.annotation.ForceInline, .Stable, java.lang.foreign.Arena, .MemorySegment, .MemorySegment.Scope, java.lang.invoke.MethodHandles, .VarHandle, java.lang.ref.Cleaner, java.util.Objects

This class manages the temporal bounds associated with a memory segment as well as thread confinement. A session has a liveness bit, which is updated when the session is closed (this operation is triggered by MemorySessionImpl#close()). This bit is consulted prior to memory access (see checkValidStateRaw()). There are two kinds of memory session: confined memory session and shared memory session. A confined memory session has an associated owner thread that confines some operations to associated owner thread such as close() or checkValidStateRaw(). Shared sessions do not feature an owner thread - meaning their operations can be called, in a racy manner, by multiple threads. To guarantee temporal safety in the presence of concurrent thread, shared sessions use a more sophisticated synchronization mechanism, which guarantees that no concurrent access is possible when a session is being closed (see jdk.internal.misc.ScopedMemoryAccess).

Nested and Inner Type Summary

Modifier and TypeClass and Description
public abstract static class
MemorySessionImpl.ResourceList

A list of all cleanup actions associated with a memory session.

Field Summary

Modifier and TypeField and Description
pack-priv static final VarHandle
pack-priv int
pack-priv static final ScopedMemoryAccess.ScopedAccessError
pack-priv static final int
public static final MemorySessionImpl
pack-priv static final int
pack-priv static final int
pack-priv static final int
OPEN

The value of the state of a MemorySessionImpl.

pack-priv final Thread
pack-priv final MemorySessionImpl.ResourceList
pack-priv static final VarHandle
pack-priv int
pack-priv static final ScopedMemoryAccess.ScopedAccessError

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
public abstract void
public void
pack-priv void
public void
pack-priv static IllegalStateException
alreadyAcquired(int acquires)

pack-priv static IllegalStateException
public ArenaImpl
public void
checkValidState()

Checks that this session is still alive (see isAlive()).

public static void
public void
checkValidStateRaw()

This is a faster version of checkValidState(), which is called upon memory access, and which relies on invariants associated with the memory session implementations (volatile access to the closed state bit is replaced with plain access).

protected Object
clone()

Overrides java.lang.Object.clone.

Creates and returns a copy of this object.

public void
close()

Closes this session, executing any cleanup action (where provided).

public static MemorySessionImpl
public static MemorySessionImpl
public static MemorySessionImpl
public static MemorySessionImpl
public final boolean
public boolean

Returns:

true if this session is not closed yet.
isAlive
()

Implements java.lang.foreign.MemorySegment.Scope.isAlive.

Returns true, if this session is still open.

public final boolean
public final boolean
pack-priv abstract void
pack-priv static UnsupportedOperationException
public final Thread
public abstract void
public static MemorySessionImpl
pack-priv static IllegalStateException
public void
pack-priv static WrongThreadException
Inherited from java.lang.Object:
equalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ACQUIRE_COUNTback to summary
pack-priv static final VarHandle ACQUIRE_COUNT
acquireCountback to summary
pack-priv int acquireCount
ALREADY_CLOSEDback to summary
pack-priv static final ScopedMemoryAccess.ScopedAccessError ALREADY_CLOSED
CLOSEDback to summary
pack-priv static final int CLOSED
GLOBAL_SESSIONback to summary
public static final MemorySessionImpl GLOBAL_SESSION
MAX_FORKSback to summary
pack-priv static final int MAX_FORKS
NONCLOSEABLEback to summary
pack-priv static final int NONCLOSEABLE
OPENback to summary
pack-priv static final int OPEN

The value of the state of a MemorySessionImpl. The only possible transition is OPEN -> CLOSED. As a result, the states CLOSED and NONCLOSEABLE are stable. This allows us to annotate state with Stable and elide liveness check on non-closeable constant scopes, such as GLOBAL_SESSION.

ownerback to summary
pack-priv final Thread owner
resourceListback to summary
pack-priv final MemorySessionImpl.ResourceList resourceList
STATEback to summary
pack-priv static final VarHandle STATE
stateback to summary
pack-priv int state
Annotations
@Stable
WRONG_THREADback to summary
pack-priv static final ScopedMemoryAccess.ScopedAccessError WRONG_THREAD

Constructor Detail

MemorySessionImplback to summary
protected MemorySessionImpl(Thread owner, MemorySessionImpl.ResourceList resourceList)

Method Detail

acquire0back to summary
public abstract void acquire0()
addCloseActionback to summary
public void addCloseAction(Runnable runnable)
addInternalback to summary
pack-priv void addInternal(MemorySessionImpl.ResourceList.ResourceCleanup resource)
addOrCleanupIfFailback to summary
public void addOrCleanupIfFail(MemorySessionImpl.ResourceList.ResourceCleanup resource)

Add a cleanup action. If a failure occurred (because of an add vs. close race), call the cleanup action. This semantics is useful when allocating new memory segments, since we first do a malloc/mmap and _then_ we register the cleanup (free/munmap) against the session; so, if registration fails, we still have to clean up memory. From the perspective of the client, such a failure would manifest as a factory returning a segment that is already "closed" - which is always possible anyway (e.g. if the session is closed _after_ the cleanup for the segment is registered but _before_ the factory returns the new segment to the client). For this reason, it's not worth adding extra complexity to the segment initialization logic here - and using an optimistic logic works well in practice.

alreadyAcquiredback to summary
pack-priv static IllegalStateException alreadyAcquired(int acquires)
alreadyClosedback to summary
pack-priv static IllegalStateException alreadyClosed()
asArenaback to summary
public ArenaImpl asArena()
checkValidStateback to summary
public void checkValidState()

Checks that this session is still alive (see isAlive()).

Exceptions
IllegalStateException:
if this session is already closed or if this is a confined session and this method is called outside the owner thread.
checkValidStateback to summary
public static void checkValidState(MemorySegment segment)
checkValidStateRawback to summary
public void checkValidStateRaw()

This is a faster version of checkValidState(), which is called upon memory access, and which relies on invariants associated with the memory session implementations (volatile access to the closed state bit is replaced with plain access). This method should be monomorphic, to avoid virtual calls in the memory access hot path. This method is not intended as general purpose method and should only be used in the memory access handle hot path; for liveness checks triggered by other API methods, please use checkValidState().

Annotations
@ForceInline
cloneback to summary
protected Object clone() throws CloneNotSupportedException

Overrides java.lang.Object.clone.

Doc from java.lang.Object.clone.

Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression:

x.clone() != x
will be true, and that the expression:
x.clone().getClass() == x.getClass()
will be true, but these are not absolute requirements. While it is typically the case that:
x.clone().equals(x)
will be true, this is not an absolute requirement.

By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().

By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.

Annotations
@Override
Exceptions
CloneNotSupportedException:
if the object's class does not support the Cloneable interface. Subclasses that override the clone method can also throw this exception to indicate that an instance cannot be cloned.
closeback to summary
public void close()

Closes this session, executing any cleanup action (where provided).

Exceptions
IllegalStateException:
if this session is already closed or if this is a confined session and this method is called outside the owner thread.
createConfinedback to summary
public static MemorySessionImpl createConfined(Thread thread)
createHeapback to summary
public static MemorySessionImpl createHeap(Object ref)
createImplicitback to summary
public static MemorySessionImpl createImplicit(Cleaner cleaner)
createSharedback to summary
public static MemorySessionImpl createShared()
isAccessibleByback to summary
public final boolean isAccessibleBy(Thread thread)
isAliveback to summary
public boolean isAlive()

Implements java.lang.foreign.MemorySegment.Scope.isAlive.

Returns true, if this session is still open. This method may be called in any thread.

Returns:boolean

true if this session is not closed yet.

isCloseableback to summary
public final boolean isCloseable()
isCloseableByback to summary
public final boolean isCloseableBy(Thread thread)
justCloseback to summary
pack-priv abstract void justClose()
nonCloseableback to summary
pack-priv static UnsupportedOperationException nonCloseable()
ownerThreadback to summary
public final Thread ownerThread()
release0back to summary
public abstract void release0()
toMemorySessionback to summary
public static MemorySessionImpl toMemorySession(Arena arena)
Annotations
@ForceInline
tooManyAcquiresback to summary
pack-priv static IllegalStateException tooManyAcquires()
whileAliveback to summary
public void whileAlive(Runnable action)
wrongThreadback to summary
pack-priv static WrongThreadException wrongThread()
jdk.internal.foreign back to summary

public abstract Class MemorySessionImpl.ResourceList

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable
Known Direct Subclasses
jdk.internal.foreign.SharedSession.SharedResourceList, jdk.internal.foreign.ConfinedSession.ConfinedResourceList

A list of all cleanup actions associated with a memory session. Cleanup actions are modelled as instances of the ResourceCleanup class, and, together, form a linked list. Depending on whether a session is shared or confined, different implementations of this class will be used, see ConfinedSession.ConfinedResourceList and SharedSession.SharedResourceList.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public abstract static class

Field Summary

Modifier and TypeField and Description
pack-priv MemorySessionImpl.ResourceList.ResourceCleanup

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
pack-priv abstract void
pack-priv abstract void
pack-priv static void
pack-priv static void
pack-priv static RuntimeException
public final void
run()

Implements java.lang.Runnable.run.

Runs this operation.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

fstback to summary
pack-priv MemorySessionImpl.ResourceList.ResourceCleanup fst

Constructor Detail

ResourceListback to summary
public ResourceList()

Method Detail

addback to summary
pack-priv abstract void add(MemorySessionImpl.ResourceList.ResourceCleanup cleanup)
cleanupback to summary
pack-priv abstract void cleanup()
cleanupback to summary
pack-priv static void cleanup(MemorySessionImpl.ResourceList.ResourceCleanup first)
cleanupback to summary
pack-priv static void cleanup(MemorySessionImpl.ResourceList.ResourceCleanup first, RuntimeException pendingException)
cleanupSingleback to summary
pack-priv static RuntimeException cleanupSingle(MemorySessionImpl.ResourceList.ResourceCleanup resource, RuntimeException pendingException)
runback to summary
public final void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

jdk.internal.foreign back to summary

public abstract Class MemorySessionImpl.ResourceList.ResourceCleanup

extends Object
Class Inheritance
  • java.lang.Object
  • jdk.internal.foreign.MemorySessionImpl.ResourceList.ResourceCleanup

Field Summary

Modifier and TypeField and Description
pack-priv static final MemorySessionImpl.ResourceList.ResourceCleanup
pack-priv MemorySessionImpl.ResourceList.ResourceCleanup

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public abstract void
pack-priv static MemorySessionImpl.ResourceList.ResourceCleanup
ofRunnable(Runnable cleanupAction)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CLOSED_LISTback to summary
pack-priv static final MemorySessionImpl.ResourceList.ResourceCleanup CLOSED_LIST
nextback to summary
pack-priv MemorySessionImpl.ResourceList.ResourceCleanup next

Constructor Detail

ResourceCleanupback to summary
public ResourceCleanup()

Method Detail

cleanupback to summary
public abstract void cleanup()
ofRunnableback to summary
pack-priv static MemorySessionImpl.ResourceList.ResourceCleanup ofRunnable(Runnable cleanupAction)