--- /dev/null 2019-12-04 18:44:18.020007537 +0000 +++ new/src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/MemoryScope.java 2019-12-09 18:15:40.560192374 +0000 @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + * + */ + +package jdk.internal.foreign; + +import java.util.concurrent.atomic.AtomicInteger; + +/** + * This class manages the temporal bounds associated with a memory segment. A scope has a liveness bit, which is updated + * when the scope is closed (this operation is triggered by {@link MemorySegmentImpl#close()}). Furthermore, a scope is + * associated with an atomic counter which can be incremented (upon calling the {@link #acquire()} method), + * and is decremented (when a previously acquired segment is later closed). + */ +public final class MemoryScope { + + //reference to keep hold onto + final Object ref; + private boolean isAlive = true; + + final AtomicInteger activeCount = new AtomicInteger(); + + final static int UNACQUIRED = 0; + final static int CLOSED = -1; + + final Runnable cleanupAction; + + public MemoryScope(Object ref, Runnable cleanupAction) { + this.ref = ref; + this.cleanupAction = cleanupAction; + } + + final boolean isAlive() { + return isAlive; + } + + final void checkAlive() { + if (!isAlive) { + throw new IllegalStateException("Segment is not alive"); + } + } + + MemoryScope acquire() { + int newValue = activeCount.updateAndGet(i -> (i < 0) ? i : i + 1); + if (newValue == CLOSED) { + //cannot get here - segment is not alive! + throw new IllegalStateException(); + } else if (newValue < 0) { + //overflow + throw new IllegalStateException("Segment acquire limit exceeded"); + } + return new MemoryScope(ref, this::release); + } + + void release() { + if (activeCount.getAndUpdate(i -> i <= UNACQUIRED ? i : i - 1) <= UNACQUIRED) { + //cannot get here - we can't close segment twice + throw new IllegalStateException(); + } + } + + void close() { + if (!activeCount.compareAndSet(UNACQUIRED, CLOSED)) { + throw new IllegalStateException("Cannot close a segment that has active acquired views"); + } + isAlive = false; + if (cleanupAction != null) { + cleanupAction.run(); + } + } +}