3383 Commits

Author SHA1 Message Date
Carl Lerche
ca8e176ce9
rt: rm coop::budget from LocalSet::run_until (#5155)
The `LocalSet::run_until` future is just a "plain" future that should
run on a runtime that already has a coop budget. In other words, the
`run_until` future should not get its own budget but should inherit the
calling task's budget. Getting this behavior is done by removing the
call to `budget` in `run_until`.
2022-11-01 14:03:01 -07:00
Carl Lerche
a051ed726f
rt: move coop mod into runtime (#5152)
This is a step towards unifying thread-local variables. In the future,
`coop` will be updated to use the runtime context thread-local to store
its state.
2022-11-01 09:03:56 -07:00
Yiyu Lin
203a079743
sync: make Notify panic safe (#5154) 2022-11-01 14:24:01 +01:00
Carl Lerche
b1f40f4356
rt: rename some confusing internal variables/fns (#5151)
This patch does some internal renames to remove some confusion.

* `allow_blocking` is renamed to `allow_block_in_place` to indicate that
  the variable only impacts the `block_in_place()` function.

* `context::try_enter` is renamed to `context::try_set_current` to
  disambiguate between the various "enter" functions. This function only
  sets the runtime handle used by Tokio's public APIs. Entering a runtime
  is a separate operation.  # Please enter the commit message for your
  changes.

* `scheduler::Handle::enter()` is removed to consolidate methods that
  set the current context.
2022-10-31 15:06:37 -07:00
Alice Ryhl
a9d5eb2fc7
io: add lines example for StreamReader (#5145) 2022-10-31 20:40:52 +01:00
Matt Schulte
c2210dfe37
net: fix function name in UdpSocket recv documentation (#5150)
In the "cancellation safety" section of the UdpSocket recv function, "recv_from" is referenced when it should be "recv"
2022-10-31 16:10:26 +00:00
Carl Lerche
df99428c17
rt: add runtime::context to unify thread-locals (#5143)
This patch is the first step towards unifying all the thread-local
variables spread out across Tokio. A new `Context` struct is added which
will be used to replace the various thread-locals that exist today.

Initially, `Context` only holds the current runtime handle and the
random number generator. Further PRs will add other thread-local state.

A previous PR removed `runtime::context`. At that time,
`runtime::context` was used as an extra layer to access the various
runtime driver handles. This version of `runtime::context` serves a
different purpose (unifying all the thread-locals).
2022-10-31 09:05:10 -07:00
Vitaly Shukela
d1a8ec6495
sync: add Semaphore::MAX_PERMITS (#5144) 2022-10-30 21:19:37 +00:00
Abutalib Aghayev
fe1843c0e0
rt: add Handle::runtime_flavor (#5138) 2022-10-30 15:26:07 +00:00
Stepan Koltsov
15b362d2dd
sync: name mpsc semaphore types (#5146)
Make code easier to read. No functional/perf changes.
2022-10-30 15:38:48 +01:00
Daniel Bischof
620880f4ca
io: add tokio_util::io::{CopyToBytes, SinkWriter} (#5070)
Co-authored-by: Alice Ryhl <aliceryhl@google.com>
Co-authored-by: Simon Farnsworth <simon@farnz.org.uk>
2022-10-30 11:04:22 +00:00
Duarte Nunes
29c6de0e3e
sync: add PollSemaphore::poll_acquire_many (#5137) 2022-10-30 10:06:10 +01:00
Sebastian Meßmer
3886a3eaf4
sync: add Mutex::blocking_lock_owned (#5130) 2022-10-29 14:59:40 +00:00
Ryan Thomas
3b5ef4eb98
time: document that timeouts check only before poll (#5126) 2022-10-29 14:59:29 +00:00
crusader-mike
cbbf81b922
codec: expose backpressure_boundary in Framed API (#5124) 2022-10-29 14:59:22 +00:00
Harvey Hunt
1ab80ba580
process: add Command::process_group (#5114) 2022-10-29 14:59:15 +00:00
Carl Lerche
7483509746
rt: keep driver cfgs in driver.rs (#5141)
This patch removes driver related cfg_* macro calls from `scheduler` in
favor of keeping it in the driver module.
2022-10-28 13:04:09 -07:00
Carl Lerche
121f839b4b
rt: remove rng_seed+mt test and update docs (#5142)
While setting the random number generator seed with the multi-threaded
scheduler does result in deterministic behavior related to the random
number generator, threads still introduce non-determinism, making it
hard (impossible?) to test this. There also is little value in doing so.

This patch also updates the docs to remove mention of work stealing.
2022-10-28 11:55:34 -07:00
Lucio Franco
b749013a54
test: Improve tokio_test::task docs (#5132) 2022-10-28 13:55:13 -04:00
Carl Lerche
2f24451434
rt: remove runtime::context module (#5140)
The current runtime thread-local is moved to `runtime::scheduler` as
well as methods to enter and access the current handle.
2022-10-28 10:39:44 -07:00
Carl Lerche
5c2e275659
rt: move internal clock fns out of context (#5139)
A step towards getting rid of `runtime::context`. Internal functions
related to getting the current clock are moved to the time driver.
2022-10-27 14:18:39 -07:00
Carl Lerche
6c19748f90
rt: use signal driver handle via scheduler::Handle (#5135)
The signal driver still uses an `Arc` internally to track if the driver
is still running, however, using the `scheduler::Handle` to access the
signal driver handle lets us delete some code.
2022-10-27 13:07:51 -07:00
Carl Lerche
32d68fe579
rt: remove Arc from I/O driver (#5134)
The next step in the great driver cleanup. This patch removes the Arc
used in the I/O driver in favor of `runtime::scheduler::Handle`.
2022-10-27 11:13:25 -07:00
Hayden Stainsby
4bcd08b938
rt: fix rng_seed test for threaded runtime (#5133)
The improvement to the `rng_seed` tests added in #5075 missed a case in
the `rt_threaded` tests which was still checking for a specific value.
As described in that PR, this makes the tests fragile and changing tokio
internals may require updating the test.

This change fixes that half-implemented improvement so that the tests no
longer depend on the exact internal ordering, but rather compare two
runs of separate runtimes built with the same seed to check that the
results are the same.
2022-10-27 18:29:30 +02:00
Carl Lerche
cb67f28fe3
rt: switch io::handle refs with scheduler:Handle (#5128)
The `schedule::Handle` reference is the internal runtime handle. This
patch replaces owned refs to `runtime::io::Handle` with
`scheduler::Handle`.
2022-10-27 08:51:03 -07:00
Carl Lerche
58c457190b
rt: start decoupling I/O driver and I/O handle (#5127)
This is the start of applying a similar treatment to the I/O driver as
the time driver. The I/O driver will no longer hold its own reference to
the I/O handle. Instead, the handle is passed in when needed.

This patch also moves the process driver to the `runtime` module.
2022-10-26 12:07:38 -07:00
Carl Lerche
ec66a92b01
rt: signal driver now uses I/O driver directly (#5125)
The signal driver uses a `UnixStream` to receive signal events.
Previously, the signal driver used `PollEvented` internally to receive
events on the `UnixStream`. However, using `PollEvented` from
within a runtime driver created a circular link between the runtime and
the `PollEvented` instance.

This patch replaces `PollEvented` usage in favor of accessing the I/O
driver directly. The I/O driver now reserves a token for signal-related
events and tracks signal readiness internally. The signal driver queries
the I/O driver to check for signal-related readiness.
2022-10-26 10:17:59 -07:00
Carl Lerche
1ca17bef67
rt: move signal driver to runtime module (#5121)
The signal feature only requires a driver with unix platforms. The
unix signal driver uses the I/O driver. A future refactor would like to
make the signal driver use internal APIs of the I/O driver. To do this,
the signal driver must be moved to the runtime module.
2022-10-24 15:51:18 -07:00
Carl Lerche
80568dfc6d
rt: misc time driver cleanup (#5120)
Removes an unnecessary `Arc` and reduces internal state clones.
2022-10-24 13:59:47 -07:00
Alice Ryhl
b248be2879
task: document that spawned tasks execute immedaitely (#5117) 2022-10-23 20:25:12 +02:00
Alice Ryhl
d32ba2cf9d
time: document return type of timeout (#5118) 2022-10-22 19:23:40 +02:00
Carl Lerche
a03e042024
rt: remove a reference to internal time handle (#5107)
This patch removes a handle to the internal runtime driver handle held
by the runtime. This is another step towards reducing the number of Arc
refs across the runtime internals. Specifically, this change is part of
an effort to remove an Arc in the time driver itself.
2022-10-17 13:26:11 -07:00
cjw
00082c668f
time: panic in release mode when mark_pending called illegally (#5093) 2022-10-16 08:46:57 +02:00
cjw
607816491d
runtime: update the alignment of CachePadded (#5106) 2022-10-15 11:11:10 +02:00
afajl
1e437595a3
stream: document that throttle operates on ms granularity (#5101) 2022-10-14 23:45:16 +02:00
Alice Ryhl
bf5eed8fa0
time: remove Unpin bound on Throttle methods (#5105) 2022-10-14 08:44:30 +00:00
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