< prev index next >

test/jdk/java/foreign/TestSharedAccess.java

Print this page

  7  *  published by the Free Software Foundation.
  8  *
  9  *  This code is distributed in the hope that it will be useful, but WITHOUT
 10  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  *  version 2 for more details (a copy is included in the LICENSE file that
 13  *  accompanied this code).
 14  *
 15  *  You should have received a copy of the GNU General Public License version
 16  *  2 along with this work; if not, write to the Free Software Foundation,
 17  *  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  *   Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  *  or visit www.oracle.com if you need additional information or have any
 21  *  questions.
 22  *
 23  */
 24 
 25 /*
 26  * @test
 27  * @enablePreview
 28  * @run testng/othervm --enable-native-access=ALL-UNNAMED TestSharedAccess
 29  */
 30 
 31 import java.lang.foreign.*;
 32 import java.lang.invoke.VarHandle;
 33 import java.nio.ByteBuffer;
 34 import java.util.ArrayList;
 35 import java.util.List;
 36 import java.util.Spliterator;
 37 import java.util.concurrent.CompletableFuture;
 38 import java.util.concurrent.CountDownLatch;
 39 import java.util.concurrent.atomic.AtomicInteger;
 40 
 41 import org.testng.annotations.*;
 42 
 43 import static org.testng.Assert.*;
 44 
 45 public class TestSharedAccess {
 46 
 47     static final VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();

134                         b.await();
135                     } catch (InterruptedException e) {
136                     }
137 
138                     setInt(s2.asSlice(4), -42);
139                     fail();
140                 } catch (WrongThreadException ex) {
141                     assertTrue(ex.getMessage().contains("owning thread"));
142                 }
143             });
144 
145             a.await();
146             setInt(s1.asSlice(4), 42);
147         }
148 
149         b.countDown();
150         r.get();
151     }
152 
153     static int getInt(MemorySegment base) {
154         return (int)intHandle.getVolatile(base);
155     }
156 
157     static void setInt(MemorySegment base, int value) {
158         intHandle.setVolatile(base, value);
159     }
160 }

  7  *  published by the Free Software Foundation.
  8  *
  9  *  This code is distributed in the hope that it will be useful, but WITHOUT
 10  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  *  version 2 for more details (a copy is included in the LICENSE file that
 13  *  accompanied this code).
 14  *
 15  *  You should have received a copy of the GNU General Public License version
 16  *  2 along with this work; if not, write to the Free Software Foundation,
 17  *  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  *   Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  *  or visit www.oracle.com if you need additional information or have any
 21  *  questions.
 22  *
 23  */
 24 
 25 /*
 26  * @test

 27  * @run testng/othervm --enable-native-access=ALL-UNNAMED TestSharedAccess
 28  */
 29 
 30 import java.lang.foreign.*;
 31 import java.lang.invoke.VarHandle;
 32 import java.nio.ByteBuffer;
 33 import java.util.ArrayList;
 34 import java.util.List;
 35 import java.util.Spliterator;
 36 import java.util.concurrent.CompletableFuture;
 37 import java.util.concurrent.CountDownLatch;
 38 import java.util.concurrent.atomic.AtomicInteger;
 39 
 40 import org.testng.annotations.*;
 41 
 42 import static org.testng.Assert.*;
 43 
 44 public class TestSharedAccess {
 45 
 46     static final VarHandle intHandle = ValueLayout.JAVA_INT.varHandle();

133                         b.await();
134                     } catch (InterruptedException e) {
135                     }
136 
137                     setInt(s2.asSlice(4), -42);
138                     fail();
139                 } catch (WrongThreadException ex) {
140                     assertTrue(ex.getMessage().contains("owning thread"));
141                 }
142             });
143 
144             a.await();
145             setInt(s1.asSlice(4), 42);
146         }
147 
148         b.countDown();
149         r.get();
150     }
151 
152     static int getInt(MemorySegment base) {
153         return (int)intHandle.getVolatile(base, 0L);
154     }
155 
156     static void setInt(MemorySegment base, int value) {
157         intHandle.setVolatile(base, 0L, value);
158     }
159 }
< prev index next >