summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorihc童鞋@提不起劲 <[email protected]>2023-08-17 19:37:20 +0800
committerGitHub <[email protected]>2023-08-17 19:37:20 +0800
commit70d3ac07b7974ec8a0b09da70042bfc0cef34a6b (patch)
tree9bece9f99b3228993d8f7724a362ccc596635dbf
parent6075c3c64904cb82afba3c7170ebf43a7ed57fb1 (diff)
refactor: use thread_local (#204)
-rw-r--r--monoio/src/lib.rs1
-rw-r--r--monoio/src/net/tcp/tfo/linux.rs11
-rw-r--r--monoio/src/task/waker_fn.rs11
3 files changed, 10 insertions, 13 deletions
diff --git a/monoio/src/lib.rs b/monoio/src/lib.rs
index 1a39d53..85afd91 100644
--- a/monoio/src/lib.rs
+++ b/monoio/src/lib.rs
@@ -7,6 +7,7 @@
#![feature(lazy_cell)]
#![feature(slice_internals)]
#![feature(stmt_expr_attributes)]
+#![feature(thread_local)]
#[macro_use]
pub mod macros;
diff --git a/monoio/src/net/tcp/tfo/linux.rs b/monoio/src/net/tcp/tfo/linux.rs
index 9ec162e..32f67df 100644
--- a/monoio/src/net/tcp/tfo/linux.rs
+++ b/monoio/src/net/tcp/tfo/linux.rs
@@ -1,8 +1,7 @@
-use std::{cell::RefCell, io, os::fd::AsRawFd};
+use std::{cell::Cell, io, os::fd::AsRawFd};
-thread_local! {
- pub(crate) static TFO_CONNECT_AVAILABLE: RefCell<bool> = RefCell::new(true);
-}
+#[thread_local]
+pub(crate) static TFO_CONNECT_AVAILABLE: Cell<bool> = Cell::new(true);
/// Call before listen.
pub(crate) fn set_tcp_fastopen<S: AsRawFd>(fd: &S, fast_open: i32) -> io::Result<()> {
@@ -32,13 +31,13 @@ pub(crate) fn set_tcp_fastopen_connect<S: AsRawFd>(fd: &S) -> io::Result<()> {
}
pub(crate) fn try_set_tcp_fastopen_connect<S: AsRawFd>(fd: &S) {
- if !TFO_CONNECT_AVAILABLE.with(|f| *f.borrow()) {
+ if !TFO_CONNECT_AVAILABLE.get() {
return;
}
match set_tcp_fastopen_connect(fd) {
Ok(_) => (),
Err(e) if e.raw_os_error() == Some(libc::ENOPROTOOPT) => {
- TFO_CONNECT_AVAILABLE.with(|f| *f.borrow_mut() = false);
+ TFO_CONNECT_AVAILABLE.set(false);
}
Err(_e) => {
#[cfg(all(debug_assertions, feature = "debug"))]
diff --git a/monoio/src/task/waker_fn.rs b/monoio/src/task/waker_fn.rs
index 0a5cc20..5d7f664 100644
--- a/monoio/src/task/waker_fn.rs
+++ b/monoio/src/task/waker_fn.rs
@@ -27,18 +27,15 @@ pub(crate) fn dummy_waker() -> Waker {
unsafe { Waker::from_raw(raw_waker()) }
}
-thread_local! {
- pub(crate) static SHOULD_POLL: Cell<bool> = Cell::new(true);
-}
+#[thread_local]
+static SHOULD_POLL: Cell<bool> = Cell::new(true);
#[inline]
pub(crate) fn should_poll() -> bool {
- SHOULD_POLL.with(|b| b.replace(false))
+ SHOULD_POLL.replace(false)
}
#[inline]
pub(crate) fn set_poll() {
- SHOULD_POLL.with(|b| {
- b.set(true);
- })
+ SHOULD_POLL.set(true);
}