Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*/
package jetbrains.jetpad.base.edt;

import jetbrains.jetpad.base.Async;
import jetbrains.jetpad.base.Registration;

import javax.swing.SwingUtilities;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public final class AwtEventDispatchThread implements EventDispatchThread {
public final class AwtEventDispatchThread extends DefaultAsyncEdt {
public static final AwtEventDispatchThread INSTANCE = new AwtEventDispatchThread();

private AwtEventDispatchThread() {
Expand All @@ -34,8 +35,9 @@ public long getCurrentTimeMillis() {
}

@Override
public void schedule(Runnable r) {
SwingUtilities.invokeLater(r);
protected <ResultT> Async<ResultT> asyncSchedule(RunnableWithAsync<ResultT> runnableWithAsync) {
SwingUtilities.invokeLater(runnableWithAsync);
return runnableWithAsync;
}

@Override
Expand Down Expand Up @@ -75,4 +77,4 @@ protected void doRemove() {
}
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package jetbrains.jetpad.base.edt;

import jetbrains.jetpad.base.Async;
import jetbrains.jetpad.base.function.Supplier;

public abstract class DefaultAsyncEdt implements EventDispatchThread {
@Override
public final Async<Void> schedule(Runnable r) throws EdtException {
return asyncSchedule(RunnableWithAsync.fromRunnable(r));
}

@Override
public final <ResultT> Async<ResultT> schedule(Supplier<ResultT> s) throws EdtException {
return asyncSchedule(RunnableWithAsync.fromSupplier(s));
}

@Override
public <ResultT> Async<ResultT> flatSchedule(Supplier<Async<ResultT>> s) throws EdtException {
return asyncSchedule(RunnableWithAsync.fromAsyncSupplier(s));
}

protected abstract <ResultT> Async<ResultT> asyncSchedule(RunnableWithAsync<ResultT> runnableWithAsync);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package jetbrains.jetpad.base.edt;

import jetbrains.jetpad.base.Async;
import jetbrains.jetpad.base.Registration;
import jetbrains.jetpad.base.function.Supplier;

import java.util.concurrent.CountDownLatch;
import java.util.logging.Logger;
Expand Down Expand Up @@ -156,8 +158,18 @@ public long getCurrentTimeMillis() {
}

@Override
public void schedule(Runnable runnable) {
myManager.getEdt().schedule(runnable);
public Async<Void> schedule(Runnable runnable) {
return myManager.getEdt().schedule(runnable);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I understand kill() contract, it should fail all asyncs that was scheduled in corresponding edt. As far as I see, this implementation fails to follow such contract.

}

@Override
public <ResultT> Async<ResultT> schedule(Supplier<ResultT> s) {
return myManager.getEdt().schedule(s);
}

@Override
public <ResultT> Async<ResultT> flatSchedule(Supplier<Async<ResultT>> s) {
return myManager.getEdt().flatSchedule(s);
}

@Override
Expand All @@ -176,4 +188,4 @@ public String toString() {
+ ("".equals(myName) ? "" : " (" + myName + ")");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
*/
package jetbrains.jetpad.base.edt;

import jetbrains.jetpad.base.Async;
import jetbrains.jetpad.base.Registration;
import jetbrains.jetpad.base.function.Supplier;

public interface EventDispatchThread {

long getCurrentTimeMillis();
void schedule(Runnable r) throws EdtException;
Async<Void> schedule(Runnable r) throws EdtException;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be we need to clarify threading of callbacks and kill/finish semantics?

<ResultT> Async<ResultT> schedule(Supplier<ResultT> s) throws EdtException;
<ResultT> Async<ResultT> flatSchedule(Supplier<Async<ResultT>> s) throws EdtException;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you illustrate how this method will be used?

Registration schedule(int delay, Runnable r) throws EdtException;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be schedule(int delay, Runnable r) should return AsyncWithRegistration?

Registration scheduleRepeating(int period, Runnable r) throws EdtException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@
*/
package jetbrains.jetpad.base.edt;

import jetbrains.jetpad.base.Async;
import jetbrains.jetpad.base.Registration;
import jetbrains.jetpad.base.ThrowableHandlers;
import jetbrains.jetpad.base.function.Supplier;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

public final class ExecutorEdtManager implements EdtManager, EventDispatchThread {
Expand Down Expand Up @@ -58,7 +63,7 @@ public void finish() {
@Override
public void kill() {
ensureCanShutdown();
myEdt.getExecutor().shutdownNow();
myEdt.kill();
waitTermination();
}

Expand Down Expand Up @@ -94,9 +99,27 @@ public long getCurrentTimeMillis() {
}

@Override
public void schedule(Runnable runnable) {
public <ResultT> Async<ResultT> schedule(Supplier<ResultT> s) {
try {
myEdt.schedule(runnable);
return myEdt.schedule(s);
} catch (RejectedExecutionException e) {
throw new EdtException(e);
}
}

@Override
public <ResultT> Async<ResultT> flatSchedule(Supplier<Async<ResultT>> s) {
try {
return myEdt.flatSchedule(s);
} catch (RejectedExecutionException e) {
throw new EdtException(e);
}
}

@Override
public Async<Void> schedule(Runnable runnable) {
try {
return myEdt.schedule(runnable);
} catch (RejectedExecutionException e) {
throw new EdtException(e);
}
Expand Down Expand Up @@ -133,8 +156,10 @@ public String toString() {
return "ExecutorEdtManager@" + Integer.toHexString(hashCode()) + myThreadFactory.getPrintName();
}

private static class ExecutorEdt implements EventDispatchThread {
private static class ExecutorEdt extends DefaultAsyncEdt {
private final ScheduledExecutorService myExecutor;
private final ConcurrentMap<Integer, RunnableWithAsync<?>> myUnresolvedAsyncs = new ConcurrentHashMap<>();
private final AtomicInteger myCounter = new AtomicInteger(0);

ExecutorEdt(ThreadFactory threadFactory) {
myExecutor = Executors.newSingleThreadScheduledExecutor(threadFactory);
Expand All @@ -146,8 +171,17 @@ public long getCurrentTimeMillis() {
}

@Override
public void schedule(Runnable r) {
myExecutor.submit(handleFailure(r));
protected <ResultT> Async<ResultT> asyncSchedule(final RunnableWithAsync<ResultT> task) {
final int i = myCounter.incrementAndGet();
myUnresolvedAsyncs.put(i, task);
myExecutor.submit(new Runnable() {
@Override
public void run() {
task.run();
myUnresolvedAsyncs.remove(i);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can shutdownNow() interrupt this runnable between task.run() and myUnresolvedAsyncs().remove? If so, kill() method can throw exception about failing already completed async

}
});
return task;
}

@Override
Expand Down Expand Up @@ -183,6 +217,13 @@ ExecutorService getExecutor() {
public String toString() {
return "ExecutorEdt@" + Integer.toHexString(hashCode()) + " " + Thread.currentThread().getName();
}

private void kill() {
myExecutor.shutdownNow();
for (RunnableWithAsync<?> async : myUnresolvedAsyncs.values()) {
async.fail();
}
}
}

private static class FutureRegistration extends Registration {
Expand Down Expand Up @@ -236,4 +277,4 @@ Object getEdtId() {
return myEdtId;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
import com.google.gwt.core.client.JavaScriptException;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.user.client.Timer;
import jetbrains.jetpad.base.Async;
import jetbrains.jetpad.base.Registration;
import jetbrains.jetpad.base.ThrowableHandlers;

import java.util.logging.Logger;

public final class JsEventDispatchThread implements EventDispatchThread {
public final class JsEventDispatchThread extends DefaultAsyncEdt {
private static final Logger LOG = Logger.getLogger(JsEventDispatchThread.class.getName());

public static final JsEventDispatchThread INSTANCE = new JsEventDispatchThread();
Expand All @@ -37,13 +38,14 @@ public long getCurrentTimeMillis() {
}

@Override
public void schedule(final Runnable r) {
protected <ResultT> Async<ResultT> asyncSchedule(final RunnableWithAsync<ResultT> runnableWithAsync) {
Scheduler.get().scheduleFinally(new Scheduler.ScheduledCommand() {
@Override
public void execute() {
doExecute(r);
doExecute(runnableWithAsync);
}
});
return runnableWithAsync;
}

@Override
Expand Down Expand Up @@ -97,4 +99,4 @@ protected void doRemove() {
}
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@
*/
package jetbrains.jetpad.base.edt;

import jetbrains.jetpad.base.Async;
import jetbrains.jetpad.base.Asyncs;
import jetbrains.jetpad.base.Registration;

public final class NullEventDispatchThread implements EventDispatchThread {
public final class NullEventDispatchThread extends DefaultAsyncEdt {
@Override
public long getCurrentTimeMillis() {
return 0L;
}

@Override
public void schedule(Runnable r) {
protected <ResultT> Async<ResultT> asyncSchedule(RunnableWithAsync<ResultT> runnableWithAsync) {
return Asyncs.constant(null);
}

@Override
Expand All @@ -36,4 +39,4 @@ public Registration schedule(int delay, Runnable r) {
public Registration scheduleRepeating(int period, Runnable r) {
return Registration.EMPTY;
}
}
}
Loading