Commit Graph
2889 Commits
Author SHA1 Message Date
Eliza Weisman b1557ea5b2 task: add Builder::{spawn_on, spawn_local_on, spawn_blocking_on} (#4683)
## Motivation

`task::JoinSet` currently has both `spawn`/`spawn_local` methods,
and `spawn_on`/`spawn_local_on` variants of these methods that take a
reference to a runtime `Handle` or to a `LocalSet`, and spawn tasks on
the provided runtime/`LocalSet`, rather than the current one. The
`task::Builder` type is _also_ an API type that can spawn tasks, but it
doesn't have `spawn_on` variants of its methods. It occurred to me that
it would be nice to have similar APIs on `task::Builder`.

## Solution

This branch adds `task::Builder::spawn_on`,
`task::Builder::spawn_local_on`, and `task::Builder::spawn_blocking_on`
methods, similar to those on `JoinSet`. In addition, I did some
refactoring of the internal spawning APIs --- there was a bit of
duplicated code that this PR reduces.

Signed-off-by: Eliza Weisman <[email protected]>
2022-05-14 10:44:47 -07:00
Alice Ryhl ce0e1152ad util: display JoinMap on docs.rs (#4689) 2022-05-14 18:55:26 +02:00
Sabrina Jewson f7346f04af util: simplify ReusableBoxFuture (#4675) 2022-05-13 23:39:13 +02:00
Finomnis addf5b5749 sync: rewrite CancellationToken (#4652) 2022-05-13 23:26:15 +02:00
Noah Kennedy 4ec6ba8b76 metrics: fix compilation with unstable, process, and rt (#4682)
Fixes #4681.
2022-05-11 19:31:37 +00:00
Sabrina Jewson 593b042f7b docs: mention that Clippy must be run with the MSRV (#4676)
## Motivation

In #4675 I learnt the hard way that Tokio uses Clippy on its MSRV. 

## Solution

Document this in the contributor's guide.
2022-05-10 17:53:43 +00:00
Alice Ryhl 71e18f7b75 Merge branch 'merge-1.18.2' into master 2022-05-08 23:37:56 +02:00
Alice Ryhl 7aa1566cde chore: prepare Tokio v1.18.2 tokio-1.18.2 2022-05-08 21:41:15 +02:00
Alice Ryhl 7c8e552f29 windows: add features for winapi (#4663) 2022-05-08 21:41:15 +02:00
Alice Ryhl 67074c3d44 windows: add features for winapi (#4663) 2022-05-08 20:14:28 +02:00
Alice Ryhl 938b7d6742 util: improve impl Send for ReusableBoxFuture docs (#4658) 2022-05-07 22:32:29 +02:00
Bruno 1872a425e2 macros: avoid starvation in join! and try_join! (#4624)
Fixes: #4612
2022-05-07 12:42:44 +02:00
Erick Tryzelaar c5ff797dcf udp: document and shrink some unsafe blocks (#4655)
This documents why it is safe to convert `bytes::UninitSlice` to `&mut
[MaybeUninit<u8>]`, and shrinks one of the unsafe blocks to make these
functions easier to audit.
2022-05-05 19:48:39 +00:00
Taiki Endo 2a305d2423 ci: update actions/checkout action to v3 (#4646) 2022-05-04 15:54:16 +02:00
Uwe Klotz c14566e9df watch: modify and send value conditionally (#4591)
Add a function that is more versatile than send_modify(). The result of
the passed closure indicates if the mutably borrowed value has actually
been modified or not. Receivers are only notified if the value has been
modified as indicated by the sender.

Signed-off-by: Uwe Klotz <[email protected]>
2022-05-03 06:57:10 +00:00
Alice Ryhl 148bea82ee tokio: prepare Tokio v1.18.1 (#4650) tokio-1.18.1 2022-05-02 17:07:27 +02:00
Alice Ryhl dc54aec1c7 metrics: use mocked AtomicU64 in IO metrics driver (#4649) 2022-05-02 13:32:59 +02:00
Taiki Endo fa665b91a8 macros: always emit return statement (#4636)
Fixes #4635
2022-04-28 07:53:40 +09:00
Eliza Weisman 48183430fb tokio: prepare to release v1.18.0 (#4641)
# 1.18.0 (April 27, 2022)

This release adds a number of new APIs in `tokio::net`, `tokio::signal`, and
`tokio::sync`. In addition, it adds new unstable APIs to `tokio::task` (`Id`s
for uniquely identifying a task, and `AbortHandle` for remotely cancelling a
task), as well as a number of bugfixes.

### Fixed

- blocking: add missing `#[track_caller]` for `spawn_blocking` ([#4616])
- macros: fix `select` macro to process 64 branches ([#4519])
- net: fix `try_io` methods not calling Mio's `try_io` internally ([#4582])
- runtime: recover when OS fails to spawn a new thread ([#4485])

### Added

- macros: support setting a custom crate name for `#[tokio::main]` and
  `#[tokio::test]` ([#4613])
- net: add `UdpSocket::peer_addr` ([#4611])
- net: add `try_read_buf` method for named pipes ([#4626])
- signal: add `SignalKind` `Hash`/`Eq` impls and `c_int` conversion ([#4540])
- signal: add support for signals up to `SIGRTMAX` ([#4555])
- sync: add `watch::Sender::send_modify` method ([#4310])
- sync: add `broadcast::Receiver::len` method ([#4542])
- sync: add `watch::Receiver::same_channel` method ([#4581])
- sync: implement `Clone` for `RecvError` types ([#4560])

### Changed

- update `nix` to 0.24, limit features ([#4631])
- update `mio` to 0.8.1 ([#4582])
- macros: rename `tokio::select!`'s internal `util` module ([#4543])
- runtime: use `Vec::with_capacity` when building runtime ([#4553])

### Documented

- improve docs for `tokio_unstable` ([#4524])
- runtime: include more documentation for thread_pool/worker ([#4511])
- runtime: update `Handle::current`'s docs to mention `EnterGuard` ([#4567])
- time: clarify platform specific timer resolution ([#4474])
- signal: document that `Signal::recv` is cancel-safe ([#4634])
- sync: `UnboundedReceiver` close docs ([#4548])

### Unstable

The following changes only apply when building with `--cfg tokio_unstable`:

- task: add `task::Id` type ([#4630])
- task: add `AbortHandle` type for cancelling tasks in a `JoinSet` ([#4530],
  [#4640])
- task: fix missing `doc(cfg(...))` attributes for `JoinSet` ([#4531])
- task: fix broken link in `AbortHandle` RustDoc ([#4545])
- metrics: add initial IO driver metrics ([#4507])

[#4616]: https://github.com/tokio-rs/tokio/pull/4616
[#4519]: https://github.com/tokio-rs/tokio/pull/4519
[#4582]: https://github.com/tokio-rs/tokio/pull/4582
[#4485]: https://github.com/tokio-rs/tokio/pull/4485
[#4613]: https://github.com/tokio-rs/tokio/pull/4613
[#4611]: https://github.com/tokio-rs/tokio/pull/4611
[#4626]: https://github.com/tokio-rs/tokio/pull/4626
[#4540]: https://github.com/tokio-rs/tokio/pull/4540
[#4555]: https://github.com/tokio-rs/tokio/pull/4555
[#4310]: https://github.com/tokio-rs/tokio/pull/4310
[#4542]: https://github.com/tokio-rs/tokio/pull/4542
[#4581]: https://github.com/tokio-rs/tokio/pull/4581
[#4560]: https://github.com/tokio-rs/tokio/pull/4560
[#4631]: https://github.com/tokio-rs/tokio/pull/4631
[#4582]: https://github.com/tokio-rs/tokio/pull/4582
[#4543]: https://github.com/tokio-rs/tokio/pull/4543
[#4553]: https://github.com/tokio-rs/tokio/pull/4553
[#4524]: https://github.com/tokio-rs/tokio/pull/4524
[#4511]: https://github.com/tokio-rs/tokio/pull/4511
[#4567]: https://github.com/tokio-rs/tokio/pull/4567
[#4474]: https://github.com/tokio-rs/tokio/pull/4474
[#4634]: https://github.com/tokio-rs/tokio/pull/4634
[#4548]: https://github.com/tokio-rs/tokio/pull/4548
[#4630]: https://github.com/tokio-rs/tokio/pull/4630
[#4530]: https://github.com/tokio-rs/tokio/pull/4530
[#4640]: https://github.com/tokio-rs/tokio/pull/4640
[#4531]: https://github.com/tokio-rs/tokio/pull/4531
[#4545]: https://github.com/tokio-rs/tokio/pull/4545
[#4507]: https://github.com/tokio-rs/tokio/pull/4507

Signed-off-by: Eliza Weisman <[email protected]>
tokio-1.18.0
2022-04-27 17:08:07 +00:00
Eliza Weisman d456706528 util: implement JoinMap (#4640)
## Motivation

In many cases, it is desirable to spawn a set of tasks associated with
keys, with the ability to cancel them by key. As an example use case for
this sort of thing, see Tower's [`ReadyCache` type][1].

Now that PR #4530 adds a way of cancelling tasks in a
`tokio::task::JoinSet`, we can implement a map-like API based on the
same `IdleNotifiedSet` primitive.

## Solution

This PR adds an implementation of a `JoinMap` type to
`tokio_util::task`, using the `JoinSet` type from `tokio::task`, the
`AbortHandle` type added in #4530, and the new task IDs added in #4630.

Individual tasks can be aborted by key using the `JoinMap::abort`
method, and a set of tasks whose key match a given predicate can be
aborted using `JoinMap::abort_matching`.

When tasks complete, `JoinMap::join_one` returns their associated key
alongside the output from the spawned future, or the key and the
`JoinError` if the task did not complete successfully.

Overall, I think the way this works is pretty straightforward; much of
this PR is just API boilerplate to implement the union of applicable
APIs from `JoinSet` and `HashMap`. Unlike previous iterations on the
`JoinMap` API (e.g. #4538), this version is implemented entirely in
`tokio_util`, using only public APIs from the `tokio` crate. Currently,
the required `tokio` APIs are unstable, but implementing `JoinMap` in
`tokio-util` means we will never have to make stability commitments for
the `JoinMap` API itself.

[1]: https://github.com/tower-rs/tower/blob/master/tower/src/ready_cache/cache.rs

Signed-off-by: Eliza Weisman <[email protected]>
2022-04-26 17:25:48 +00:00
Eliza Weisman 1d3f12304e task: add task IDs (#4630)
## Motivation

PR #4538 adds a prototype implementation of a `JoinMap` API in
`tokio::task`. In [this comment][1] on that PR, @carllerche pointed out
that a much simpler `JoinMap` type could be implemented outside of
`tokio` (either in `tokio-util` or in user code) if we just modified
`JoinSet` to return a task ID type when spawning new tasks, and when
tasks complete. This seems like a better approach for the following
reasons:

* A `JoinMap`-like type need not become a permanent part of `tokio`'s
  stable API
* Task IDs seem like something that could be generally useful outside of
  a `JoinMap` implementation

## Solution

This branch adds a `tokio::task::Id` type that uniquely identifies a
task relative to all other spawned tasks. Task IDs are assigned
sequentially based on an atomic `usize` counter of spawned tasks.

In addition, I modified `JoinSet` to add a `join_with_id` method that
behaves identically to `join_one` but also returns an ID. This can be
used to implement a `JoinMap` type.

Note that because `join_with_id` must return a task ID regardless of
whether the task completes successfully or returns a `JoinError`, I've
also changed `JoinError` to carry the ID of the task that errored, and 
added a `JoinError::id` method for accessing it. Alternatively, we could
have done one of the following:

* have `join_with_id` return `Option<(Id, Result<T, JoinError>)>`, which
  would be inconsistent with the return type of `join_one` (which we've
  [already bikeshedded over once][2]...)
* have `join_with_id` return `Result<Option<(Id, T)>, (Id, JoinError)>>`,
  which just feels gross.

I thought adding the task ID to `JoinError` was the nicest option, and
is potentially useful for other stuff as well, so it's probably a good API to
have anyway.

[1]: https://github.com/tokio-rs/tokio/pull/4538#issuecomment-1065614755
[2]: https://github.com/tokio-rs/tokio/pull/4335#discussion_r773377901

Closes #4538

Signed-off-by: Eliza Weisman <[email protected]>
2022-04-25 17:31:19 +00:00
Aleksey Kladov b4d82c3e70 docs: Signal::recv is cancel-safe (#4634) 2022-04-23 09:12:24 +00:00
cui fliter 1472af5bd4 docs: fix some typos (#4632)
Signed-off-by: cuishuang <[email protected]>
2022-04-21 10:16:19 +00:00
Piepmatz d397e77c90 net: add try_read_buf for named pipes (#4626) 2022-04-21 09:50:12 +02:00
Ryan Zoeller 711d9d0156 chore: upgrade nix to 0.24, limit features (#4631)
This reduces tokio's test compile time by a few seconds.
2022-04-21 04:31:15 +00:00
Carl Lerche 911a0efa87 rt: internally split Handle into two structs (#4629)
Previously, `runtime::Handle` was a single struct composed of the
internal handles for each runtime component. This patch splits the
`Handle` struct into a `HandleInner` which contains everything
**except** the task scheduler handle. Now, `HandleInner` is passed to
the task scheduler during creation and the task scheduler is responsible
for storing it. `Handle` only  needs to hold the scheduler handle and
can access the rest of the component handles by querying the task
scheduler.

The motivation for this change is it now enables the multi-threaded
scheduler to have direct access to the blocking spawner handle.
Previously, when spawning a new thread, the multi-threaded scheduler had
to access the blocking spawner by accessing a thread-local variable.
Now, in theory, the multi-threaded scheduler can use `HandleInner`
directly. However, this change hasn't been done in this PR yet.

Also, now the `Handle` struct is much smaller.

This change is intended to make it easier for the multi-threaded
scheduler to shutdown idle threads and respawn them on demand.
2022-04-20 12:56:55 -07:00
Kezhu Wang d590a369d5 macros: custom crate name for #[tokio::main] and #[tokio::test] (#4613)
This also enables `#[crate::test(crate = "crate")]` in unit tests.

See: rust-lang/cargo#5653
Fixes: #2312
2022-04-18 11:24:27 +02:00
Name1e5s 2fe49a68a4 sync: add panic docs for tokio::sync::broadcast::channel (#4622) 2022-04-17 13:34:51 +02:00
Alan Somers c43832a7b1 ci: update FreeBSD CI image to 12.3 (#4620) 2022-04-15 11:50:40 +02:00
Dom 221bb94b9c blocking: #[track_caller] for spawn_blocking (#4616)
Annotates spawn_blocking() with #[track_caller] in order to produce the
correct tracing span spawn locations that show the caller as the spawn
point.
2022-04-13 15:27:58 +00:00
Bruno 252b0fa9d5 net: add peer_addr to UdpSocket (#4611)
The std UdpSocket has the peer_addr method which can be used to get the address of the remote peer the socket is connected to.

Fixes: #4609
2022-04-10 01:52:45 +00:00
Jedidiah Buck McCready 83477c725a time: clarify platform specific resolution in sleep function docs (#4474) 2022-04-06 15:29:44 +02:00
Stepan Koltsov 3652f71ade sync: add Clone to RecvError types (#4560) 2022-04-06 15:26:45 +02:00
ObsidianMinor b98a7e4d07 runtime: update Handle::current to mention EnterGuard (#4567)
Handle::current docs say it's not possible to call it on any non-runtime thread, but you can call it from a runtime context created by an EnterGuard. This updates the docs to mention EnterGuard as a way to avoid this panic.
2022-04-06 15:23:49 +02:00
Adam Cigánek 7d3b9d73ff stream: expose Timout (#4601) 2022-04-06 15:17:58 +02:00
Paolo Barbolini f8a6cf49cd tracing: don't require default tracing features (#4592) 2022-04-03 11:30:07 +02:00
Arash Sal Moslehian 702d6dccc9 tests: complete TODOs in uds_stream (#4587) 2022-03-28 19:58:06 +00:00
Dirkjan Ochtman a05135a4f8 chore: prepare tokio-util 0.7.1 release (#4521) tokio-util-0.7.1 2022-03-28 14:34:11 +02:00
masa.koz 2bb97db5e1 net: make try_io methods call mio's try_io internally (#4582) 2022-03-28 09:06:47 +00:00
Matthew Ahrens a8b75dbdf4 sync: add watch::Receiver::same_channel (#4581) 2022-03-25 19:16:47 +00:00
b-naber f84c4d596a io: add StreamReader::into_inner_with_chunk (#4559) 2022-03-23 20:41:09 +01:00
Taiki Endo 121769c762 ci: run reusable_box tests with Miri (#4578) 2022-03-21 23:57:47 +09:00
Jedidiah Buck McCready 0abe825b72 time: clarify platform specific timer resolution (#4474) 2022-03-16 15:49:16 +00:00
Eliza Weisman 61e37c6c8d ci: run doctests for unstable APIs (#4562)
It turns out that the CI job for testing `tokio_unstable` features isn't
actually running doctests for `tokio_unstable`, just lib and integration
tests. This is because RustDoc is responsible for running doctests, and
it needs the unstable cfg passed to it separately from `RUSTFLAGS`.

This means that if the examples for unstable APIs are broken, CI won't
catch this, which is not great!

This commit changes the `test-unstable` CI job to pass `--cfg
tokio_unstable` in `RUSTDOCFLAGS` as well as `RUSTFLAGS`. This way,
doctests for unstable APIs should actually run.

I also fixed a typo in one of the runtime metrics doctests that was
causing a compilation error, which was caught as a result of actually
testing the unstable API docs on CI. :)

Signed-off-by: Eliza Weisman <[email protected]>
2022-03-11 20:13:51 +00:00
Eliza Weisman dee26c92dd chore: fix a bunch of annoying clippy lints (#4558)
## Motivation

Recent Clippy releases have added some new lints that trigger on some
code in Tokio. These aren't a big deal, but seeing them in my editor is
mildly annoying.

## Solution

This branch fixes the following issues flagged by Clippy:

* manual `Option::map` implementation
* use of `.map(...).flatten(...)` that could be replaced with
  `.and_then(...)`
* manual implementation of saturating arithmetic on `Duration`s
* simplify some boolean expressions in assertions (`!res.is_ok()` can be
`res.is_err()`)
* fix redundant field names in initializers
* replace an unnecessary cast to `usize` with an explicitly typed
  integer literal

Signed-off-by: Eliza Weisman <[email protected]>
2022-03-08 12:24:19 -08:00
b-naber 2f944dfa1b sync: add broadcast::Receiver::len (#4542) 2022-03-07 13:47:26 +01:00
weisbrja e8ae65a697 tokio: add support for signals up to SIGRTMAX (#4555)
The POSIX standard not only supports "reliable signals", but also
"real-time signals". This commit adds support for the latter.
2022-03-05 06:33:32 +00:00
Rafael Camargo LeiteandRafael 5b947ca2c7 runtime: include more documentation for thread_pool/worker (#4511)
Co-authored-by: Rafael <[email protected]>
2022-03-02 13:55:15 +01:00
Alisue 014be71cca stream: expose Elapsed error (#4502) 2022-03-02 13:53:21 +01:00
William ba49294bae macros: rename tokio::select!'s internal util module (#4543)
This internal module's identifier can clash with existing identifiers in library users' code and produce confusing error messages.
2022-03-02 13:37:37 +01:00