Skip to content

Commit 05e1e03

Browse files
authored
feat(runtime): enable current_thread_id on nightly (#548)
1 parent c92fb64 commit 05e1e03

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

compio-runtime/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ time = []
6767
# Enable it to always notify the driver when a task schedules.
6868
notify-always = []
6969

70+
current_thread_id = []
71+
nightly = ["current_thread_id"]
72+
7073
[[test]]
7174
name = "custom_loop"
7275
required-features = ["event", "time"]

compio-runtime/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//! ```
1010
1111
#![cfg_attr(docsrs, feature(doc_cfg))]
12+
#![cfg_attr(feature = "current_thread_id", feature(current_thread_id))]
1213
#![warn(missing_docs)]
1314

1415
mod affinity;

compio-runtime/src/runtime/send_wrapper.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,31 @@
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
99

10+
#[cfg(feature = "current_thread_id")]
11+
use std::thread::current_id;
1012
use std::{
11-
cell::Cell,
1213
mem::{self, ManuallyDrop},
1314
thread::{self, ThreadId},
1415
};
1516

16-
thread_local! {
17-
static THREAD_ID: Cell<ThreadId> = Cell::new(thread::current().id());
17+
#[cfg(not(feature = "current_thread_id"))]
18+
mod imp {
19+
use std::{
20+
cell::Cell,
21+
thread::{self, ThreadId},
22+
};
23+
thread_local! {
24+
static THREAD_ID: Cell<ThreadId> = Cell::new(thread::current().id());
25+
}
26+
27+
pub fn current_id() -> ThreadId {
28+
THREAD_ID.get()
29+
}
1830
}
1931

32+
#[cfg(not(feature = "current_thread_id"))]
33+
use imp::current_id;
34+
2035
/// A wrapper that copied from `send_wrapper` crate, with our own optimizations.
2136
pub struct SendWrapper<T> {
2237
data: ManuallyDrop<T>,
@@ -30,15 +45,15 @@ impl<T> SendWrapper<T> {
3045
pub fn new(data: T) -> SendWrapper<T> {
3146
SendWrapper {
3247
data: ManuallyDrop::new(data),
33-
thread_id: THREAD_ID.get(),
48+
thread_id: current_id(),
3449
}
3550
}
3651

3752
/// Returns `true` if the value can be safely accessed from within the
3853
/// current thread.
3954
#[inline]
4055
pub fn valid(&self) -> bool {
41-
self.thread_id == THREAD_ID.get()
56+
self.thread_id == current_id()
4257
}
4358

4459
/// Returns a reference to the contained value.

compio/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ ring = ["compio-tls?/ring", "compio-quic?/ring", "compio-ws?/ring"]
121121

122122
# Nightly features
123123
allocator_api = ["compio-buf/allocator_api", "compio-io?/allocator_api"]
124+
current_thread_id = ["compio-runtime/current_thread_id"]
124125
lazy_cell = ["compio-signal?/lazy_cell"]
125126
linux_pidfd = ["compio-process?/linux_pidfd"]
126127
once_cell_try = [
@@ -139,6 +140,7 @@ try_trait_v2 = ["compio-buf/try_trait_v2"]
139140
windows_by_handle = ["compio-fs?/windows_by_handle"]
140141
nightly = [
141142
"allocator_api",
143+
"current_thread_id",
142144
"lazy_cell",
143145
"linux_pidfd",
144146
"once_cell_try",

0 commit comments

Comments
 (0)