< prev index next >

src/java.base/share/classes/jdk/internal/foreign/MemorySessionImpl.java

Print this page

 60     static final int MAX_FORKS = Integer.MAX_VALUE;
 61 
 62     public static final MemorySessionImpl GLOBAL = new GlobalSession(null);
 63 
 64     static final ScopedMemoryAccess.ScopedAccessError ALREADY_CLOSED = new ScopedMemoryAccess.ScopedAccessError(MemorySessionImpl::alreadyClosed);
 65     static final ScopedMemoryAccess.ScopedAccessError WRONG_THREAD = new ScopedMemoryAccess.ScopedAccessError(MemorySessionImpl::wrongThread);
 66 
 67     final ResourceList resourceList;
 68     final Thread owner;
 69     int state = OPEN;
 70 
 71     static {
 72         try {
 73             STATE = MethodHandles.lookup().findVarHandle(MemorySessionImpl.class, "state", int.class);
 74         } catch (Exception ex) {
 75             throw new ExceptionInInitializerError(ex);
 76         }
 77     }
 78 
 79     public Arena asArena() {
 80         return new Arena() {
 81             @Override
 82             public Scope scope() {
 83                 return MemorySessionImpl.this;
 84             }
 85 
 86             @Override
 87             public void close() {
 88                 MemorySessionImpl.this.close();
 89             }
 90         };
 91     }
 92 
 93     @ForceInline
 94     public static final MemorySessionImpl toMemorySession(Arena arena) {
 95         return (MemorySessionImpl) arena.scope();
 96     }
 97 
 98     public final boolean isCloseableBy(Thread thread) {
 99         Objects.requireNonNull(thread);
100         return isCloseable() &&
101                 (owner == null || owner == thread);
102     }
103 
104     public void addCloseAction(Runnable runnable) {
105         Objects.requireNonNull(runnable);
106         addInternal(ResourceList.ResourceCleanup.ofRunnable(runnable));
107     }
108 
109     /**
110      * Add a cleanup action. If a failure occurred (because of a add vs. close race), call the cleanup action.

136         resourceList.add(resource);
137     }
138 
139     protected MemorySessionImpl(Thread owner, ResourceList resourceList) {
140         this.owner = owner;
141         this.resourceList = resourceList;
142     }
143 
144     public static MemorySessionImpl createConfined(Thread thread) {
145         return new ConfinedSession(thread);
146     }
147 
148     public static MemorySessionImpl createShared() {
149         return new SharedSession();
150     }
151 
152     public static MemorySessionImpl createImplicit(Cleaner cleaner) {
153         return new ImplicitSession(cleaner);
154     }
155 
156     public MemorySegment allocate(long byteSize, long byteAlignment) {
157         Utils.checkAllocationSizeAndAlign(byteSize, byteAlignment);
158         return NativeMemorySegmentImpl.makeNativeSegment(byteSize, byteAlignment, this);
159     }
160 
161     public abstract void release0();
162 
163     public abstract void acquire0();
164 
165     public void whileAlive(Runnable action) {
166         Objects.requireNonNull(action);
167         acquire0();
168         try {
169             action.run();
170         } finally {
171             release0();
172         }
173     }
174 
175     public final Thread ownerThread() {
176         return owner;
177     }
178 
179     public final boolean isAccessibleBy(Thread thread) {
180         Objects.requireNonNull(thread);

 60     static final int MAX_FORKS = Integer.MAX_VALUE;
 61 
 62     public static final MemorySessionImpl GLOBAL = new GlobalSession(null);
 63 
 64     static final ScopedMemoryAccess.ScopedAccessError ALREADY_CLOSED = new ScopedMemoryAccess.ScopedAccessError(MemorySessionImpl::alreadyClosed);
 65     static final ScopedMemoryAccess.ScopedAccessError WRONG_THREAD = new ScopedMemoryAccess.ScopedAccessError(MemorySessionImpl::wrongThread);
 66 
 67     final ResourceList resourceList;
 68     final Thread owner;
 69     int state = OPEN;
 70 
 71     static {
 72         try {
 73             STATE = MethodHandles.lookup().findVarHandle(MemorySessionImpl.class, "state", int.class);
 74         } catch (Exception ex) {
 75             throw new ExceptionInInitializerError(ex);
 76         }
 77     }
 78 
 79     public Arena asArena() {
 80         return new ArenaImpl(this);










 81     }
 82 
 83     @ForceInline
 84     public static final MemorySessionImpl toMemorySession(Arena arena) {
 85         return (MemorySessionImpl) arena.scope();
 86     }
 87 
 88     public final boolean isCloseableBy(Thread thread) {
 89         Objects.requireNonNull(thread);
 90         return isCloseable() &&
 91                 (owner == null || owner == thread);
 92     }
 93 
 94     public void addCloseAction(Runnable runnable) {
 95         Objects.requireNonNull(runnable);
 96         addInternal(ResourceList.ResourceCleanup.ofRunnable(runnable));
 97     }
 98 
 99     /**
100      * Add a cleanup action. If a failure occurred (because of a add vs. close race), call the cleanup action.

126         resourceList.add(resource);
127     }
128 
129     protected MemorySessionImpl(Thread owner, ResourceList resourceList) {
130         this.owner = owner;
131         this.resourceList = resourceList;
132     }
133 
134     public static MemorySessionImpl createConfined(Thread thread) {
135         return new ConfinedSession(thread);
136     }
137 
138     public static MemorySessionImpl createShared() {
139         return new SharedSession();
140     }
141 
142     public static MemorySessionImpl createImplicit(Cleaner cleaner) {
143         return new ImplicitSession(cleaner);
144     }
145 





146     public abstract void release0();
147 
148     public abstract void acquire0();
149 
150     public void whileAlive(Runnable action) {
151         Objects.requireNonNull(action);
152         acquire0();
153         try {
154             action.run();
155         } finally {
156             release0();
157         }
158     }
159 
160     public final Thread ownerThread() {
161         return owner;
162     }
163 
164     public final boolean isAccessibleBy(Thread thread) {
165         Objects.requireNonNull(thread);
< prev index next >