Skip to content

Commit d5600f5

Browse files
committed
[APGAS] added finish expression construct
1 parent 5ef7060 commit d5600f5

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

apgas/src/apgas/Constructs.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import java.io.Serializable;
1515
import java.util.List;
16+
import java.util.concurrent.Callable;
1617

1718
import apgas.impl.SerializableRunnable;
1819

@@ -31,7 +32,7 @@ private Constructs() {
3132
* Runs {@code f} then waits for all tasks transitively spawned by {@code f}
3233
* to complete.
3334
* <p>
34-
* If {@code f} or the transitively tasks spawned by {@code f} have uncaught
35+
* If {@code f} or the tasks transitively spawned by {@code f} have uncaught
3536
* exceptions then {@code finish(f)} then throws a {@link MultipleException}
3637
* that collects these uncaught exceptions.
3738
*
@@ -44,6 +45,24 @@ public static void finish(Job f) {
4445
GlobalRuntime.getRuntime().finish(f);
4546
}
4647

48+
/**
49+
* Evaluates {@code f}, waits for all the tasks transitively spawned by
50+
* {@code f}, and returns the result.
51+
* <p>
52+
* If {@code f} or the tasks transitively spawned by {@code f} have uncaught
53+
* exceptions then {@code finish(F)} then throws a {@link MultipleException}
54+
* that collects these uncaught exceptions.
55+
*
56+
* @param <T>
57+
* the type of the result
58+
* @param f
59+
* the function to run
60+
* @return the result of the evaluation
61+
*/
62+
public static <T> T finish(Callable<T> f) {
63+
return GlobalRuntime.getRuntime().finish(f);
64+
}
65+
4766
/**
4867
* Submits a new local task to the global runtime with body {@code f} and
4968
* returns immediately.

apgas/src/apgas/GlobalRuntime.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import java.io.Serializable;
1515
import java.util.List;
16+
import java.util.concurrent.Callable;
1617
import java.util.concurrent.ExecutorService;
1718
import java.util.function.Consumer;
1819

@@ -98,7 +99,7 @@ public static GlobalRuntime getRuntime() {
9899
* Runs {@code f} then waits for all tasks transitively spawned by {@code f}
99100
* to complete.
100101
* <p>
101-
* If {@code f} or the transitively tasks spawned by {@code f} have uncaught
102+
* If {@code f} or the tasks transitively spawned by {@code f} have uncaught
102103
* exceptions then {@code finish(F)} then throws a {@link MultipleException}
103104
* that collects these uncaught exceptions.
104105
*
@@ -109,6 +110,22 @@ public static GlobalRuntime getRuntime() {
109110
*/
110111
protected abstract void finish(Job f);
111112

113+
/**
114+
* Evaluates {@code f}, waits for all the tasks transitively spawned by
115+
* {@code f}, and returns the result.
116+
* <p>
117+
* If {@code f} or the tasks transitively spawned by {@code f} have uncaught
118+
* exceptions then {@code finish(F)} then throws a {@link MultipleException}
119+
* that collects these uncaught exceptions.
120+
*
121+
* @param <T>
122+
* the type of the result
123+
* @param f
124+
* the function to run
125+
* @return the result of the evaluation
126+
*/
127+
protected abstract <T> T finish(Callable<T> f);
128+
112129
/**
113130
* Submits a new local task to the global runtime with body {@code f} and
114131
* returns immediately.

apgas/src/apgas/impl/GlobalRuntimeImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.List;
2121
import java.util.SortedSet;
2222
import java.util.TreeSet;
23+
import java.util.concurrent.Callable;
2324
import java.util.concurrent.ExecutorService;
2425
import java.util.concurrent.ForkJoinPool;
2526
import java.util.concurrent.ForkJoinTask;
@@ -34,6 +35,7 @@
3435
import apgas.Place;
3536
import apgas.SerializableCallable;
3637
import apgas.SerializableJob;
38+
import apgas.util.Cell;
3739
import apgas.util.GlobalID;
3840

3941
import com.hazelcast.core.IMap;
@@ -366,6 +368,13 @@ public void finish(Job f) {
366368
}
367369
}
368370

371+
@Override
372+
public <T> T finish(Callable<T> f) {
373+
final Cell<T> cell = new Cell<T>();
374+
finish(() -> cell.set(f.call()));
375+
return cell.get();
376+
}
377+
369378
@Override
370379
public void async(Job f) {
371380
final Worker worker = currentWorker();

0 commit comments

Comments
 (0)