3192 Commits

Author SHA1 Message Date
Carl Lerche
b2f5dbea47
rt: remove handle reference from each scheduler (#5166)
Instead of each scheduler flavor holding a reference to the scheduler
handle, the scheduler handle is passed in as needed. This removes a
duplicate handle reference in the `Runtime` struct and lays the
groundwork for further handle struct tweaks.
2022-11-04 13:36:12 -07:00
Duarte Nunes
23fdd32b01
rt: export metrics about the blocking thread pool (#5161)
Publish the blocking thread pool metrics as thread-safe values, written
under the blocking thread pool's lock and able to be read in a lock-free
fashion by any reader.

Fixes #5156
2022-11-04 11:05:36 -07:00
Hubert Hirtz
5b46395a1e
sync: fix Sync assertion for AtomicWaker (#5165) 2022-11-04 13:22:30 +01:00
Carl Lerche
32da1aa9da
rt: unify entering a runtime with Handle::enter (#5163)
This is a first step towards unifying the concepts of "entering a
runtime" and setting `Handle::current`.

Previously, these two operations were performed separately at each call
site (runtime block_on, ...). This is error-prone and also requires
multiple accesses to the thread-local variable. Additionally, "entering
the runtime" conflated the concept of entering a blocking region. For
example, calling `mpsc::Receiver::recv_blocking` performed the "enter
the runtime" step. This was done to prevent blocking a runtime, as the
operation will panic when called from an existing runtime.

To untangle these concepts, the patch splits out each logical operation
into functions. In total, there are three "enter" operations:

* `set_current_handle`
* `enter_runtime`
* `enter_blocking_region`

There are some behavior changes with each function, but they
should not translate to public behavior changes. The most significant is
`enter_blocking_region` does not change the value of the thread-local
variable, which means the function can be re-entered. Since
`enter_blocking_region` is an internal-only function and we do not
re-enter, this has no public-facing impact.

Because `enter_runtime` takes a `&Handle` to combine the
`set_current_handle` operation with entering a runtime, the patch
exposes an annoyance with the current `scheduler::Handle` struct layout.
A new instance of `scheduler::Handle` must be constructed at each call
to `enter_runtime`. We can explore cleaning this up later.

This patch also does not combine the "entered runtime" thread-local
variable with the "context" thread-local variable. To keep the patch
smaller, this has been punted to a follow-up change.
2022-11-03 15:01:08 -07:00
Carl Lerche
df6348fb4a
chore: fix tests for Rust 1.65 release (#5164)
Rust 1.65 reduces some struct sizes.
2022-11-03 14:24:10 -07:00
Rustom Shareef
74a29be607
chore: typo in TryLockError for RwLock::try_write (#5160)
Fixes an incoherent sentence describing when RwLock::try_write (and related) would error with TryLockError.
2022-11-02 18:13:31 -05:00
Carl Lerche
26791a62bc
rt: move Runtime into its own file (#5159)
This removes the `Runtime` implementation from a cfg macro so it can be
formatted.
2022-11-02 15:44:56 -07:00
Carl Lerche
467adec4e1
rt: move park logic into runtime module (#5158)
The runtime is the primary user of parking. Moving park into runtime
will help future cleanups around combining context entering w/ block_on
calls.
2022-11-02 14:37:44 -07:00
Carl Lerche
d8ee205530
rt: move budget state to context thread-local (#5157)
This patch consolidates the budget thread-local state with the
`runtime::context` thread local. This reduces the number of thread-local
variables used by Tokio by one.
2022-11-02 12:24:12 -07:00
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