Skip to content

Commit

Permalink
Fix/mc oneshot busywait (#720)
Browse files Browse the repository at this point in the history
* Store wakers in array instead of busy waiting

* Use single Arc and shared struct

* Use notify

* Add tokio as required dep

* Add tokio sync feature

* Impl deref for sender

* Add deref
  • Loading branch information
richarddavison authored Dec 3, 2024
1 parent 29fa0e5 commit da805d0
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions libs/llrt_utils/src/mc_oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ pub struct Shared<T> {
#[derive(Clone, Debug)]
pub struct Sender<T: Clone>(Arc<Shared<T>>);

impl<T: Clone> Deref for Sender<T> {
type Target = Arc<Shared<T>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<'js> Trace<'js> for Sender<Value<'js>> {
fn trace<'a>(&self, tracer: Tracer<'a, 'js>) {
if let Ok(v) = self.0.value.read() {
if let Ok(v) = self.value.read() {
if let Some(v) = v.as_ref() {
tracer.mark(v)
}
Expand All @@ -35,10 +42,10 @@ impl<'js> Trace<'js> for Sender<Value<'js>> {

impl<T: Clone> Sender<T> {
pub fn send(&self, value: T) {
if !self.0.is_sent.load(Ordering::Relaxed) {
self.0.value.write().unwrap().replace(value);
self.0.is_sent.store(true, Ordering::Release);
self.0.notify.notify_waiters();
if !self.is_sent.load(Ordering::Relaxed) {
self.value.write().unwrap().replace(value);
self.is_sent.store(true, Ordering::Release);
self.notify.notify_waiters();
}
}

Expand Down

0 comments on commit da805d0

Please sign in to comment.