Skip to content

Park trait, ParkThread implementation and using it in LocalPool#1668

Open
stbuehler wants to merge 3 commits into
rust-lang:mainfrom
stbuehler:park
Open

Park trait, ParkThread implementation and using it in LocalPool#1668
stbuehler wants to merge 3 commits into
rust-lang:mainfrom
stbuehler:park

Conversation

@stbuehler

Copy link
Copy Markdown
Contributor

Hi,

I think it would be good to have a Park abstration (like the one in tokio) in futures-executor (this has already been proposed and merged in #665 for the tokio-reform branch, which has been abonded in #645).

In #665 Park was renamed to Sleep, but I think Park is the better choice:

  • std lib provides thread::park for a sleeping mechanism that can be cancelled ("unparked").
  • tokio users already know it by that name
  • it is not just a "sleep" mechanism due to the "wakeup" part

Apart from the names Sleep from #665 is basically the same as tokio Park; further changes to the API (compared to tokio):

  • merge Park::park_timeout into Park::park, now taking a ParkDuration enum: most implementations I've seen used shared code anyway.
  • Park::park now also includes a &mut Enter argument
  • Removed Unpark trait, Park no longer has an associated Unpark type. Using futures_util::task::WakerRef instead for the result of the waker function (renamed from unpark).

I think reusing the Waker abstraction is rather obvious; but there are probably two open questions:

  • Use Waker instead of WakerRef?
  • Remove ParkDuration::Poll and interpret a zero duration in ParkDuration::Duration(..) as "poll only but don't yield"?

This pull request also contains a ParkThread implementation of Park (based on std::thread::park and similar to what LocalPool was using) and supports using LocalPool with custom Park implentations (defaulting to ParkThread). This way LocalPool can be combined into a "local-thread runtime" with a timer and a reactor.

References:

@stbuehler

Copy link
Copy Markdown
Contributor Author

Just rebased to current master. Is there anything I can do to help moving this (and maybe other pull requests) forward?

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@stbuehler
stbuehler force-pushed the park branch 2 times, most recently from c24322f to dfeae9c Compare April 10, 2026 12:13
@stbuehler

Copy link
Copy Markdown
Contributor Author

So, I basically rewrote this PR - I still (7 years later :) ) think this is would be an interesting and useful addition to the LocalPool API.

I also added a separate ParkPoll trait to handle the non-blocking additions from #1437 / #1537 - I don't really like try_run_one and run_until_stalled, as you basically need to busy-loop poll them (and ParkPoll obviously has the same problem). (Perhaps whatever they were used for could actually be fixed by some Park implementation.)

Generic default params are not working well, so I renamed the actual type to LocalPoolGen and aliased it with the default ParkThread to LocalPool.

@rustbot

This comment has been minimized.

@rustbot

rustbot commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

- make LocalPool type alias with default ParkThread implementation
  matching previous behavior (default generic type doesn't work;
  would break in code like `LocalPool::new()`)
- split non-park state into non-generic inner state
- implement Park for LocalPool too
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-executor Area: futures::executor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants