@@ -38,10 +38,11 @@ public class TaskQueue {
3838
3939 // @protectedby tasks
4040 private final LinkedList <Task > tasks = new LinkedList <>();
41- private final Set <ContinuationTask > suspendedTasks = new HashSet <>();
41+ private final Set <ContinuationTask > continuations = new HashSet <>();
4242 private boolean closed ;
4343 private Executor currentExecutor ;
4444 private Thread currentThread ;
45+ private ExecuteTask currentTask ;
4546
4647 private final Runnable runner ;
4748
@@ -65,6 +66,7 @@ private void run() {
6566 ContinuationTask resume = (ContinuationTask ) task ;
6667 currentExecutor = resume .executor ;
6768 currentThread = resume .thread ;
69+ currentTask = resume .task ;
6870 resume .latch .run ();
6971 return ;
7072 }
@@ -78,11 +80,13 @@ private void run() {
7880 }
7981 try {
8082 currentThread = Thread .currentThread ();
83+ currentTask = execute ;
8184 execute .runnable .run ();
8285 } catch (Throwable t ) {
8386 log .error ("Caught unexpected Throwable" , t );
8487 } finally {
8588 currentThread = null ;
89+ currentTask = null ;
8690 }
8791 }
8892 }
@@ -100,6 +104,7 @@ private interface Task {
100104 * @throws IllegalStateException if the current thread is not currently being executed by the queue
101105 */
102106 private ContinuationTask continuationTask () {
107+ ExecuteTask task ;
103108 Thread thread ;
104109 Executor executor ;
105110 synchronized (tasks ) {
@@ -108,8 +113,9 @@ private ContinuationTask continuationTask() {
108113 }
109114 thread = currentThread ;
110115 executor = currentExecutor ;
116+ task = currentTask ;
111117 }
112- return new ContinuationTask (thread , executor );
118+ return new ContinuationTask (task , thread , executor );
113119 }
114120
115121 /**
@@ -152,12 +158,20 @@ public boolean isEmpty() {
152158 public final static class CloseResult {
153159
154160 private final Thread activeThread ;
155- private final List <Runnable > pendingTasks ;
161+ private final Runnable activeTask ;
162+ private final List <Runnable > suspendedTasks ;
156163 private final List <Thread > suspendedThreads ;
164+ private final List <Runnable > pendingTasks ;
157165
158- private CloseResult (Thread activeThread , List <Thread > suspendedThreads , List <Runnable > pendingTasks ) {
166+ private CloseResult (Thread activeThread ,
167+ Runnable activeTask ,
168+ List <Thread > suspendedThreads ,
169+ List <Runnable > suspendedTasks ,
170+ List <Runnable > pendingTasks ) {
159171 this .activeThread = activeThread ;
172+ this .activeTask = activeTask ;
160173 this .suspendedThreads = suspendedThreads ;
174+ this .suspendedTasks = suspendedTasks ;
161175 this .pendingTasks = pendingTasks ;
162176 }
163177
@@ -168,6 +182,10 @@ public Thread activeThread() {
168182 return activeThread ;
169183 }
170184
185+ public Runnable activeTask () {
186+ return activeTask ;
187+ }
188+
171189 /**
172190 * @return the list of pending tasks
173191 */
@@ -181,6 +199,13 @@ public List<Runnable> pendingTasks() {
181199 public List <Thread > suspendedThreads () {
182200 return suspendedThreads ;
183201 }
202+
203+ /**
204+ * @return the list of suspended tasks
205+ */
206+ public List <Runnable > suspendedTasks () {
207+ return suspendedTasks ;
208+ }
184209 }
185210
186211 /**
@@ -191,12 +216,15 @@ public List<Thread> suspendedThreads() {
191216 public CloseResult close () {
192217 List <Runnable > pendingTasks = Collections .emptyList ();
193218 List <Thread > suspendedThreads ;
194- Thread currentThread ;
219+ List <Runnable > suspendedTasks ;
220+ Thread activeThread ;
221+ Runnable activeTask ;
195222 synchronized (tasks ) {
196223 if (closed ) {
197224 throw new IllegalStateException ("Already closed" );
198225 }
199- suspendedThreads = new ArrayList <>(suspendedTasks .size () + 1 );
226+ suspendedThreads = new ArrayList <>(continuations .size ());
227+ suspendedTasks = new ArrayList <>(continuations .size ());
200228 for (Task t : tasks ) {
201229 if (t instanceof ExecuteTask ) {
202230 if (pendingTasks .isEmpty ()) {
@@ -206,31 +234,36 @@ public CloseResult close() {
206234 } else if (t instanceof ContinuationTask ) {
207235 ContinuationTask rt = (ContinuationTask ) t ;
208236 suspendedThreads .add (rt .thread );
237+ suspendedTasks .add (rt .task .runnable );
209238 }
210239 }
211240 tasks .clear ();
212- for (ContinuationTask task : suspendedTasks ) {
213- suspendedThreads .add (task .thread );
241+ for (ContinuationTask cont : continuations ) {
242+ suspendedThreads .add (cont .thread );
243+ suspendedTasks .add (cont .task .runnable );
214244 }
215- suspendedTasks .clear ();
216- currentThread = this .currentThread ;
245+ continuations .clear ();
246+ activeThread = currentThread ;
247+ activeTask = currentTask != null ? currentTask .runnable : null ;
217248 currentExecutor = null ;
218249 closed = true ;
219250 }
220- return new CloseResult (currentThread , suspendedThreads , pendingTasks );
251+ return new CloseResult (activeThread , activeTask , suspendedThreads , suspendedTasks , pendingTasks );
221252 }
222253
223254 private class ContinuationTask extends CountDownLatch implements WorkerExecutor .Continuation , Task {
224255
225256 private static final int ST_CREATED = 0 , ST_SUSPENDED = 1 , ST_RESUMED = 2 ;
226257
258+ private final ExecuteTask task ;
227259 private final Thread thread ;
228260 private final Executor executor ;
229261 private int status ;
230262 private Runnable latch ;
231263
232- public ContinuationTask (Thread thread , Executor executor ) {
264+ public ContinuationTask (ExecuteTask task , Thread thread , Executor executor ) {
233265 super (1 );
266+ this .task = task ;
234267 this .thread = thread ;
235268 this .executor = executor ;
236269 this .status = ST_CREATED ;
@@ -244,7 +277,7 @@ public void resume(Runnable callback) {
244277 }
245278 switch (status ) {
246279 case ST_SUSPENDED :
247- boolean removed = suspendedTasks .remove (this );
280+ boolean removed = continuations .remove (this );
248281 assert removed ;
249282 latch = () -> {
250283 callback .run ();
@@ -256,11 +289,13 @@ public void resume(Runnable callback) {
256289 }
257290 currentExecutor = executor ;
258291 currentThread = thread ;
292+ currentTask = task ;
259293 break ;
260294 case ST_CREATED :
261295 // The current task still owns the queue
262296 assert currentExecutor == executor ;
263297 assert currentThread == thread ;
298+ assert currentTask == task ;
264299 latch = callback ;
265300 break ;
266301 default :
@@ -290,9 +325,10 @@ public boolean suspend() {
290325 throw new IllegalStateException ();
291326 }
292327 status = ST_SUSPENDED ;
293- boolean added = suspendedTasks .add (this );
328+ boolean added = continuations .add (this );
294329 assert added ;
295330 currentThread = null ;
331+ currentTask = null ;
296332 }
297333 executor .execute (runner );
298334 return true ;
0 commit comments