Skip to content

Commit

Permalink
[GR-12153] extend TruffleLanguage.Env.createThread: ThreadGroup, stac…
Browse files Browse the repository at this point in the history
…kSize

PullRequest: graal/2340
  • Loading branch information
cosminbasca committed Oct 25, 2018
2 parents 634dc88 + 1db74df commit 3889578
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 27 deletions.
5 changes: 5 additions & 0 deletions truffle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This changelog summarizes major changes between Truffle versions relevant to languages implementors building upon the Truffle framework. The main focus is on APIs exported by Truffle.

## Version 1.0.0 RC9

* Added support for setting the `ThreadGroup` and `stackSize` on truffle thread creation in `TruffleLanguage.Env.createThread`.

## Version 1.0.0 RC8

* Added `SuspendedEvent.setReturnValue` to change the return value of the currently executed source location.
Expand All @@ -10,6 +14,7 @@ This changelog summarizes major changes between Truffle versions relevant to lan
* Added `TruffleFile.relativize`, `TruffleFile.startsWith`, `TruffleFile.endsWith`, `TruffleFile.createLink`, `TruffleFile.createSymbolicLink`, `TruffleFile.getOwner`, `TruffleFile.getGroup`, `TruffleFile.newDirectoryStream`, `TruffleFile.visit`, `TruffleFile.copy` methods.

## Version 1.0.0 RC7

* Truffle was relicensed from GPLv2 with CPE to Universal Permissive License (UPL).
* Made all Truffle DSL annotations retention policy CLASS instead of RUNTIME. Reflecting DSL annotations at runtime is no longer possible. It is recommended to use `@Introspectable` instead.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1398,33 +1398,47 @@ public boolean isCreateThreadAllowed() {
}

/**
* Creates a new thread that has access to the current language context. A thread is
* {@link TruffleLanguage#initializeThread(Object, Thread) initialized} when it is
* {@link Thread#start() started} and {@link TruffleLanguage#disposeThread(Object, Thread)
* disposed} as soon as the thread finished the execution. In order to start threads the
* language needs to {@link TruffleLanguage#isThreadAccessAllowed(Thread, boolean) allow}
* access from multiple threads at the same time.
* <p>
* It is recommended to set an
* {@link Thread#setUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)
* uncaught exception handler} for the created thread. For example the thread can throw an
* uncaught exception if one of the initialized language contexts don't support execution on
* this thread.
* <p>
* The language that created and started the thread is responsible to complete all running
* or waiting threads when the context is {@link TruffleLanguage#disposeContext(Object)
* disposed}.
* Creates a new thread that has access to the current language context. See
* {@link #createThread(Runnable, TruffleContext, ThreadGroup, long)} for a detailed
* description of the parameters. The <code>group</code> is null and <code>stackSize</code>
* set to 0.
*
* @param runnable the runnable to run on this thread.
* @throws IllegalStateException if thread creation is not {@link #isCreateThreadAllowed()
* allowed}.
* @since 0.28
*/
@TruffleBoundary
public Thread createThread(Runnable runnable) {
return createThread(runnable, null);
}

/**
* Creates a new thread that has access to the given context. See
* {@link #createThread(Runnable, TruffleContext, ThreadGroup, long)} for a detailed
* description of the parameters. The <code>group</code> is null and <code>stackSize</code>
* set to 0.
*
* @see #getContext()
* @see #newContextBuilder()
* @since 0.28
*/
@TruffleBoundary
public Thread createThread(Runnable runnable, @SuppressWarnings("hiding") TruffleContext context) {
return createThread(runnable, context, null, 0);
}

/**
* Creates a new thread that has access to the given context. See
* {@link #createThread(Runnable, TruffleContext, ThreadGroup, long)} for a detailed
* description of the parameters. The <code>stackSize</code> set to 0.
*
* @see #getContext()
* @see #newContextBuilder()
* @since 0.28
*/
@TruffleBoundary
public Thread createThread(Runnable runnable, @SuppressWarnings("hiding") TruffleContext context, ThreadGroup group) {
return createThread(runnable, context, group, 0);
}

