Chains this Deferred with another one we need to wait on. @param d The other Deferred we need to wait on. @param cb The callback that returned that Deferred or null if we don't know where this Deferred comes from (it was our initial result). @throws AssertionError if this == d as thi
(final Deferred d, final Callback cb)
| 1298 | * infinite recursion. |
| 1299 | */ |
| 1300 | @SuppressWarnings("unchecked") |
| 1301 | private void handleContinuation(final Deferred d, final Callback cb) { |
| 1302 | if (this == d) { |
| 1303 | final String cb2s = cb == null ? "null" : cb + "@" + cb.hashCode(); |
| 1304 | throw new AssertionError("After " + this + " executed callback=" + cb2s |
| 1305 | + ", the result returned was the same Deferred object. This is illegal" |
| 1306 | + ", a Deferred can't run itself recursively. Something is wrong."); |
| 1307 | } |
| 1308 | // Optimization: if `d' is already DONE, instead of calling |
| 1309 | // adding a callback on `d' to continue when `d' completes, |
| 1310 | // we atomically read the result off of `d' immediately. To |
| 1311 | // do this safely, we need to change `d's state momentarily. |
| 1312 | if (d.casState(DONE, RUNNING)) { |
| 1313 | result = d.result; // No one will change `d.result' now. |
| 1314 | d.state = DONE; |
| 1315 | runCallbacks(); |
| 1316 | return; |
| 1317 | } |
| 1318 | |
| 1319 | // Our `state' was either RUNNING or DONE. |
| 1320 | // If it was RUNNING, we have to suspend the callback chain here and |
| 1321 | // resume when the result of that other Deferred is available. |
| 1322 | // If it was DONE, well we're no longer DONE because we now need to wait |
| 1323 | // on that other Deferred to complete, so we're PAUSED again. |
| 1324 | state = PAUSED; |
| 1325 | d.addBoth(new Continue(d, cb)); |
| 1326 | // If d is DONE and our callback chain is empty, we're now in state DONE. |
| 1327 | // Otherwise we're still in state PAUSED. |
| 1328 | if (LOG.isDebugEnabled() && state == PAUSED) { |
| 1329 | if (cb != null) { |
| 1330 | LOG.debug("callback=" + cb + '@' + cb.hashCode() + " returned " + d |
| 1331 | + ", so the following Deferred is getting paused: " + this); |
| 1332 | } else { |
| 1333 | LOG.debug("The following Deferred is getting paused: " + this |
| 1334 | + " as it received another Deferred as a result: " + d); |
| 1335 | } |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | /** |
| 1340 | * A {@link Callback} to resume execution after another Deferred. |