Search in sources :

Example 66 with Addressable

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

the class MultiThreadingTests1 method test_twoThreadsWithSameFuncDescriptor.

@Test
public void test_twoThreadsWithSameFuncDescriptor() 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(112, 123);
                Assert.assertEquals(result, 235);
            } catch (Throwable t) {
                throw new RuntimeException(t);
            }
        }
    };
    thr1.setUncaughtExceptionHandler(this);
    thr1.start();
    Thread thr2 = 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(235, 439);
                Assert.assertEquals(result, 674);
            } catch (Throwable t) {
                throw new RuntimeException(t);
            }
        }
    };
    thr2.setUncaughtExceptionHandler(this);
    thr2.start();
    thr1.join();
    thr2.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 67 with Addressable

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

the class PrimitiveTypeTests1 method test_printfFromDefaultLibWithMemAddr_1.

@Test
public void test_printfFromDefaultLibWithMemAddr_1() 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();
        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(functionSymbol, 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) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 68 with Addressable

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

the class PrimitiveTypeTests1 method test_addTwoFloats_fromMemAddr_1.

@Test
public void test_addTwoFloats_fromMemAddr_1() throws Throwable {
    MethodType mt = MethodType.methodType(float.class, float.class, float.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, C_FLOAT);
    Addressable functionSymbol = nativeLibLookup.lookup("add2Floats").get();
    MemoryAddress memAddr = functionSymbol.address();
    MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd);
    float result = (float) mh.invokeExact(5.74f, 6.79f);
    Assert.assertEquals(result, 12.53f, 0.01f);
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 69 with Addressable

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

the class PrimitiveTypeTests1 method test_addBoolAndBoolFromPointerWithOr_1.

@Test
public void test_addBoolAndBoolFromPointerWithOr_1() throws Throwable {
    MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemoryAddress.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_POINTER);
    Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolFromPointerWithOr").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment boolSegmt = MemorySegment.allocateNative(C_CHAR, resourceScope);
    MemoryAccess.setByte(boolSegmt, (byte) 1);
    boolean result = (boolean) mh.invokeExact(false, boolSegmt.address());
    Assert.assertEquals(result, true);
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 70 with Addressable

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

the class PrimitiveTypeTests1 method test_addTwoNegtiveShorts_1.

@Test
public void test_addTwoNegtiveShorts_1() throws Throwable {
    MethodType mt = MethodType.methodType(short.class, short.class, short.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, C_SHORT);
    Addressable functionSymbol = nativeLibLookup.lookup("add2Shorts").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    short result = (short) mh.invokeExact((short) -24, (short) -32);
    Assert.assertEquals(result, (short) -56);
}
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