mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-09 00:00:08 +02:00
Compare commits
18
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
edd172cd32 | ||
|
|
9877fa2a97 | ||
|
|
0f898a3148 | ||
|
|
d6a9ef5333 | ||
|
|
2a180188c6 | ||
|
|
f3ce29a003 | ||
|
|
0d8fe5fe75 | ||
|
|
171ce0ff8d | ||
|
|
d6ea7a742b | ||
|
|
ba81945ffc | ||
|
|
763bdc967e | ||
|
|
9f98535877 | ||
|
|
9241c3eddf | ||
|
|
699573d550 | ||
|
|
3d95a4626f | ||
|
|
2063d6692e | ||
|
|
5c76d070e2 | ||
|
|
05e661490b |
@@ -6,7 +6,7 @@ jobs:
|
||||
resource_class: arm.medium
|
||||
environment:
|
||||
# Change to pin rust version
|
||||
RUST_STABLE: stable
|
||||
RUST_STABLE: 1.62.1
|
||||
steps:
|
||||
- checkout
|
||||
- run:
|
||||
@@ -22,4 +22,4 @@ jobs:
|
||||
workflows:
|
||||
ci:
|
||||
jobs:
|
||||
- test-arm
|
||||
- test-arm
|
||||
|
||||
+7
-5
@@ -1,7 +1,9 @@
|
||||
only_if: $CIRRUS_TAG == '' && ($CIRRUS_PR != '' || $CIRRUS_BRANCH == 'master' || $CIRRUS_BRANCH =~ 'tokio-.*')
|
||||
auto_cancellation: $CIRRUS_BRANCH != 'master' && $CIRRUS_BRANCH !=~ 'tokio-.*'
|
||||
freebsd_instance:
|
||||
image: freebsd-12-3-release-amd64
|
||||
image_family: freebsd-13-1
|
||||
env:
|
||||
RUST_STABLE: stable
|
||||
RUST_STABLE: 1.62.1
|
||||
RUST_NIGHTLY: nightly-2022-03-21
|
||||
RUSTFLAGS: -D warnings
|
||||
|
||||
@@ -12,7 +14,7 @@ env:
|
||||
task:
|
||||
name: FreeBSD 64-bit
|
||||
setup_script:
|
||||
- pkg install -y bash curl
|
||||
- pkg install -y bash
|
||||
- curl https://sh.rustup.rs -sSf --output rustup.sh
|
||||
- sh rustup.sh -y --profile minimal --default-toolchain $RUST_STABLE
|
||||
- . $HOME/.cargo/env
|
||||
@@ -29,7 +31,7 @@ task:
|
||||
RUSTFLAGS: --cfg docsrs --cfg tokio_unstable
|
||||
RUSTDOCFLAGS: --cfg docsrs --cfg tokio_unstable -Dwarnings
|
||||
setup_script:
|
||||
- pkg install -y bash curl
|
||||
- pkg install -y bash
|
||||
- curl https://sh.rustup.rs -sSf --output rustup.sh
|
||||
- sh rustup.sh -y --profile minimal --default-toolchain $RUST_NIGHTLY
|
||||
- . $HOME/.cargo/env
|
||||
@@ -43,7 +45,7 @@ task:
|
||||
task:
|
||||
name: FreeBSD 32-bit
|
||||
setup_script:
|
||||
- pkg install -y bash curl
|
||||
- pkg install -y bash
|
||||
- curl https://sh.rustup.rs -sSf --output rustup.sh
|
||||
- sh rustup.sh -y --profile minimal --default-toolchain $RUST_STABLE
|
||||
- . $HOME/.cargo/env
|
||||
|
||||
+12
-26
@@ -10,9 +10,9 @@ env:
|
||||
RUSTFLAGS: -Dwarnings
|
||||
RUST_BACKTRACE: 1
|
||||
# Change to specific Rust release to pin
|
||||
rust_stable: stable
|
||||
rust_stable: 1.62.1
|
||||
rust_nightly: nightly-2022-04-18
|
||||
rust_clippy: 1.52.0
|
||||
rust_clippy: 1.56.0
|
||||
# When updating this, also update:
|
||||
# - README.md
|
||||
# - tokio/README.md
|
||||
@@ -41,7 +41,6 @@ jobs:
|
||||
- features
|
||||
- minrust
|
||||
- fmt
|
||||
- clippy
|
||||
- docs
|
||||
- valgrind
|
||||
- loom-compile
|
||||
@@ -72,7 +71,7 @@ jobs:
|
||||
run: rustup update stable
|
||||
- uses: Swatinem/rust-cache@v1
|
||||
- name: Install cargo-hack
|
||||
run: cargo install cargo-hack
|
||||
run: cargo install cargo-hack --version 0.5.26
|
||||
|
||||
# Run `tokio` with `full` features. This excludes testing utilities which
|
||||
# can alter the runtime behavior of Tokio.
|
||||
@@ -275,7 +274,7 @@ jobs:
|
||||
override: true
|
||||
- uses: Swatinem/rust-cache@v1
|
||||
- name: Install cargo-hack
|
||||
run: cargo install cargo-hack
|
||||
run: cargo install cargo-hack --version 0.5.26
|
||||
- name: check --each-feature
|
||||
run: cargo hack check --all --each-feature -Z avoid-dev-deps
|
||||
# Try with unstable feature flags
|
||||
@@ -295,8 +294,9 @@ jobs:
|
||||
toolchain: ${{ env.rust_min }}
|
||||
override: true
|
||||
- uses: Swatinem/rust-cache@v1
|
||||
- name: "test --workspace --all-features"
|
||||
run: cargo check --workspace --all-features
|
||||
- name: "test --all-features"
|
||||
run: cargo check --all-features
|
||||
working-directory: tokio
|
||||
|
||||
minimal-versions:
|
||||
name: minimal-versions
|
||||
@@ -310,7 +310,7 @@ jobs:
|
||||
override: true
|
||||
- uses: Swatinem/rust-cache@v1
|
||||
- name: Install cargo-hack
|
||||
run: cargo install cargo-hack
|
||||
run: cargo install cargo-hack --version 0.5.26
|
||||
- name: "check --all-features -Z minimal-versions"
|
||||
run: |
|
||||
# Remove dev-dependencies from Cargo.toml to prevent the next `cargo update`
|
||||
@@ -351,22 +351,6 @@ jobs:
|
||||
exit 1
|
||||
fi
|
||||
|
||||
clippy:
|
||||
name: clippy
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Install Rust ${{ env.rust_clippy }}
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: ${{ env.rust_clippy }}
|
||||
override: true
|
||||
components: clippy
|
||||
- uses: Swatinem/rust-cache@v1
|
||||
# Run clippy
|
||||
- name: "clippy --all"
|
||||
run: cargo clippy --all --tests --all-features
|
||||
|
||||
docs:
|
||||
name: docs
|
||||
runs-on: ubuntu-latest
|
||||
@@ -469,10 +453,10 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Install Rust ${{ env.rust_stable }}
|
||||
- name: Install Rust stable
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: ${{ env.rust_stable }}
|
||||
toolchain: stable
|
||||
override: true
|
||||
- uses: Swatinem/rust-cache@v1
|
||||
|
||||
@@ -497,3 +481,5 @@ jobs:
|
||||
# TODO: this should become: `cargo hack wasi test --each-feature`
|
||||
run: cargo wasi test --test rt_yield --features wasi-rt
|
||||
working-directory: tests-integration
|
||||
env:
|
||||
RUSTFLAGS: ""
|
||||
|
||||
@@ -11,7 +11,7 @@ env:
|
||||
RUSTFLAGS: -Dwarnings
|
||||
RUST_BACKTRACE: 1
|
||||
# Change to specific Rust release to pin
|
||||
rust_stable: stable
|
||||
rust_stable: 1.62.1
|
||||
|
||||
jobs:
|
||||
loom:
|
||||
|
||||
@@ -9,7 +9,7 @@ env:
|
||||
RUSTFLAGS: -Dwarnings
|
||||
RUST_BACKTRACE: 1
|
||||
# Change to specific Rust release to pin
|
||||
rust_stable: stable
|
||||
rust_stable: 1.62.1
|
||||
|
||||
jobs:
|
||||
stess-test:
|
||||
|
||||
Generated
+32
@@ -0,0 +1,32 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "predicates"
|
||||
version = "1.0.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f49cfaf7fdaa3bfacc6fa3e7054e65148878354a5cfddcf661df4c851f8021df"
|
||||
dependencies = [
|
||||
"difference",
|
||||
"float-cmp",
|
||||
"normalize-line-endings",
|
||||
"predicates-core",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "predicates-core"
|
||||
version = "1.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72f883590242d3c6fc5bf50299011695fa6590c2c70eac95ee1bdb9a733ad1a2"
|
||||
|
||||
[[package]]
|
||||
name = "predicates-tree"
|
||||
version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "54ff541861505aabf6ea722d2131ee980b8276e10a1297b94e896dd8b621850d"
|
||||
dependencies = [
|
||||
"predicates-core",
|
||||
"termtree",
|
||||
]
|
||||
@@ -56,7 +56,7 @@ Make sure you activated the full features of the tokio crate on Cargo.toml:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
tokio = { version = "1.20.1", features = ["full"] }
|
||||
tokio = { version = "1.20.5", features = ["full"] }
|
||||
```
|
||||
Then, on your main.rs:
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@ serde_derive = "1.0"
|
||||
serde_json = "1.0"
|
||||
httparse = "1.0"
|
||||
httpdate = "1.0"
|
||||
once_cell = "1.5.2"
|
||||
rand = "0.8.3"
|
||||
|
||||
[target.'cfg(windows)'.dev-dependencies.winapi]
|
||||
@@ -71,11 +70,6 @@ path = "udp-codec.rs"
|
||||
name = "tinyhttp"
|
||||
path = "tinyhttp.rs"
|
||||
|
||||
[[example]]
|
||||
name = "custom-executor"
|
||||
path = "custom-executor.rs"
|
||||
|
||||
|
||||
[[example]]
|
||||
name = "custom-executor-tokio-context"
|
||||
path = "custom-executor-tokio-context.rs"
|
||||
|
||||
+71
-1
@@ -1,3 +1,41 @@
|
||||
# 1.20.5 (May 28, 2023)
|
||||
|
||||
Forward ports 1.18.6 changes.
|
||||
|
||||
### Fixed
|
||||
|
||||
- deps: disable default features for mio ([#5728])
|
||||
|
||||
[#5728]: https://github.com/tokio-rs/tokio/pull/5728
|
||||
|
||||
# 1.20.4 (January 17, 2023)
|
||||
|
||||
Forward ports 1.18.5 changes.
|
||||
|
||||
### Fixed
|
||||
|
||||
- io: fix unsoundness in `ReadHalf::unsplit` ([#5375])
|
||||
|
||||
[#5375]: https://github.com/tokio-rs/tokio/pull/5375
|
||||
|
||||
# 1.20.3 (January 3, 2022)
|
||||
|
||||
This release forward ports changes from 1.18.4.
|
||||
|
||||
### Fixed
|
||||
|
||||
- net: fix Windows named pipe server builder to maintain option when toggling
|
||||
pipe mode ([#5336]).
|
||||
|
||||
[#5336]: https://github.com/tokio-rs/tokio/pull/5336
|
||||
|
||||
# 1.20.2 (September 27, 2022)
|
||||
|
||||
This release removes the dependency on the `once_cell` crate to restore the MSRV
|
||||
of the 1.20.x LTS release. ([#5048])
|
||||
|
||||
[#5048]: https://github.com/tokio-rs/tokio/pull/5048
|
||||
|
||||
# 1.20.1 (July 25, 2022)
|
||||
|
||||
### Fixed
|
||||
@@ -116,6 +154,38 @@ This release fixes a bug in `Notified::enable`. ([#4747])
|
||||
[#4729]: https://github.com/tokio-rs/tokio/pull/4729
|
||||
[#4739]: https://github.com/tokio-rs/tokio/pull/4739
|
||||
|
||||
# 1.18.6 (May 28, 2023)
|
||||
|
||||
### Fixed
|
||||
|
||||
- deps: disable default features for mio ([#5728])
|
||||
|
||||
[#5728]: https://github.com/tokio-rs/tokio/pull/5728
|
||||
|
||||
# 1.18.5 (January 17, 2023)
|
||||
|
||||
### Fixed
|
||||
|
||||
- io: fix unsoundness in `ReadHalf::unsplit` ([#5375])
|
||||
|
||||
[#5375]: https://github.com/tokio-rs/tokio/pull/5375
|
||||
|
||||
# 1.18.4 (January 3, 2022)
|
||||
|
||||
### Fixed
|
||||
|
||||
- net: fix Windows named pipe server builder to maintain option when toggling
|
||||
pipe mode ([#5336]).
|
||||
|
||||
[#5336]: https://github.com/tokio-rs/tokio/pull/5336
|
||||
|
||||
# 1.18.3 (September 27, 2022)
|
||||
|
||||
This release removes the dependency on the `once_cell` crate to restore the MSRV
|
||||
of the 1.18.x LTS release. ([#5048])
|
||||
|
||||
[#5048]: https://github.com/tokio-rs/tokio/pull/5048
|
||||
|
||||
# 1.18.2 (May 5, 2022)
|
||||
|
||||
Add missing features for the `winapi` dependency. ([#4663])
|
||||
@@ -236,7 +306,7 @@ performance improvements.
|
||||
- time: use bit manipulation instead of modulo to improve performance ([#4480])
|
||||
- net: use `std::future::Ready` instead of our own `Ready` future ([#4271])
|
||||
- replace deprecated `atomic::spin_loop_hint` with `hint::spin_loop` ([#4491])
|
||||
- fix miri failures in intrusive linked lists ([#4397])
|
||||
- fix miri failures in intrusive linked lists ([#4397])
|
||||
|
||||
### Documented
|
||||
|
||||
|
||||
+3
-6
@@ -6,7 +6,7 @@ name = "tokio"
|
||||
# - README.md
|
||||
# - Update CHANGELOG.md.
|
||||
# - Create "v1.0.x" git tag.
|
||||
version = "1.20.1"
|
||||
version = "1.20.5"
|
||||
edition = "2018"
|
||||
rust-version = "1.49"
|
||||
authors = ["Tokio Contributors <[email protected]>"]
|
||||
@@ -61,7 +61,6 @@ net = [
|
||||
]
|
||||
process = [
|
||||
"bytes",
|
||||
"once_cell",
|
||||
"libc",
|
||||
"mio/os-poll",
|
||||
"mio/os-ext",
|
||||
@@ -75,13 +74,12 @@ process = [
|
||||
"winapi/minwindef",
|
||||
]
|
||||
# Includes basic task execution capabilities
|
||||
rt = ["once_cell"]
|
||||
rt = []
|
||||
rt-multi-thread = [
|
||||
"num_cpus",
|
||||
"rt",
|
||||
]
|
||||
signal = [
|
||||
"once_cell",
|
||||
"libc",
|
||||
"mio/os-poll",
|
||||
"mio/net",
|
||||
@@ -110,9 +108,8 @@ pin-project-lite = "0.2.0"
|
||||
|
||||
# Everything else is optional...
|
||||
bytes = { version = "1.0.0", optional = true }
|
||||
once_cell = { version = "1.5.2", optional = true }
|
||||
memchr = { version = "2.2", optional = true }
|
||||
mio = { version = "0.8.1", optional = true }
|
||||
mio = { version = "0.8.1", optional = true, default-features = false }
|
||||
socket2 = { version = "0.4.4", optional = true, features = [ "all" ] }
|
||||
num_cpus = { version = "1.8.0", optional = true }
|
||||
parking_lot = { version = "0.12.0", optional = true }
|
||||
|
||||
+1
-1
@@ -56,7 +56,7 @@ Make sure you activated the full features of the tokio crate on Cargo.toml:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
tokio = { version = "1.20.1", features = ["full"] }
|
||||
tokio = { version = "1.20.5", features = ["full"] }
|
||||
```
|
||||
Then, on your main.rs:
|
||||
|
||||
|
||||
@@ -75,7 +75,10 @@ impl<T> ReadHalf<T> {
|
||||
/// This can be checked ahead of time by comparing the stream ID
|
||||
/// of the two halves.
|
||||
#[track_caller]
|
||||
pub fn unsplit(self, wr: WriteHalf<T>) -> T {
|
||||
pub fn unsplit(self, wr: WriteHalf<T>) -> T
|
||||
where
|
||||
T: Unpin,
|
||||
{
|
||||
if self.is_pair_of(&wr) {
|
||||
drop(wr);
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ impl<T> Mutex<T> {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[cfg(all(feature = "parking_lot", not(all(loom, test)),))]
|
||||
#[cfg(all(feature = "parking_lot", not(all(loom, test))))]
|
||||
#[cfg_attr(docsrs, doc(cfg(all(feature = "parking_lot",))))]
|
||||
pub(crate) const fn const_new(t: T) -> Mutex<T> {
|
||||
Mutex(PhantomData, parking_lot::const_mutex(t))
|
||||
|
||||
@@ -1681,11 +1681,10 @@ impl ServerOptions {
|
||||
///
|
||||
/// [`dwPipeMode`]: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createnamedpipea
|
||||
pub fn pipe_mode(&mut self, pipe_mode: PipeMode) -> &mut Self {
|
||||
self.pipe_mode = match pipe_mode {
|
||||
PipeMode::Byte => winbase::PIPE_TYPE_BYTE,
|
||||
PipeMode::Message => winbase::PIPE_TYPE_MESSAGE,
|
||||
};
|
||||
|
||||
let is_msg = matches!(pipe_mode, PipeMode::Message);
|
||||
// Pipe mode is implemented as a bit flag 0x4. Set is message and unset
|
||||
// is byte.
|
||||
bool_flag!(self.pipe_mode, is_msg, winbase::PIPE_TYPE_MESSAGE);
|
||||
self
|
||||
}
|
||||
|
||||
@@ -2412,3 +2411,48 @@ unsafe fn named_pipe_info(handle: RawHandle) -> io::Result<PipeInfo> {
|
||||
max_instances,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use self::winbase::{PIPE_REJECT_REMOTE_CLIENTS, PIPE_TYPE_BYTE, PIPE_TYPE_MESSAGE};
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn opts_default_pipe_mode() {
|
||||
let opts = ServerOptions::new();
|
||||
assert_eq!(opts.pipe_mode, PIPE_TYPE_BYTE | PIPE_REJECT_REMOTE_CLIENTS);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opts_unset_reject_remote() {
|
||||
let mut opts = ServerOptions::new();
|
||||
opts.reject_remote_clients(false);
|
||||
assert_eq!(opts.pipe_mode & PIPE_REJECT_REMOTE_CLIENTS, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opts_set_pipe_mode_maintains_reject_remote_clients() {
|
||||
let mut opts = ServerOptions::new();
|
||||
opts.pipe_mode(PipeMode::Byte);
|
||||
assert_eq!(opts.pipe_mode, PIPE_TYPE_BYTE | PIPE_REJECT_REMOTE_CLIENTS);
|
||||
|
||||
opts.reject_remote_clients(false);
|
||||
opts.pipe_mode(PipeMode::Byte);
|
||||
assert_eq!(opts.pipe_mode, PIPE_TYPE_BYTE);
|
||||
|
||||
opts.reject_remote_clients(true);
|
||||
opts.pipe_mode(PipeMode::Byte);
|
||||
assert_eq!(opts.pipe_mode, PIPE_TYPE_BYTE | PIPE_REJECT_REMOTE_CLIENTS);
|
||||
|
||||
opts.reject_remote_clients(false);
|
||||
opts.pipe_mode(PipeMode::Message);
|
||||
assert_eq!(opts.pipe_mode, PIPE_TYPE_MESSAGE);
|
||||
|
||||
opts.reject_remote_clients(true);
|
||||
opts.pipe_mode(PipeMode::Message);
|
||||
assert_eq!(
|
||||
opts.pipe_mode,
|
||||
PIPE_TYPE_MESSAGE | PIPE_REJECT_REMOTE_CLIENTS
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,10 +34,10 @@ use crate::process::kill::Kill;
|
||||
use crate::process::SpawnedChild;
|
||||
use crate::signal::unix::driver::Handle as SignalHandle;
|
||||
use crate::signal::unix::{signal, Signal, SignalKind};
|
||||
use crate::util::once_cell::OnceCell;
|
||||
|
||||
use mio::event::Source;
|
||||
use mio::unix::SourceFd;
|
||||
use once_cell::sync::Lazy;
|
||||
use std::fmt;
|
||||
use std::fs::File;
|
||||
use std::future::Future;
|
||||
@@ -64,25 +64,29 @@ impl Kill for StdChild {
|
||||
}
|
||||
}
|
||||
|
||||
static ORPHAN_QUEUE: Lazy<OrphanQueueImpl<StdChild>> = Lazy::new(OrphanQueueImpl::new);
|
||||
fn get_orphan_queue() -> &'static OrphanQueueImpl<StdChild> {
|
||||
static ORPHAN_QUEUE: OnceCell<OrphanQueueImpl<StdChild>> = OnceCell::new();
|
||||
|
||||
ORPHAN_QUEUE.get(OrphanQueueImpl::new)
|
||||
}
|
||||
|
||||
pub(crate) struct GlobalOrphanQueue;
|
||||
|
||||
impl fmt::Debug for GlobalOrphanQueue {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
ORPHAN_QUEUE.fmt(fmt)
|
||||
get_orphan_queue().fmt(fmt)
|
||||
}
|
||||
}
|
||||
|
||||
impl GlobalOrphanQueue {
|
||||
fn reap_orphans(handle: &SignalHandle) {
|
||||
ORPHAN_QUEUE.reap_orphans(handle)
|
||||
get_orphan_queue().reap_orphans(handle)
|
||||
}
|
||||
}
|
||||
|
||||
impl OrphanQueue<StdChild> for GlobalOrphanQueue {
|
||||
fn push_orphan(&self, orphan: StdChild) {
|
||||
ORPHAN_QUEUE.push_orphan(orphan)
|
||||
get_orphan_queue().push_orphan(orphan)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -505,11 +505,17 @@ impl Id {
|
||||
|
||||
cfg_not_has_atomic_u64! {
|
||||
pub(crate) fn next() -> Self {
|
||||
use once_cell::sync::Lazy;
|
||||
use crate::util::once_cell::OnceCell;
|
||||
use crate::loom::sync::Mutex;
|
||||
|
||||
static NEXT_ID: Lazy<Mutex<u64>> = Lazy::new(|| Mutex::new(1));
|
||||
let mut lock = NEXT_ID.lock();
|
||||
fn init_next_id() -> Mutex<u64> {
|
||||
Mutex::new(1)
|
||||
}
|
||||
|
||||
static NEXT_ID: OnceCell<Mutex<u64>> = OnceCell::new();
|
||||
|
||||
let next_id = NEXT_ID.get(init_next_id);
|
||||
let mut lock = next_id.lock();
|
||||
let id = *lock;
|
||||
*lock += 1;
|
||||
Self(id)
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
#![allow(clippy::unit_arg)]
|
||||
|
||||
use crate::signal::os::{OsExtraData, OsStorage};
|
||||
|
||||
use crate::sync::watch;
|
||||
use crate::util::once_cell::OnceCell;
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
use std::ops;
|
||||
use std::pin::Pin;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
@@ -152,19 +151,25 @@ impl Globals {
|
||||
}
|
||||
}
|
||||
|
||||
fn globals_init() -> Globals
|
||||
where
|
||||
OsExtraData: 'static + Send + Sync + Init,
|
||||
OsStorage: 'static + Send + Sync + Init,
|
||||
{
|
||||
Globals {
|
||||
extra: OsExtraData::init(),
|
||||
registry: Registry::new(OsStorage::init()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn globals() -> Pin<&'static Globals>
|
||||
where
|
||||
OsExtraData: 'static + Send + Sync + Init,
|
||||
OsStorage: 'static + Send + Sync + Init,
|
||||
{
|
||||
static GLOBALS: Lazy<Pin<Box<Globals>>> = Lazy::new(|| {
|
||||
Box::pin(Globals {
|
||||
extra: OsExtraData::init(),
|
||||
registry: Registry::new(OsStorage::init()),
|
||||
})
|
||||
});
|
||||
static GLOBALS: OnceCell<Globals> = OnceCell::new();
|
||||
|
||||
GLOBALS.as_ref()
|
||||
Pin::new(GLOBALS.get(globals_init))
|
||||
}
|
||||
|
||||
#[cfg(all(test, not(loom)))]
|
||||
|
||||
@@ -6,6 +6,15 @@ cfg_io_driver! {
|
||||
#[cfg(feature = "rt")]
|
||||
pub(crate) mod atomic_cell;
|
||||
|
||||
cfg_has_atomic_u64! {
|
||||
#[cfg(any(feature = "signal", all(unix, feature = "process")))]
|
||||
pub(crate) mod once_cell;
|
||||
}
|
||||
cfg_not_has_atomic_u64! {
|
||||
#[cfg(any(feature = "rt", feature = "signal", all(unix, feature = "process")))]
|
||||
pub(crate) mod once_cell;
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
// io driver uses `WakeList` directly
|
||||
feature = "net",
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
#![cfg_attr(loom, allow(dead_code))]
|
||||
use std::cell::UnsafeCell;
|
||||
use std::mem::MaybeUninit;
|
||||
use std::sync::Once;
|
||||
|
||||
pub(crate) struct OnceCell<T> {
|
||||
once: Once,
|
||||
value: UnsafeCell<MaybeUninit<T>>,
|
||||
}
|
||||
|
||||
unsafe impl<T: Send + Sync> Send for OnceCell<T> {}
|
||||
unsafe impl<T: Send + Sync> Sync for OnceCell<T> {}
|
||||
|
||||
impl<T> OnceCell<T> {
|
||||
pub(crate) const fn new() -> Self {
|
||||
Self {
|
||||
once: Once::new(),
|
||||
value: UnsafeCell::new(MaybeUninit::uninit()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the value inside this cell, intiailizing it using the provided
|
||||
/// function if necessary.
|
||||
///
|
||||
/// If the `init` closure panics, then the `OnceCell` is poisoned and all
|
||||
/// future calls to `get` will panic.
|
||||
#[inline]
|
||||
pub(crate) fn get(&self, init: fn() -> T) -> &T {
|
||||
if !self.once.is_completed() {
|
||||
self.do_init(init);
|
||||
}
|
||||
|
||||
// Safety: The `std::sync::Once` guarantees that we can only reach this
|
||||
// line if a `call_once` closure has been run exactly once and without
|
||||
// panicking. Thus, the value is not uninitialized.
|
||||
//
|
||||
// There is also no race because the only `&self` method that modifies
|
||||
// `value` is `do_init`, but if the `call_once` closure is still
|
||||
// running, then no thread has gotten past the `call_once`.
|
||||
unsafe { &*(self.value.get() as *const T) }
|
||||
}
|
||||
|
||||
#[cold]
|
||||
fn do_init(&self, init: fn() -> T) {
|
||||
let value_ptr = self.value.get() as *mut T;
|
||||
|
||||
self.once.call_once(|| {
|
||||
let set_to = init();
|
||||
|
||||
// Safety: The `std::sync::Once` guarantees that this initialization
|
||||
// will run at most once, and that no thread can get past the
|
||||
// `call_once` until it has run exactly once. Thus, we have
|
||||
// exclusive access to `value`.
|
||||
unsafe {
|
||||
std::ptr::write(value_ptr, set_to);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Drop for OnceCell<T> {
|
||||
fn drop(&mut self) {
|
||||
if self.once.is_completed() {
|
||||
let value_ptr = self.value.get() as *mut T;
|
||||
unsafe {
|
||||
std::ptr::drop_in_place(value_ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user