From 70d3ac07b7974ec8a0b09da70042bfc0cef34a6b Mon Sep 17 00:00:00 2001 From: "ihc童鞋@提不起劲" Date: Thu, 17 Aug 2023 19:37:20 +0800 Subject: refactor: use thread_local (#204) --- monoio/src/lib.rs | 1 + monoio/src/net/tcp/tfo/linux.rs | 11 +++++------ monoio/src/task/waker_fn.rs | 11 ++++------- 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 = RefCell::new(true); -} +#[thread_local] +pub(crate) static TFO_CONNECT_AVAILABLE: Cell = Cell::new(true); /// Call before listen. pub(crate) fn set_tcp_fastopen(fd: &S, fast_open: i32) -> io::Result<()> { @@ -32,13 +31,13 @@ pub(crate) fn set_tcp_fastopen_connect(fd: &S) -> io::Result<()> { } pub(crate) fn try_set_tcp_fastopen_connect(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 = Cell::new(true); -} +#[thread_local] +static SHOULD_POLL: Cell = 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); } -- cgit v1.2.3