Skip to content

Commit

Permalink
Remove the support to pass an Isolate to C entry point, return both t…
Browse files Browse the repository at this point in the history
…he Isolate and the IsolateThread in the API function
  • Loading branch information
Oleg Pliss authored and Oleg Pliss committed Nov 1, 2018
1 parent c952582 commit ff486a6
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 133 deletions.
14 changes: 7 additions & 7 deletions sdk/src/org.graalvm.nativeimage/snapshot.sigtest
Original file line number Diff line number Diff line change
Expand Up @@ -392,13 +392,13 @@ meth public abstract !hasdefault org.graalvm.nativeimage.c.function.CEntryPoint$

CLSS public final static !enum org.graalvm.nativeimage.c.function.CEntryPoint$Builtin
outer org.graalvm.nativeimage.c.function.CEntryPoint
fld public final static org.graalvm.nativeimage.c.function.CEntryPoint$Builtin AttachThread
fld public final static org.graalvm.nativeimage.c.function.CEntryPoint$Builtin CreateIsolate
fld public final static org.graalvm.nativeimage.c.function.CEntryPoint$Builtin CurrentIsolate
fld public final static org.graalvm.nativeimage.c.function.CEntryPoint$Builtin CurrentThread
fld public final static org.graalvm.nativeimage.c.function.CEntryPoint$Builtin DetachThread
fld public final static org.graalvm.nativeimage.c.function.CEntryPoint$Builtin NoBuiltin
fld public final static org.graalvm.nativeimage.c.function.CEntryPoint$Builtin TearDownIsolate
fld public final static org.graalvm.nativeimage.c.function.CEntryPoint$Builtin ATTACH_THREAD
fld public final static org.graalvm.nativeimage.c.function.CEntryPoint$Builtin CREATE_ISOLATE
fld public final static org.graalvm.nativeimage.c.function.CEntryPoint$Builtin GET_ISOLATE
fld public final static org.graalvm.nativeimage.c.function.CEntryPoint$Builtin GET_CURRENT_THREAD
fld public final static org.graalvm.nativeimage.c.function.CEntryPoint$Builtin DETACH_THREAD
fld public final static org.graalvm.nativeimage.c.function.CEntryPoint$Builtin NO_BUILTIN
fld public final static org.graalvm.nativeimage.c.function.CEntryPoint$Builtin TEAR_DOWN_ISOLATE
meth public static org.graalvm.nativeimage.c.function.CEntryPoint$Builtin valueOf(java.lang.String)
meth public static org.graalvm.nativeimage.c.function.CEntryPoint$Builtin[] values()
supr java.lang.Enum<org.graalvm.nativeimage.c.function.CEntryPoint$Builtin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ public static IsolateThread getCurrentThread(Isolate isolate) throws IsolateExce
}

/**
* Given an isolate thread structure for the current thread, determines to which isolate it
* belongs and returns the address of the isolate structure. May return {@code null} if the
* specified isolate thread structure is no longer valid.
* Given an isolate thread structure, determines to which isolate it belongs and returns the
* address of the isolate structure. May return {@code null} if the specified isolate thread
* structure is no longer valid.
*
* @param thread The isolate thread for which to retrieve the isolate.
* @return A pointer to the isolate, or {@code null}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private FatalExceptionHandler() {
*
* @since 1.0
*/
Builtin builtin() default Builtin.NoBuiltin;
Builtin builtin() default Builtin.NO_BUILTIN;

/**
* The built-in methods which can be {@linkplain #builtin() aliased}.
Expand All @@ -146,16 +146,16 @@ enum Builtin {
*
* @since 1.0
*/
NoBuiltin,
NO_BUILTIN,

/**
* The annotated method creates an isolate. An alias for this built-in requires no
* arguments, and must have a return type of {@link Isolate}. In case of an error,
* arguments, and must have a return type of {@link IsolateThread}. In case of an error,
* {@link WordFactory#nullPointer() NULL} is returned.
*
* @since 1.0
*/
CreateIsolate,
CREATE_ISOLATE,

/**
* The annotated method attaches the current thread to an isolate. It requires a parameter
Expand All @@ -165,7 +165,7 @@ enum Builtin {
*
* @since 1.0
*/
AttachThread,
ATTACH_THREAD,

