3097 Commits

Author SHA1 Message Date
Lukas Wirth
37d1d09d4a
macros: reduce usage of last statement spans in proc-macros (#5092)
This excludes the initial let statement of the proc-macro expansion
from receiving the last statement spans to aid completions in rust-analyzer.
The current span confuses rust-analyzer as it will map the tail expression
tokens to the let keyword (as this is the first token it finds with the
same span) which currently breaks completions. This commit should not
degrade the initial intent of the span reusages, as the type mismatch
parts are still spanned appropriately.
2022-10-14 09:50:09 +02:00
Alice Ryhl
6929decf2c
macros: don't take ownership of futures in macros (#5087) 2022-10-14 09:45:31 +02:00
Carl Lerche
f8097437dd
rt: remove a conditional compilation clause (#5104)
The `LocalSet` implementation includes a conditional compilation clause
that removes the `const` statement from the `thread_local` definition.
However, there already is an internal macro that does this:
`tokio_thread_local`.

This patch removes the conditional compilation in favor of using the
`tokio_thread_local` macro. This also fixes a conditional compilation
issue with an internal utility (`RcCell`).
2022-10-13 16:45:15 -07:00
Carl Lerche
964535eab0
tokio: rename internal thread_local macro (#5103)
Tokio maintains an internal thread_local macro that abstracts some
conditional build logic. Before this patch, the macro was named the same
as the `std` macro (`thread_local`). This resulted in confusion as to
whether or not the internal macro or the std macro was being called.

This patch renames the internal macro to `tokio_thread_local` making it
more obvious.
2022-10-13 14:29:12 -07:00
Eliza Weisman
23a1ccf24f
task: wake local tasks to the local queue when woken by the same thread (#5095)
Motivation

Currently, when a task spawned on a `LocalSet` is woken by an I/O driver
or time driver running on the same thread as the `LocalSet`, the task is
pushed to the `LocalSet`'s locked remote run queue rather than to its
unsynchronized local run queue. This is unfortunate, as it
negates some of the performance benefits of having an unsynchronized
local run queue. Instead, tasks are only woken to the local queue when
they are woken by other tasks also running on the local set.

This occurs because the local queue is only used when the `CONTEXT`
thread-local contains a Context that's the same as the task's
`Schedule` instance (an `Arc<Shared>`)'s Context. When the `LocalSet`
is not being polled, the thread-local is unset, and the local run queue
cannot be accessed by the `Schedule` implementation for `Arc<Shared>`.

Solution

This branch fixes this by moving the local run queue into Shared along
with the remote run queue. When an `Arc<Shared>`'s Schedule impl wakes
a task and the `CONTEXT` thread-local is None (indicating we are not
currently polling the LocalSet on this thread), we now check if the
current thread's `ThreadId` matches that of the thread the `LocalSet`
was created on, and push the woken task to the local queue if it was.

Moving the local run queue into `Shared` is somewhat unfortunate, as it
means we now have a single field on the `Shared` type, which must not be
accessed from other threads and must add an unsafe impl `Sync` for `Shared`.
However, it's the only viable way to wake to the local queue
from the Schedule impl for `Arc<Shared>`, so I figured it was worth
the additional unsafe code. I added a debug assertion to check that the
local queue is only accessed from the thread that owns the `LocalSet`.
2022-10-13 11:27:19 -07:00
Eliza Weisman
ca9dd726b1
runtime: fix Stacked Borrows violation in LocalOwnedTasks (#5099)
## Motivation

It turns out that `LocalSet` has...never actually passed Miri. This is
because all of the tests for `LocalSet` are defined as integration tests
in `tokio/tests/task_local_set.rs`, rather than lib tests in
`tokio/src`, and we never run Miri for integration tests.

PR #5095 added a new test reproducing an unrelated bug in `LocalSet`,
which had to be implemented as a lib test, as it must make internal
assertions about the state of the `LocalSet` that cannot be tested with
just the public API. This test failed under Miri, and it turns out that
the reason for this is unrelated to the change in #5095 --- `LocalSet`
uses the `LocalOwnedTasks` type, which turns out to contain a stacked
borrows violation due to converting an `&Header` into an
`NonNull<Header>` when removing a task from the `LocalOwnedTasks` list.

## Solution

Fortunately, this was actually quite easy to fix. The non-`Local`
version of `OwnedTasks` already uses `Task::header_ptr()` to get a
`NonNull<Header>` in its version of `remove`, which avoids this issue.
Therefore, the fix was as simple as updating `LocalOwnedTasks` to do the
same.

I've also added a very simple lib test in `src/task/local_set.rs` that
just creates a `LocalSet` and spawns a single task on it under a
current-thread runtime. This test fails under Miri without the fix in
this PR: https://github.com/tokio-rs/tokio/actions/runs/3237072694/jobs/5303682530

We should probably keep the test so that we're always testing at least
the most trivial `LocalSet` usage under Miri...
2022-10-12 19:18:34 +00:00
Makro
ae0d49d59c
chore: release tokio-stream v0.1.11 (#5094) tokio-stream-0.1.11 2022-10-11 16:48:24 +02:00
cjw
992a168122
runtime: remove Option around mio::Events in io driver (#5078) 2022-10-07 16:47:33 +02:00
Hayden Stainsby
9b486357ed
rt: improve rng_seed test robustness (#5075)
The original tests for the `Builder::rng_seed` added in #4910 were a bit
fragile. There have already been a couple of instances where internal
refactoring caused the tests to fail and need to be modified. While it
is expected that internal refactoring may cause the random values to
change, this shouldn't cause the tests to break.

The tests should be more robust and not be affected by internal
refactoring or changes in the Rust compiler version. The tests are
changed to perform the same operation in 2 runtimes created with the
same seed, the expectation is that the values that result from each
runtime are the same.
2022-10-05 14:31:08 +02:00
Alice Ryhl
fdc28bc883
coop: fix flaky coop test (#5074) 2022-10-05 12:55:02 +02:00
xudong.w
5dbd8ca8d4
chore: use std::AtomicPtr and std::AtomicU8 (#5071) 2022-10-04 10:06:24 +00:00
Anders Kiel Hovgaard
ff4ab49bcf
net: fix doc typos for TCP and UDP set_tos methods (#5073) 2022-10-03 13:28:50 -05:00
Simon Farnsworth
6bdcb813c6
io: make copy continue filling the buffer when writer stalls (#5066) 2022-10-03 12:58:21 +02:00
Ashish Kurmi
b821e436c5
ci: add minimum GitHub token permissions for workflows (#5072)
Signed-off-by: Ashish Kurmi <akurmi@stepsecurity.io>
2022-10-03 05:15:23 -04:00
Alice Ryhl
f4e08aec66
tokio: use const Mutex::new for globals (#5061) 2022-09-28 21:21:31 +02:00
Simon Farnsworth
96fab053ab
io: wrappers for inspecting data on IO resources (#5033) 2022-09-28 18:21:51 +00:00
Simon Farnsworth
9b87daad7e
net: don't use port 8080 in tests (#5062) 2022-09-28 11:26:24 +00:00
Erwan
e8354279fb
time: add DelayQueue::try_remove (#5052) 2022-09-28 07:50:00 +00:00
Serge Barral
78fc94099b
runtime: mitigate ABA with 32-bit queue indices when possible (#5042)
When 64-bit atomics are supported, use 32-bit queue indices. This
greatly improves resilience to ABA and has no impact on performance on
64-bit platforms.

Fixes: #5041
2022-09-27 23:02:40 +00:00
Marek Kuskowski
2df45234ef
stream: fix panic in ChunksTimeout::new (#5036) 2022-09-27 22:34:08 +00:00
grydholt
aedcec666d
io: fix doc for write_i8 to use signed integers (#5040)
Fixes: #5039
2022-09-27 22:08:20 +00:00
Dom
909de08845
sync: add merge() to semaphore permits (#4948) 2022-09-27 23:51:47 +02:00
Carl Dong
3db330dad2
examples: use write_all instead of write in hello world example (#5044) 2022-09-27 21:50:17 +00:00
Alice Ryhl
feef48fb58
ci: improve minrust check and re-enable example (#5060) 2022-09-27 21:18:13 +00:00
Alice Ryhl
47b6e194fb Merge 'tokio-1.21.2' into 'master' (#5059) 2022-09-27 21:53:36 +02:00
Alice Ryhl
a79b824faa
chore: prepare Tokio v1.21.2 (#5058) tokio-1.21.2 2022-09-27 21:42:23 +02:00
Alice Ryhl
3fbf314985 Merge 'tokio-1.20.2' into 'tokio-1.21.x' (#5057) 2022-09-27 21:13:45 +02:00
Alice Ryhl
3d95a4626f
chore: prepare Tokio v1.20.2 (#5055) tokio-1.20.2 2022-09-27 21:05:16 +02:00
Alice Ryhl
2063d6692e Merge 'tokio-1.18.3' into 'tokio-1.20.x' (#5054) 2022-09-27 10:48:37 +02:00
Alice Ryhl
5c76d070e2
chore: prepare Tokio v1.18.3 (#5051) tokio-1.18.3 2022-09-27 09:42:51 +02:00
Alice Ryhl
05e661490b
chore: don't use once_cell for 1.18.x LTS release (#5048)
The latest release of once_cell has an MSRV that is incompatible with the MSRV of the 1.18.x LTS release.
2022-09-26 10:41:35 +02:00
Hayden Stainsby
7096a80075
task: add track_caller to block_in_place and spawn_local (#5034)
Functions that may panic can be annotated with `#[track_caller]` so that
in the event of a panic, the function where the user called the
panicking function is shown instead of the file and line within Tokio
source.

This change adds `#[track_caller]` to two public APIs in tokio task
module which weren't added in #4848.
* `tokio::task::block_in_place`
* `tokio::task::spawn_local`

These APIs had call stacks that went through closures, which is a use
case not supported by `#[track_caller]`. These two cases have been
refactored so that the `panic!` call is no longer inside a closure.

Tests have been added for these two new cases.

Refs: #4413
2022-09-20 22:55:51 +02:00
Carl Lerche
d69e5bebf1
chore: release tokio-stream 0.1.10 (#5028)
Closes #5011
tokio-stream-0.1.10
2022-09-18 10:11:03 -07:00
Carl Lerche
0b92f80a65
rt: rm internal Unpark types for Handle types (#5027)
This patch removes all internal `Unpark` runtime types. The runtime now
uses the `Handle` types (`runtime::io::Handle` and
`runtime::time::Handle`) to signal threads to unpark.

Without separate `Unpark` types, future patches will be able to remove
more `Arc`s used inside various drivers.
2022-09-18 10:10:49 -07:00
Carl Lerche
cba5c1009e
rt: move driver unpark out of multi-thread parker (#5026)
This patch removes the driver Unpark handle out of the multi-thread
parker and passes a reference in when it is needed. This is a first step
towards getting rid of the separate driver unpark handle in favor of
just using the regular driver handle. Because the regular driver handle
is owned at a higher level (at the top of the worker struct).
2022-09-17 16:46:44 -07:00
Carl Lerche
cdd6eeaf70
rt: update MultiThread to use its own Handle (#5025)
This is the equivalent as tokio-rs/tokio#5023, but for the MultiThread
scheduler. This patch updates `MultiThread` to use `Arc<Handle>` for all
of its cross-thread needs instead of `Arc<Shared>`.

The effect of this change is that the multi-thread scheduler only has a
single `Arc` type, which includes the driver handles, keeping all
"shared state" together.
2022-09-16 19:57:45 -07:00
Carl Lerche
2effa7ff8a
rt: update CurrentThread to use its own Handle (#5023)
This is the next step in the internal runtime refactoring. This patch
updates `CurrentThread` to use `Arc<Handle>` for all of its cross-thread
needs instead of `Arc<Shared>`.

The effect of this change is that the current-thread scheduler only has
a single `Arc` type, which includes the driver handles, keeping all
"shared state" together.
2022-09-16 17:34:09 -07:00
Carl Lerche
ebeb78ed40
rt: split internal runtime::Handle concerns (#5022)
The `runtime::Handle` struct is part of the public API but is also used
internally. This has created a bit of tension. An earlier patch made
defined Handle as a private struct in some cases when `rt` is not
enabled.

This patch splits out internal handle concerns into a new
`scheduler::Handle` type, which will only be internal. This also defines
a `Handle` type for each scheduler variant. Eventually, the
per-scheduler `Handle` types will replace the per-scheduler `Spawner`
types, but more work is needed before we can make that change.
2022-09-16 14:05:09 -07:00
Hayden Stainsby
b5709baa91
rt: add rng_seed option to runtime::Builder (#4910)
The `tokio::select!` macro polls branches in a random order. While this
is desirable in production, for testing purposes a more deterministic
approach can be useul.

This change adds an additional parameter `rng_seed` to the runtime
`Builder` to set the random number generator seed. This value is then
used to reset the seed on the current thread when the runtime is entered
into (restoring the previous value when the thread leaves the runtime). All
threads created explicitly by the runtime also have a seed set as the
runtime is built. Each thread is set with a seed from a deterministic
sequence.

This guarantees that calls to the `tokio::select!` macro which are
performed in the same order on the same thread will poll branches in the
same order.

Additionally, the peer chosen to attempt to steal work from also uses a
deterministic sequence if `rng_seed` is set.

Both the builder parameter as well as the `RngSeed` struct are marked
unstable initially.
2022-09-16 17:41:09 +02:00
Carl Lerche
9e02759779
rt: create driver::Handle struct (#5018)
Move all individual driver handles into a single `driver::Handle`
struct.
2022-09-15 16:31:55 -07:00
Carl Lerche
198e2d8e79
rt: rename internal runtime::Kind to Scheduler (#5017)
The `runtime::Kind` enum doesn't really represent the runtime flavor,
but is an enumeration of the different scheduler types. This patch
renames the enum to reflect this.

At a later time, it is likely the `Scheduler` enum will be moved to
`tokio::runtime::scheduler`, but that is punted to a later PR.

This rename is to make space for other enums
2022-09-15 15:10:08 -07:00
Carl Lerche
57e08e7147
chore: remove println from tests (#5015)
This patch removes println statements from tests. Some of these were
used to test that types implement `fmt::Debug`. These println statements
have been replaced with an equivalent test that does not output to
STDOUT. The rest of the println statements are most likely left over
from debugging sessions.
2022-09-14 18:43:40 -07:00
Carl Lerche
98e642d234
rt: remove driver drop implementations (#5014)
Currently, drivers are responsible for clean up when they are dropped.
This is possible today because each driver struct holds a reference to
its internal handle.

As part of an effort to decouple drivers from their handles, this patch
removes the drop implementations for the time and IO driver in favor of
having the scheduler explicitly call `shutdown()` as part of its
shutdown process. The scheduler will hold a reference to both the driver
and the driver handles, so in the future, it will be able to pass in the
driver handles as part of the shutdown process.
2022-09-14 15:47:41 -07:00
Carl Lerche
3f379abda4 rt: remove unparker from time driver (#5013)
Currently, the various resource drivers use a layered approach where
each driver contains the next one. When the scheduler calls park on the
top-most driver, it does work then calls park on the inner driver it
holds. The handles do the same with unparking.

This patch is a step towards refactoring the runtime to move away from
the nested approach towards keeping all drivers and handles together in
a single runtime driver/handle. The unparker is removed from the time
handle and placed in the runtime handle and is passed into the time
driver as needed.
2022-09-14 15:38:42 -07:00
Carl Lerche
588408c060 rt: update TimerEntry to use runtime::Handle
The `TimerEntry` struct is the internal integration point for public
time APIs (`sleep`, `interval`, ...) with the time driver. Currently,
`TimerEntry` holds an ref-counted reference to the time driver handle.

This patch replaces the reference to the time driver handle with a
reference to the runtime handle. This is part of a larger effort to
consolate internal handles across the runtime.
2022-09-14 15:38:42 -07:00
Taiki Endo
ac1ae2cfbc
ci: use latest Valgrind (#4367) 2022-09-14 21:49:32 +09:00
Carl Lerche
56ffea09e5
rt: store driver handles next to scheduler handle (#5008)
In an earlier PR (#4629), driver handles were moved into the scheduler
handle (`Spawner`). This was done to let the multi-threaded scheduler
have direct access to the thread pool spawner.

However, we are now working on a greater decoupling of the runtime
internals. All drivers and schedulers will be peers, stored in a single
thread-local variable, and the scheduler will be passed the full
runtime::Handle. This will achieve the original goal of giving the
scheduler access to the thread-pool while also (hopefully) simplifying
other aspects of the code.
2022-09-13 12:40:46 -07:00
Taiki Endo
b891714bdb
ci: use --feature-powerset --depth 2 in features check (#5007)
As has been pointed out a few times in the past (e.g., #4036 (comment)), each-feature 
is not sufficient for features check.

Ideally, we'd like to check all combinations of features, but there are too many
combinations. So limit the max number of simultaneous feature flags to 2 by --depth
option. I think this should be sufficient in most cases as @carllerche said in
taiki-e/cargo-hack#58.
2022-09-13 11:05:38 -07:00
Jernej Kos
0fddb765d4
ci: add build test for x86_64-fortanix-unknown-sgx target (#5001) 2022-09-13 15:12:35 +02:00
Alice Ryhl
29e3584c4d
ci: don't auto-cancel CI on old commits on master (#5006) 2022-09-13 13:13:27 +02:00