forked from tokio-rs/tokio
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rt: move scheduler ctxs to runtime::context (tokio-rs#5727)
This commit eliminates the current_thread::CURRENT and multi_thread::current thread-local variables in favor of using `runtime::context`. This is another step towards reducing the total number of thread-local variables used by Tokio.
- Loading branch information
1 parent
d274ef3
commit 9f9db7d
Showing
8 changed files
with
199 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use std::cell::Cell; | ||
use std::ptr; | ||
|
||
/// Scoped thread-local storage | ||
pub(super) struct Scoped<T> { | ||
pub(super) inner: Cell<*const T>, | ||
} | ||
|
||
unsafe impl<T> Sync for Scoped<T> {} | ||
|
||
impl<T> Scoped<T> { | ||
pub(super) fn new() -> Scoped<T> { | ||
Scoped { | ||
inner: Cell::new(ptr::null()), | ||
} | ||
} | ||
|
||
/// Inserts a value into the scoped cell for the duration of the closure | ||
pub(super) fn set<F, R>(&self, t: &T, f: F) -> R | ||
where | ||
F: FnOnce() -> R, | ||
{ | ||
struct Reset<'a, T> { | ||
cell: &'a Cell<*const T>, | ||
prev: *const T, | ||
} | ||
|
||
impl<T> Drop for Reset<'_, T> { | ||
fn drop(&mut self) { | ||
self.cell.set(self.prev); | ||
} | ||
} | ||
|
||
let prev = self.inner.get(); | ||
self.inner.set(t as *const _); | ||
|
||
let _reset = Reset { | ||
cell: &self.inner, | ||
prev, | ||
}; | ||
|
||
f() | ||
} | ||
|
||
/// Gets the value out of the scoped cell; | ||
pub(super) fn with<F, R>(&self, f: F) -> R | ||
where | ||
F: FnOnce(Option<&T>) -> R, | ||
{ | ||
let val = self.inner.get(); | ||
|
||
if val.is_null() { | ||
f(None) | ||
} else { | ||
unsafe { f(Some(&*(val as *const T))) } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.