1 use crate::loom::sync::atomic::{AtomicBool, Ordering}; 2 use crate::loom::sync::{Barrier, Mutex}; 3 use crate::runtime::dump::Dump; 4 use crate::runtime::scheduler::multi_thread_alt::Handle; 5 use crate::sync::notify::Notify; 6 7 /// Tracing status of the worker. 8 pub(super) struct TraceStatus { 9 pub(super) trace_requested: AtomicBool, 10 pub(super) trace_start: Barrier, 11 pub(super) trace_end: Barrier, 12 pub(super) result_ready: Notify, 13 pub(super) trace_result: Mutex<Option<Dump>>, 14 } 15 16 impl TraceStatus { new(remotes_len: usize) -> Self17 pub(super) fn new(remotes_len: usize) -> Self { 18 Self { 19 trace_requested: AtomicBool::new(false), 20 trace_start: Barrier::new(remotes_len), 21 trace_end: Barrier::new(remotes_len), 22 result_ready: Notify::new(), 23 trace_result: Mutex::new(None), 24 } 25 } 26 trace_requested(&self) -> bool27 pub(super) fn trace_requested(&self) -> bool { 28 self.trace_requested.load(Ordering::Relaxed) 29 } 30 start_trace_request(&self, handle: &Handle)31 pub(super) async fn start_trace_request(&self, handle: &Handle) { 32 while self 33 .trace_requested 34 .compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed) 35 .is_err() 36 { 37 handle.notify_all(); 38 crate::task::yield_now().await; 39 } 40 } 41 stash_result(&self, dump: Dump)42 pub(super) fn stash_result(&self, dump: Dump) { 43 let _ = self.trace_result.lock().insert(dump); 44 self.result_ready.notify_one(); 45 } 46 take_result(&self) -> Option<Dump>47 pub(super) fn take_result(&self) -> Option<Dump> { 48 self.trace_result.lock().take() 49 } 50 end_trace_request(&self, handle: &Handle)51 pub(super) async fn end_trace_request(&self, handle: &Handle) { 52 while self 53 .trace_requested 54 .compare_exchange(true, false, Ordering::Acquire, Ordering::Relaxed) 55 .is_err() 56 { 57 handle.notify_all(); 58 crate::task::yield_now().await; 59 } 60 } 61 } 62