| 331 | } |
| 332 | |
| 333 | void run(Future<T> next) |
| 334 | { |
| 335 | auto self = shared(); |
| 336 | |
| 337 | // Reset `discard` so that we're not delaying cleanup of any |
| 338 | // captured futures longer than necessary. |
| 339 | // |
| 340 | // TODO(benh): Use `WeakFuture` in `discard` functions instead. |
| 341 | synchronized (mutex) { |
| 342 | discard = []() {}; |
| 343 | } |
| 344 | |
| 345 | while (next.isReady()) { |
| 346 | Future<ControlFlow<R>> flow = body(next.get()); |
| 347 | if (flow.isReady()) { |
| 348 | switch (flow->statement()) { |
| 349 | case ControlFlow<R>::Statement::CONTINUE: { |
| 350 | next = iterate(); |
| 351 | continue; |
| 352 | } |
| 353 | case ControlFlow<R>::Statement::BREAK: { |
| 354 | promise.set(flow->value()); |
| 355 | return; |
| 356 | } |
| 357 | } |
| 358 | } else { |
| 359 | auto continuation = [self](const Future<ControlFlow<R>>& flow) { |
| 360 | if (flow.isReady()) { |
| 361 | switch (flow->statement()) { |
| 362 | case ControlFlow<R>::Statement::CONTINUE: { |
| 363 | self->run(self->iterate()); |
| 364 | break; |
| 365 | } |
| 366 | case ControlFlow<R>::Statement::BREAK: { |
| 367 | self->promise.set(flow->value()); |
| 368 | break; |
| 369 | } |
| 370 | } |
| 371 | } else if (flow.isFailed()) { |
| 372 | self->promise.fail(flow.failure()); |
| 373 | } else if (flow.isDiscarded()) { |
| 374 | self->promise.discard(); |
| 375 | } |
| 376 | }; |
| 377 | |
| 378 | if (pid.isSome()) { |
| 379 | flow.onAny(defer(pid.get(), continuation)); |
| 380 | } else { |
| 381 | flow.onAny(continuation); |
| 382 | } |
| 383 | |
| 384 | if (!promise.future().hasDiscard()) { |
| 385 | synchronized (mutex) { |
| 386 | self->discard = [=]() mutable { flow.discard(); }; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | // There's a race between when a discard occurs and the |
no test coverage detected