/**
* Creates a new thread that has access to the given context. A thread is
* {@link TruffleLanguage#initializeThread(Object, Thread) initialized} when it is
Expand All @@ -1445,17 +1459,20 @@ public Thread createThread(Runnable runnable) {
* {@link #newContextBuilder()}.{@link TruffleContext.Builder#build() build()}, or the
* context associated with this environment obtained from {@link #getContext()}.
*
* @param runnable the runnable to run on this thread
* @param runnable the runnable to run on this thread.
* @param context the context to enter and leave when the thread is started.
* @param group the thread group, passed on to the underlying {@link Thread}.
* @param stackSize the desired stack size for the new thread, or zero if this parameter is
* to be ignored.
* @throws IllegalStateException if thread creation is not {@link #isCreateThreadAllowed()
* allowed}.
* @see #getContext()
* @see #newContextBuilder()
* @since 0.28
*/
@TruffleBoundary
public Thread createThread(Runnable runnable, @SuppressWarnings("hiding") TruffleContext context) {
return AccessAPI.engineAccess().createThread(vmObject, runnable, context != null ? context.impl : null);
public Thread createThread(Runnable runnable, @SuppressWarnings("hiding") TruffleContext context, ThreadGroup group, long stackSize) {
return AccessAPI.engineAccess().createThread(vmObject, runnable, context != null ? context.impl : null, group, stackSize);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,15 @@ public final void detachOutputConsumer(DispatchOutputStream dos, OutputStream ou

public abstract boolean isCreateThreadAllowed(Object vmObject);

public abstract Thread createThread(Object vmObject, Runnable runnable, Object context);
public Thread createThread(Object vmObject, Runnable runnable, Object innerContextImpl, ThreadGroup group) {
return createThread(vmObject, runnable, innerContextImpl, group, 0);
}

public Thread createThread(Object vmObject, Runnable runnable, Object innerContextImpl) {
return createThread(vmObject, runnable, innerContextImpl, null, 0);
}

public abstract Thread createThread(Object vmObject, Runnable runnable, Object innerContextImpl, ThreadGroup group, long stackSize);

public abstract Iterable<Scope> createDefaultLexicalScope(Node node, Frame frame);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ public boolean isCreateThreadAllowed(Object vmObject) {
}

@Override
public Thread createThread(Object vmObject, Runnable runnable, Object innerContextImpl) {
public Thread createThread(Object vmObject, Runnable runnable, Object innerContextImpl, ThreadGroup group, long stackSize) {
if (!isCreateThreadAllowed(vmObject)) {
throw new IllegalStateException("Creating threads is not allowed.");
}
Expand All @@ -792,7 +792,7 @@ public Thread createThread(Object vmObject, Runnable runnable, Object innerConte
PolyglotContextImpl innerContext = (PolyglotContextImpl) innerContextImpl;
threadContext = innerContext.getContext(threadContext.language);
}
return new PolyglotThread(threadContext, runnable);
return new PolyglotThread(threadContext, runnable, group, stackSize);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,20 @@ final class PolyglotThread extends Thread {

Object context;

PolyglotThread(PolyglotLanguageContext languageContext, Runnable runnable) {
super(runnable, createDefaultName(languageContext));
PolyglotThread(PolyglotLanguageContext languageContext, Runnable runnable, ThreadGroup group, long stackSize) {
super(group, runnable, createDefaultName(languageContext), stackSize);
this.languageContext = languageContext;
setUncaughtExceptionHandler(languageContext.getPolyglotExceptionHandler());
}

PolyglotThread(PolyglotLanguageContext languageContext, Runnable runnable, ThreadGroup group) {
this(languageContext, runnable, group, 0);
}

PolyglotThread(PolyglotLanguageContext languageContext, Runnable runnable) {
this(languageContext, runnable, null, 0);
}

private static String createDefaultName(PolyglotLanguageContext creator) {
return "Polyglot-" + creator.language.getId() + "-" + THREAD_INIT_NUMBER.getAndIncrement();
}
Expand Down

0 comments on commit 3889578

Please sign in to comment.