Skip to content

Commit

Permalink
Initial push
Browse files Browse the repository at this point in the history
  • Loading branch information
mcimadamore committed Nov 28, 2024
1 parent 25313ba commit 2bcc2e3
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
*/
public abstract sealed class MemorySessionImpl
implements Scope
permits ConfinedSession, GlobalSession, SharedSession {
permits ConfinedSession, GlobalSession, SharedSession, StructuredSession {

/**
* The value of the {@code state} of a {@code MemorySessionImpl}. The only possible transition
Expand Down Expand Up @@ -144,6 +144,10 @@ public static MemorySessionImpl createConfined(Thread thread) {
return new ConfinedSession(thread);
}

public static MemorySessionImpl createStructured(Thread thread) {
return new StructuredSession(thread);
}

public static MemorySessionImpl createShared() {
return new SharedSession();
}
Expand Down Expand Up @@ -174,7 +178,7 @@ public final Thread ownerThread() {
return owner;
}

public final boolean isAccessibleBy(Thread thread) {
public boolean isAccessibleBy(Thread thread) {
Objects.requireNonNull(thread);
return owner == null || owner == thread;
}
Expand All @@ -196,15 +200,20 @@ public boolean isAlive() {
* please use {@link #checkValidState()}.
*/
@ForceInline
public void checkValidStateRaw() {
if (owner != null && owner != Thread.currentThread()) {
throw WRONG_THREAD;
}
public final void checkValidStateRaw() {
checkThreadRaw();
if (state < OPEN) {
throw ALREADY_CLOSED;
}
}

@ForceInline
void checkThreadRaw() {
if (owner != null && owner != Thread.currentThread()) {
throw WRONG_THREAD;
}
}

/**
* Checks that this session is still alive (see {@link #isAlive()}).
* @throws IllegalStateException if this session is already closed or if this is
Expand Down
10 changes: 9 additions & 1 deletion src/java.base/share/classes/jdk/internal/foreign/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,20 @@

package jdk.internal.foreign;

import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.misc.Unsafe;
import jdk.internal.vm.ScopedValueContainer;
import jdk.internal.vm.ScopedValueContainer.BindingsSnapshot;
import jdk.internal.vm.StackableScope;
import jdk.internal.vm.annotation.ForceInline;

import java.lang.ScopedValue.CallableOp;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.StructureViolationException;

public class Stack {

Expand Down Expand Up @@ -139,7 +147,7 @@ MemorySegment nextSlice(long bytesSize, long bytesAlignment, Arena arena) {

class ArenaStack implements Arena {

final Arena arena = Arena.ofConfined();
final Arena arena = MemorySessionImpl.createStructured(Thread.currentThread()).asArena();
final long startOffset;
final long prevPendingStart;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package jdk.internal.foreign;

import jdk.internal.foreign.ConfinedSession.ConfinedResourceList;
import jdk.internal.vm.StackableScope;
import jdk.internal.vm.annotation.ForceInline;

import java.util.Objects;

public final class StructuredSession extends MemorySessionImpl {

private final StackableScope stackableScope = new StackableScope() { };

public StructuredSession(Thread owner) {
super(owner, new ConfinedResourceList());
stackableScope.push();
}

public boolean isAccessibleBy(Thread thread) {
Objects.requireNonNull(thread);
return StackableScope.contains(stackableScope);
}

@Override
@ForceInline
public void acquire0() {
checkValidState();
// do nothing
}

@Override
@ForceInline
public void release0() {
// do nothing
}

@Override
@ForceInline
void checkThreadRaw() {
if (Thread.currentThread() != owner &&
!StackableScope.contains(stackableScope)) {
throw WRONG_THREAD;
}
}

void justClose() {
checkValidState();
if (!stackableScope.tryPop()) {
throw new IllegalStateException("Cannot close");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ private void unlink() {
/**
* Returns true if the given scope is on the current thread's scope stack.
*/
private static boolean contains(StackableScope scope) {
public static boolean contains(StackableScope scope) {
assert scope != null;
StackableScope current = head();
while (current != null && current != scope) {
Expand Down

0 comments on commit 2bcc2e3

Please sign in to comment.