/**
* The annotated method returns the {@link IsolateThread} of the current thread in a
Expand All @@ -175,17 +175,16 @@ enum Builtin {
*
* @since 1.0
*/
CurrentThread,
GET_CURRENT_THREAD,

/**
* The annotated method returns the {@link Isolate} for an {@link IsolateThread} which
* represents the current thread. It requires a parameter of type {@link IsolateThread}, and
* a return type of {@link Isolate}. In case of an error, {@link WordFactory#nullPointer()
* NULL} is returned.
* The annotated method returns the {@link Isolate} for an {@link IsolateThread}. It
* requires a parameter of type {@link IsolateThread}, and a return type of {@link Isolate}.
* In case of an error, {@link WordFactory#nullPointer() NULL} is returned.
*
* @since 1.0
*/
CurrentIsolate,
GET_ISOLATE,

/**
* The annotated method detaches the current thread, given as an {@link IsolateThread}, from
Expand All @@ -195,16 +194,17 @@ enum Builtin {
*
* @since 1.0
*/
DetachThread,
DETACH_THREAD,

/**
* The annotated method tears down the specified isolate. It requires a parameter of type
* {@link Isolate}, and a return type of {@code int} or {@code void}. With an {@code int}
* return type, zero is returned when successful, or non-zero in case of an error.
* {@link IsolateThread}, and a return type of {@code int} or {@code void}. With an
* {@code int} return type, zero is returned when successful, or non-zero in case of an
* error.
*
* @since 1.0
*/
TearDownIsolate,
TEAR_DOWN_ISOLATE,
}

/**
Expand Down
17 changes: 9 additions & 8 deletions substratevm/C-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ typedef struct __graal_create_isolate_params_t graal_create_isolate_params_t;
* Create a new isolate, considering the passed parameters (which may be NULL).
* Returns 0 on success, or a non-zero value on failure.
* On success, the current thread is attached to the created isolate, and the
* address of the isolate structure is written to the passed pointer.
* address of the isolate and the isolate thread structures is written to the
* passed pointers if they are not NULL.
*/
int graal_create_isolate(graal_create_isolate_params_t* params, graal_isolate_t** isolate);
int graal_create_isolate(graal_create_isolate_params_t* params, graal_isolate_t** isolate, graal_isolatethread_t** thread);

/*
* Attaches the current thread to the passed isolate.
Expand All @@ -46,14 +47,14 @@ int graal_attach_thread(graal_isolate_t* isolate, graal_isolatethread_t** thread
* the thread's associated isolate thread structure. If the current thread is not
* attached to the passed isolate or if another error occurs, returns NULL.
*/
graal_isolatethread_t* graal_current_thread(graal_isolate_t* isolate);
graal_isolatethread_t* graal_get_current_thread(graal_isolate_t* isolate);

/*
* Given an isolate thread structure for the current thread, determines to which
* isolate it belongs and returns the address of its isolate structure. If an
* error occurs, returns NULL instead.
* Given an isolate thread structure, determines to which isolate it belongs and
* returns the address of its isolate structure. If an error occurs, returns NULL
* instead.
*/
graal_isolate_t* graal_current_isolate(graal_isolatethread_t* thread);
graal_isolate_t* graal_get_isolate(graal_isolatethread_t* thread);

