diff --git a/src/context/mod.rs b/src/context/mod.rs index 6f6e7f9481..df07976e6a 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -104,7 +104,7 @@ pub fn init() { CONTEXTS .write() - .insert(ContextRef(Arc::downgrade(&context_lock))); + .insert(ContextRef(Arc::clone(&context_lock))); unsafe { let percpu = PercpuBlock::current(); @@ -140,11 +140,16 @@ pub fn current_pid() -> Result { Ok(current().read().pid) } -pub struct ContextRef(pub Weak>); +pub struct ContextRef(pub Arc>); +impl ContextRef { + pub fn upgrade(&self) -> Option>> { + Some(Arc::clone(&self.0)) + } +} impl Ord for ContextRef { fn cmp(&self, other: &Self) -> core::cmp::Ordering { - Ord::cmp(&self.0.as_ptr(), &other.0.as_ptr()) + Ord::cmp(&Arc::as_ptr(&self.0), &Arc::as_ptr(&other.0)) } } impl PartialOrd for ContextRef { @@ -175,7 +180,7 @@ pub fn spawn( CONTEXTS .write() - .insert(ContextRef(Arc::downgrade(&context_lock))); + .insert(ContextRef(Arc::clone(&context_lock))); process.write().threads.push(Arc::downgrade(&context_lock)); { diff --git a/src/context/switch.rs b/src/context/switch.rs index 6754a50a48..d37e8ea4ef 100644 --- a/src/context/switch.rs +++ b/src/context/switch.rs @@ -131,7 +131,7 @@ pub fn switch() -> SwitchResult { for next_context_lock in contexts // Include all contexts with IDs greater than the current... .range(( - Bound::Excluded(ContextRef(Arc::downgrade(&prev_context_lock))), + Bound::Excluded(ContextRef(Arc::clone(&prev_context_lock))), Bound::Unbounded, )) .chain( @@ -139,10 +139,10 @@ pub fn switch() -> SwitchResult { // ... and all contexts with IDs less than the current... .range(( Bound::Unbounded, - Bound::Excluded(ContextRef(Arc::downgrade(&prev_context_lock))), + Bound::Excluded(ContextRef(Arc::clone(&prev_context_lock))), )), ) - .filter_map(|r| r.0.upgrade()) + .filter_map(|r| r.upgrade()) .chain(Some(Arc::clone(&idle_context))) // ... but not the current context, which is already locked { diff --git a/src/scheme/sys/block.rs b/src/scheme/sys/block.rs index cd281eb99a..59ffdc554f 100644 --- a/src/scheme/sys/block.rs +++ b/src/scheme/sys/block.rs @@ -10,7 +10,7 @@ pub fn resource() -> Result> { let mut rows = Vec::new(); { let contexts = context::contexts(); - for context_lock in contexts.iter().filter_map(|r| r.0.upgrade()) { + for context_lock in contexts.iter().filter_map(|r| r.upgrade()) { let context = context_lock.read(); rows.push(( context.pid.get(), diff --git a/src/scheme/sys/context.rs b/src/scheme/sys/context.rs index 3d5fdc2441..00172ab9e9 100644 --- a/src/scheme/sys/context.rs +++ b/src/scheme/sys/context.rs @@ -27,7 +27,7 @@ pub fn resource() -> Result> { ); { let contexts = context::contexts(); - for context_ref in contexts.iter().filter_map(|r| r.0.upgrade()) { + for context_ref in contexts.iter().filter_map(|r| r.upgrade()) { let context = context_ref.read(); let mut stat_string = String::new(); diff --git a/src/scheme/sys/iostat.rs b/src/scheme/sys/iostat.rs index e98a63c15d..aa5d810996 100644 --- a/src/scheme/sys/iostat.rs +++ b/src/scheme/sys/iostat.rs @@ -9,7 +9,7 @@ pub fn resource() -> Result> { let mut rows = Vec::new(); { let contexts = context::contexts(); - for context_ref in contexts.iter().filter_map(|r| r.0.upgrade()) { + for context_ref in contexts.iter().filter_map(|r| r.upgrade()) { let context = context_ref.read(); rows.push(( context.pid, diff --git a/src/scheme/sys/syscall.rs b/src/scheme/sys/syscall.rs index 52f7d8ce2f..cffbda26d4 100644 --- a/src/scheme/sys/syscall.rs +++ b/src/scheme/sys/syscall.rs @@ -10,7 +10,7 @@ pub fn resource() -> Result> { let mut rows = Vec::new(); { let contexts = context::contexts(); - for context_ref in contexts.iter().filter_map(|r| r.0.upgrade()) { + for context_ref in contexts.iter().filter_map(|r| r.upgrade()) { let context = context_ref.read(); rows.push(( context.pid.get(),