35 absl::Time deadline = absl::InfiniteFuture()) {
36 if (closed_ || state_) {
37 return FailedTask(absl::FailedPreconditionError(
38 "Native task owner already started or closed"));
40 if (deadline != absl::InfiniteFuture() && deadline <= absl::Now()) {
41 return FailedTask(absl::DeadlineExceededError(
"Owner deadline expired"));
43 auto state = std::make_shared<State>();
45 state->completion.SetCancellationCallback(
46 [
weak = std::weak_ptr<State>(state)] {
48 owned->RequestCancel();
54 children.
Add(state->timer);
57 [](
const absl::StatusOr<int>& result) -> absl::StatusOr<Unit> {
58 ABSL_RETURN_IF_ERROR(result.status());
61 state->children = children.Finish();
62 if (deadline != absl::InfiniteFuture()) {
65 state->timer.
OnReady([weak = std::weak_ptr<State>(state)](
66 const absl::StatusOr<Unit>& result) {
68 if (
auto owned = weak.lock()) {
69 owned->property.Cancel();
73 state->property.OnReady([weak = std::weak_ptr<State>(state)](
74 const absl::StatusOr<int>& result) {
76 if (
auto owned = weak.lock()) {
77 owned->timer.Cancel();
81 if (state->alarm.valid()) {
82 state->alarm.OnReady([state](
const absl::StatusOr<Unit>& result) {
83 state->alarm_result = result;
85 state->timed_out =
true;
86 state->timer.Cancel();
87 state->property.Cancel();
88 }
else if (result.status().code() != absl::StatusCode::kCancelled) {
89 state->timer.Cancel();
90 state->property.Cancel();
95 state->children.OnReady([state](
const absl::StatusOr<Unit>& result) {
96 state->child_result = result;
97 if (state->alarm.valid() && !state->alarm.IsReady()) {
98 state->alarm.Cancel();
102 return state->joined;