/*
* Detaches the passed isolate thread from its isolate and discards any state or
Expand All @@ -69,7 +70,7 @@ int graal_detach_thread(graal_isolatethread_t* thread);
* that is associated with it.
* Returns 0 on success, or a non-zero value on failure.
*/
int graal_tear_down_isolate(graal_isolate_t* isolate);
int graal_tear_down_isolate(graal_isolatethread_t* thread);
```
In addition to the C level API, there is also a way to initialize an isolate
from Java and thus use Java and Substrate VM to
Expand Down
28 changes: 14 additions & 14 deletions substratevm/ImplementingNativeMethodsInJavaWithSVM.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import org.graalvm.word.Pointer;

public final class NativeImpl {
@CEntryPoint(name = "Java_org_pkg_apinative_Native_add")
public static int add(Pointer jniEnv, Pointer clazz, @CEntryPoint.IsolateContext long isolateId, int a, int b) {
public static int add(Pointer jniEnv, Pointer clazz, @CEntryPoint.IsolateThreadContext long isolateId, int a, int b) {
return a + b;
}
}
Expand All @@ -34,7 +34,7 @@ After being processed by the `native-image` command the code
a SubstrateVM signature typical for JNI methods. The first parameter
is a reference to `JNIEnv*` value, the second parameter is a reference
to the `jclass` value for the class declaring the method. The third parameter is a
portable (e.g. `long`) identifier of the [SubstrateVM isolate](C-API.md).
portable (e.g. `long`) identifier of the [SubstrateVM isolatethread](C-API.md).
The rest of the parameters are the actual parameters of the Java `Native.add`
method described in the next section. Compile the code with shared option on:
```bash
Expand All @@ -50,13 +50,13 @@ Now we need another Java class to use the native library generated in the previo
package org.pkg.apinative;

