-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rt: implement task::Id using StaticAtomicU64
#5282
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub(crate) use std::sync::atomic::{AtomicU64, Ordering}; | ||
|
||
/// Alias `AtomicU64` to `StaticAtomicU64` | ||
pub(crate) type StaticAtomicU64 = AtomicU64; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use super::AtomicU64; | ||
use crate::loom::sync::Mutex; | ||
|
||
pub(crate) type StaticAtomicU64 = AtomicU64; | ||
|
||
impl AtomicU64 { | ||
pub(crate) const fn new(val: u64) -> Self { | ||
Self { | ||
inner: Mutex::const_new(val), | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use super::AtomicU64; | ||
use crate::loom::sync::{Mutex, atomic::Ordering}; | ||
use crate::util::once_cell::OnceCell; | ||
|
||
pub(crate) struct StaticAtomicU64 { | ||
init: u64, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is a bit annoying to store the initial value forever, but this is only for platforms that do not have |
||
cell: OnceCell<Mutex<u64>>, | ||
} | ||
|
||
impl AtomicU64 { | ||
pub(crate) fn new(val: u64) -> Self { | ||
Self { | ||
inner: Mutex::new(val), | ||
} | ||
} | ||
} | ||
|
||
impl StaticAtomicU64 { | ||
pub(crate) const fn new(val: u64) -> StaticAtomicU64 { | ||
StaticAtomicU64 { | ||
init: val, | ||
cell: OnceCell::new(), | ||
} | ||
} | ||
|
||
pub(crate) fn fetch_add(&self, val: u64, order: Ordering) -> u64 { | ||
let mut lock = self.inner().lock(); | ||
let prev = *lock; | ||
*lock = prev + val; | ||
prev | ||
} | ||
|
||
fn inner(&self) -> &Mutex<u64> { | ||
self.cell.get(|| Mutex::new(self.init)) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ impl<T> OnceCell<T> { | |
/// 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 { | ||
pub(crate) fn get(&self, init: impl FnOnce() -> T) -> &T { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo on line 22: `intiailizing' -> initializing |
||
if !self.once.is_completed() { | ||
self.do_init(init); | ||
} | ||
|
@@ -41,7 +41,7 @@ impl<T> OnceCell<T> { | |
} | ||
|
||
#[cold] | ||
fn do_init(&self, init: fn() -> T) { | ||
fn do_init(&self, init: impl FnOnce() -> T) { | ||
let value_ptr = self.value.get() as *mut T; | ||
|
||
self.once.call_once(|| { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm experimenting with a way to have less code within the
cfg
macros. This should helprustfmt
apply to more code.