Search in sources :

Example 1 with Addressable

use of jdk.incubator.foreign.Addressable in project openj9 by eclipse.

the class MultiThreadingTests4 method test_multiThreadsWithMixedFuncDescriptors.

@Test
public void test_multiThreadsWithMixedFuncDescriptors() throws Throwable {
    Thread thr1 = new Thread() {

        public void run() {
            try {
                MethodType mt = MethodType.methodType(int.class, int.class, int.class);
                FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT);
                Addressable functionSymbol = nativeLibLookup.lookup("add2Ints").get();
                MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
                int result = (int) mh.invokeExact(128, 246);
                Assert.assertEquals(result, 374);
            } catch (Throwable t) {
                throw new RuntimeException(t);
            }
        }
    };
    Thread thr2 = new Thread() {

        public void run() {
            try {
                MethodType mt = MethodType.methodType(int.class, int.class, int.class, int.class);
                FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT, C_INT);
                Addressable functionSymbol = nativeLibLookup.lookup("add3Ints").get();
                MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
                int result = (int) mh.invokeExact(112, 642, 971);
                Assert.assertEquals(result, 1725);
            } catch (Throwable t) {
                throw new RuntimeException(t);
            }
        }
    };
    Thread thr3 = new Thread() {

        public void run() {
            try {
                MethodType mt = MethodType.methodType(boolean.class, boolean.class, boolean.class);
                FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_CHAR);
                Addressable functionSymbol = nativeLibLookup.lookup("add2BoolsWithOr").get();
                MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
                boolean result = (boolean) mh.invokeExact(true, false);
                Assert.assertEquals(result, true);
            } catch (Throwable t) {
                throw new RuntimeException(t);
            }
        }
    };
    Thread thr4 = new Thread() {

        public void run() {
            try {
                MethodType mt = MethodType.methodType(int.class, int.class, int.class);
                FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT);
                Addressable functionSymbol = nativeLibLookup.lookup("add2Ints").get();
                MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
                int result = (int) mh.invokeExact(416, 728);
                Assert.assertEquals(result, 1144);
            } catch (Throwable t) {
                throw new RuntimeException(t);
            }
        }
    };
    Thread thr5 = new Thread() {

        public void run() {
            try {
                MethodType mt = MethodType.methodType(int.class, int.class, int.class, int.class);
                FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT, C_INT);
                Addressable functionSymbol = nativeLibLookup.lookup("add3Ints").get();
                MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
                int result = (int) mh.invokeExact(1012, 1023, 2035);
                Assert.assertEquals(result, 4070);
            } catch (Throwable t) {
                throw new RuntimeException(t);
            }
        }
    };
    Thread thr6 = new Thread() {

        public void run() {
            try {
                MethodType mt = MethodType.methodType(boolean.class, boolean.class, boolean.class);
                FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_CHAR);
                Addressable functionSymbol = nativeLibLookup.lookup("add2BoolsWithOr").get();
                MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
                boolean result = (boolean) mh.invokeExact(false, false);
                Assert.assertEquals(result, false);
            } catch (Throwable t) {
                throw new RuntimeException(t);
            }
        }
    };
    thr1.setUncaughtExceptionHandler(this);
    thr2.setUncaughtExceptionHandler(this);
    thr3.setUncaughtExceptionHandler(this);
    thr4.setUncaughtExceptionHandler(this);
    thr5.setUncaughtExceptionHandler(this);
    thr6.setUncaughtExceptionHandler(this);
    thr1.start();
    thr2.start();
    thr3.start();
    thr4.start();
    thr5.start();
    thr6.start();
    thr6.join();
    thr5.join();
    thr4.join();
    thr3.join();
    thr2.join();
    thr1.join();
    if (initException != null) {
        throw new RuntimeException(initException);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 2 with Addressable

use of jdk.incubator.foreign.Addressable in project openj9 by eclipse.

the class PrimitiveTypeTests2 method test_addTwoNegtiveInts_2.

@Test
public void test_addTwoNegtiveInts_2() throws Throwable {
    MethodType mt = MethodType.methodType(int.class, int.class, int.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT);
    Addressable functionSymbol = nativeLibLookup.lookup("add2Ints").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, allocator, mt, fd);
    int result = (int) mh.invokeExact(-112, -123);
    Assert.assertEquals(result, -235);
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 3 with Addressable

use of jdk.incubator.foreign.Addressable in project openj9 by eclipse.

the class PrimitiveTypeTests2 method test_printfFromDefaultLibWithMemAddr_fromMemAddr_2.

@Test
public void test_printfFromDefaultLibWithMemAddr_fromMemAddr_2() throws Throwable {
    /* Temporarily disable the default library loading on AIX till we figure out a way
		 * around to handle the case as the official implementation in OpenJDK17 doesn't
		 * help to load the static libray (libc.a).
		 */
    if (!isAixOS) {
        Addressable functionSymbol = defaultLibLookup.lookup("printf").get();
        MemoryAddress memAddr = functionSymbol.address();
        MethodType mt = MethodType.methodType(int.class, MemoryAddress.class, int.class, int.class, int.class);
        FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_POINTER, C_INT, C_INT, C_INT);
        MethodHandle mh = clinker.downcallHandle(memAddr, allocator, mt, fd);
        MemorySegment formatMemSegment = CLinker.toCString("\n%d + %d = %d\n", resourceScope);
        mh.invoke(formatMemSegment.address(), 15, 27, 42);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 4 with Addressable

use of jdk.incubator.foreign.Addressable in project openj9 by eclipse.

the class PrimitiveTypeTests2 method test_addTwoBytes_2.

@Test
public void test_addTwoBytes_2() throws Throwable {
    MethodType mt = MethodType.methodType(byte.class, byte.class, byte.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_CHAR);
    Addressable functionSymbol = nativeLibLookup.lookup("add2Bytes").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, allocator, mt, fd);
    byte result = (byte) mh.invokeExact((byte) 6, (byte) 3);
    Assert.assertEquals(result, (byte) 9);
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 5 with Addressable

use of jdk.incubator.foreign.Addressable in project openj9 by eclipse.

the class PrimitiveTypeTests2 method test_addIntAndChar_2.

@Test
public void test_addIntAndChar_2() throws Throwable {
    MethodType mt = MethodType.methodType(int.class, int.class, char.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_SHORT);
    Addressable functionSymbol = nativeLibLookup.lookup("addIntAndChar").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, allocator, mt, fd);
    int result = (int) mh.invokeExact(58, 'A');
    Assert.assertEquals(result, 123);
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)432 MethodType (java.lang.invoke.MethodType)432 Addressable (jdk.incubator.foreign.Addressable)432 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)432 Test (org.testng.annotations.Test)432 MemorySegment (jdk.incubator.foreign.MemorySegment)314 ResourceScope (jdk.incubator.foreign.ResourceScope)275 GroupLayout (jdk.incubator.foreign.GroupLayout)270 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)270 VarHandle (java.lang.invoke.VarHandle)128 SequenceLayout (jdk.incubator.foreign.SequenceLayout)96 MemoryAddress (jdk.incubator.foreign.MemoryAddress)63