public final class Native {
private static native int add(long isolateId, int a, int b);
private static native int add(long isolateThreadId, int a, int b);
}
```
the package name of the class as well as name of the method has to correspond
(after the [JNI mangling](https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/design.html))
to the name of the `@CEntryPoint` introduced previously. The first argument is
a portable (e.g. `long`) identifier of the SubstrateVM isolate. The rest of the arguments
a portable (e.g. `long`) identifier of the SubstrateVM isolate thread. The rest of the arguments
matches the parameters of the entry point.

## Loading the Native Library
Expand Down Expand Up @@ -89,7 +89,7 @@ SubstrateVM then generates default native implementation of the
method into the final `.so` library.
The method initializes the Substrate VM runtime and
returns a portable identification - e.g. `long` to hold
an instance of a [Substrate VM isolate](C-API.md). The isolate can then be used for
an instance of a [Substrate VM isolatethread](C-API.md). The isolate thread can then be used for
multiple invocations of the native part of our code:
```java
package org.pkg.apinative;
Expand All @@ -98,20 +98,20 @@ public final class Native {
public static void main(String[] args) {
System.loadLibrary("nativeimpl");

long isolate = createIsolate();
long isolateThread = createIsolate();

System.out.println("2 + 40 = " + add(isolate, 2, 40));
System.out.println("12 + 30 = " + add(isolate, 12, 30));
System.out.println("20 + 22 = " + add(isolate, 20, 22));
System.out.println("2 + 40 = " + add(isolateThread, 2, 40));
System.out.println("12 + 30 = " + add(isolateThread, 12, 30));
System.out.println("20 + 22 = " + add(isolateThread, 20, 22));
}

private static native int add(long isolate, int a, int b);
private static native int add(long isolateThread, int a, int b);
private static native long createIsolate();
}
```
The standard JVM is started. It initializes a Substrate VM isolate and
the universal answer `42` is then computed three times inside of
the isolate.
The standard JVM is started. It initializes a Substrate VM isolate,
attaches current thread to the isolate and the universal answer `42` is
then computed three times inside of the isolate.

## Calling JVM from Native Java

Expand Down Expand Up @@ -222,7 +222,7 @@ the sum of `a + b`:

```java
@CEntryPoint(name = "Java_org_pkg_apinative_Native_add")
static int add(JNIEnvironment env, JClass clazz, @CEntryPoint.IsolateContext long isolateId, int a, int b) {
static int add(JNIEnvironment env, JClass clazz, @CEntryPoint.IsolateThreadContext long isolateThreadId, int a, int b) {
JNINativeInterface fn = env.getFunctions();

try (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
import com.oracle.svm.core.c.function.CEntryPointOptions.NoPrologue;
import com.oracle.svm.core.c.function.CEntryPointOptions.Publish;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Methods implementing the built-ins of {@link Builtin}, which are matched by name
* (case-insensitive). The methods may only have {@link Isolate} or {@link IsolateThread}
Expand All @@ -45,14 +50,21 @@
public final class CEntryPointBuiltins {
private static final String UNINTERRUPTIBLE_REASON = "Unsafe state in case of failure";

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CEntryPointBuiltinImplementation {
Builtin builtin() default Builtin.NO_BUILTIN;
}

@Uninterruptible(reason = UNINTERRUPTIBLE_REASON)
@CEntryPoint
@CEntryPointOptions(prologue = NoPrologue.class, epilogue = NoEpilogue.class, publishAs = Publish.NotPublished)
public static Isolate createIsolate() {
Isolate result = WordFactory.nullPointer();
@CEntryPointBuiltinImplementation(builtin = Builtin.CREATE_ISOLATE)
public static IsolateThread createIsolate() {
IsolateThread result = WordFactory.nullPointer();
int status = CEntryPointActions.enterCreateIsolate(WordFactory.nullPointer());
if (status == 0) {
result = CurrentIsolate.getIsolate();
result = CurrentIsolate.getCurrentThread();
CEntryPointActions.leave();
}
return result;
Expand All @@ -61,6 +73,7 @@ public static Isolate createIsolate() {
@Uninterruptible(reason = UNINTERRUPTIBLE_REASON)
@CEntryPoint
@CEntryPointOptions(prologue = NoPrologue.class, epilogue = NoEpilogue.class, publishAs = Publish.NotPublished)
@CEntryPointBuiltinImplementation(builtin = Builtin.ATTACH_THREAD)
public static IsolateThread attachThread(Isolate isolate) {
IsolateThread result = WordFactory.nullPointer();
int status = CEntryPointActions.enterAttachThread(isolate);
Expand All @@ -74,7 +87,8 @@ public static IsolateThread attachThread(Isolate isolate) {
@Uninterruptible(reason = UNINTERRUPTIBLE_REASON)
@CEntryPoint
@CEntryPointOptions(prologue = NoPrologue.class, epilogue = NoEpilogue.class, publishAs = Publish.NotPublished)
public static IsolateThread currentThread(Isolate isolate) {
@CEntryPointBuiltinImplementation(builtin = Builtin.GET_CURRENT_THREAD)
public static IsolateThread getCurrentThread(Isolate isolate) {
int status = CEntryPointActions.enterIsolate(isolate);
if (status != 0) {
return WordFactory.nullPointer();
Expand All @@ -89,7 +103,8 @@ public static IsolateThread currentThread(Isolate isolate) {
@Uninterruptible(reason = UNINTERRUPTIBLE_REASON)
@CEntryPoint
@CEntryPointOptions(prologue = NoPrologue.class, epilogue = NoEpilogue.class, publishAs = Publish.NotPublished)
public static Isolate currentIsolate(IsolateThread thread) {
@CEntryPointBuiltinImplementation(builtin = Builtin.GET_ISOLATE)
public static Isolate getIsolate(IsolateThread thread) {
int status = CEntryPointActions.enter(thread);
if (status != 0) {
return WordFactory.nullPointer();
Expand All @@ -104,6 +119,7 @@ public static Isolate currentIsolate(IsolateThread thread) {
@Uninterruptible(reason = UNINTERRUPTIBLE_REASON)
@CEntryPoint
@CEntryPointOptions(prologue = NoPrologue.class, epilogue = NoEpilogue.class, publishAs = Publish.NotPublished)
@CEntryPointBuiltinImplementation(builtin = Builtin.DETACH_THREAD)
public static int detachThread(IsolateThread thread) {
int status = CEntryPointActions.enter(thread);
if (status != 0) {
Expand All @@ -117,8 +133,9 @@ public static int detachThread(IsolateThread thread) {
@Uninterruptible(reason = UNINTERRUPTIBLE_REASON)
@CEntryPoint
@CEntryPointOptions(prologue = NoPrologue.class, epilogue = NoEpilogue.class, publishAs = Publish.NotPublished)
public static int tearDownIsolate(Isolate isolate) {
int result = CEntryPointActions.enterAttachThread(isolate);
@CEntryPointBuiltinImplementation(builtin = Builtin.TEAR_DOWN_ISOLATE)
public static int tearDownIsolate(IsolateThread isolateThread) {
int result = CEntryPointActions.enter(isolateThread);
if (result != 0) {
CEntryPointActions.leave();
return result;
Expand Down
Loading

0 comments on commit ff486a6

Please sign in to comment.