From b26c3e0ae9602bd0c27f8b1899f0db8ab027fd52 Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jeremy@system76.com>
Date: Sat, 13 Feb 2021 11:10:21 -0700
Subject: [PATCH] Make context name a RwLock

---
 src/arch/x86_64/interrupt/syscall.rs |  2 +-
 src/context/context.rs               |  8 ++++----
 src/context/switch.rs                |  2 +-
 src/lib.rs                           |  2 +-
 src/scheme/proc.rs                   |  2 +-
 src/scheme/sys/block.rs              |  2 +-
 src/scheme/sys/context.rs            |  2 +-
 src/scheme/sys/exe.rs                |  2 +-
 src/scheme/sys/iostat.rs             |  2 +-
 src/scheme/sys/syscall.rs            |  2 +-
 src/syscall/mod.rs                   | 10 +++++-----
 src/syscall/process.rs               | 10 +++++-----
 12 files changed, 23 insertions(+), 23 deletions(-)

diff --git a/src/arch/x86_64/interrupt/syscall.rs b/src/arch/x86_64/interrupt/syscall.rs
index 847d3afe..82beba84 100644
--- a/src/arch/x86_64/interrupt/syscall.rs
+++ b/src/arch/x86_64/interrupt/syscall.rs
@@ -95,7 +95,7 @@ interrupt_stack!(syscall, |stack| {
             let context = contexts.current();
             if let Some(current) = context {
                 let current = current.read();
-                let name = current.name.lock();
+                let name = current.name.read();
                 println!("Warning: Context {} used deprecated `int 0x80` construct", core::str::from_utf8(&name).unwrap_or("(invalid utf8)"));
             } else {
                 println!("Warning: Unknown context used deprecated `int 0x80` construct");
diff --git a/src/context/context.rs b/src/context/context.rs
index 1304613f..15163e57 100644
--- a/src/context/context.rs
+++ b/src/context/context.rs
@@ -5,7 +5,7 @@ use alloc::collections::VecDeque;
 use core::alloc::{GlobalAlloc, Layout};
 use core::cmp::Ordering;
 use core::mem;
-use spin::Mutex;
+use spin::{Mutex, RwLock};
 
 use crate::arch::{interrupt::InterruptStack, paging::PAGE_SIZE};
 use crate::common::unique::Unique;
@@ -120,7 +120,7 @@ pub struct ContextSnapshot {
 impl ContextSnapshot {
     //TODO: Should this accept &mut Context to ensure name/files will not change?
     pub fn new(context: &Context) -> Self {
-        let name = context.name.lock().clone();
+        let name = context.name.read().clone();
         let mut files = Vec::new();
         for descriptor_opt in context.files.lock().iter() {
             let description = if let Some(descriptor) = descriptor_opt {
@@ -230,7 +230,7 @@ pub struct Context {
     /// User grants
     pub grants: Arc<Mutex<UserGrants>>,
     /// The name of the context
-    pub name: Arc<Mutex<Box<[u8]>>>,
+    pub name: Arc<RwLock<Box<[u8]>>>,
     /// The current working directory
     pub cwd: Arc<Mutex<Vec<u8>>>,
     /// The open files in the scheme
@@ -287,7 +287,7 @@ impl Context {
             sigstack: None,
             tls: None,
             grants: Arc::new(Mutex::new(UserGrants::default())),
-            name: Arc::new(Mutex::new(Vec::new().into_boxed_slice())),
+            name: Arc::new(RwLock::new(Vec::new().into_boxed_slice())),
             cwd: Arc::new(Mutex::new(Vec::new())),
             files: Arc::new(Mutex::new(Vec::new())),
             actions: Arc::new(Mutex::new(vec![(
diff --git a/src/context/switch.rs b/src/context/switch.rs
index 634aff32..2423d55e 100644
--- a/src/context/switch.rs
+++ b/src/context/switch.rs
@@ -13,7 +13,7 @@ unsafe fn update(context: &mut Context, cpu_id: usize) {
     // Take ownership if not already owned
     if context.cpu_id == None {
         context.cpu_id = Some(cpu_id);
-        // println!("{}: take {} {}", cpu_id, context.id, ::core::str::from_utf8_unchecked(&context.name.lock()));
+        // println!("{}: take {} {}", cpu_id, context.id, ::core::str::from_utf8_unchecked(&context.name.read()));
     }
 
     // Restore from signal, must only be done from another context to avoid overwriting the stack!
diff --git a/src/lib.rs b/src/lib.rs
index aad157f4..a18fb271 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -279,7 +279,7 @@ pub extern fn ksignal(signal: usize) {
         let contexts = context::contexts();
         if let Some(context_lock) = contexts.current() {
             let context = context_lock.read();
-            info!("NAME {}", unsafe { ::core::str::from_utf8_unchecked(&context.name.lock()) });
+            info!("NAME {}", unsafe { ::core::str::from_utf8_unchecked(&context.name.read()) });
         }
     }
 
diff --git a/src/scheme/proc.rs b/src/scheme/proc.rs
index 68dbf81a..99d19e35 100644
--- a/src/scheme/proc.rs
+++ b/src/scheme/proc.rs
@@ -236,7 +236,7 @@ impl Scheme for ProcScheme {
             data = match operation {
                 Operation::Memory => OperationData::Memory(MemData::default()),
                 Operation::Trace => OperationData::Trace(TraceData::default()),
-                Operation::Static(_) => OperationData::Static(StaticData::new(target.name.lock().clone())),
+                Operation::Static(_) => OperationData::Static(StaticData::new(target.name.read().clone())),
                 _ => OperationData::Other,
             };
 
diff --git a/src/scheme/sys/block.rs b/src/scheme/sys/block.rs
index 21430166..ff9d0944 100644
--- a/src/scheme/sys/block.rs
+++ b/src/scheme/sys/block.rs
@@ -15,7 +15,7 @@ pub fn resource() -> Result<Vec<u8>> {
             let contexts = context::contexts();
             for (id, context_lock) in contexts.iter() {
                 let context = context_lock.read();
-                rows.push((*id, context.name.lock().clone(), context.status_reason));
+                rows.push((*id, context.name.read().clone(), context.status_reason));
             }
         }
 
diff --git a/src/scheme/sys/context.rs b/src/scheme/sys/context.rs
index 2c949602..df457c87 100644
--- a/src/scheme/sys/context.rs
+++ b/src/scheme/sys/context.rs
@@ -107,7 +107,7 @@ pub fn resource() -> Result<Vec<u8>> {
                 format!("{} B", memory)
             };
 
-            let name_bytes = context.name.lock();
+            let name_bytes = context.name.read();
             let name = str::from_utf8(&name_bytes).unwrap_or("");
 
             string.push_str(&format!("{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<8}{:<8}{}\n",
diff --git a/src/scheme/sys/exe.rs b/src/scheme/sys/exe.rs
index e4b1a6a6..69e2261f 100644
--- a/src/scheme/sys/exe.rs
+++ b/src/scheme/sys/exe.rs
@@ -8,7 +8,7 @@ pub fn resource() -> Result<Vec<u8>> {
         let contexts = context::contexts();
         let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
         let context = context_lock.read();
-        let name = context.name.lock();
+        let name = context.name.read();
         name.clone().into_vec()
     };
     Ok(name)
diff --git a/src/scheme/sys/iostat.rs b/src/scheme/sys/iostat.rs
index e0663873..3aac16ea 100644
--- a/src/scheme/sys/iostat.rs
+++ b/src/scheme/sys/iostat.rs
@@ -16,7 +16,7 @@ pub fn resource() -> Result<Vec<u8>> {
             let contexts = context::contexts();
             for (id, context_lock) in contexts.iter() {
                 let context = context_lock.read();
-                rows.push((*id, context.name.lock().clone(), context.files.lock().clone()));
+                rows.push((*id, context.name.read().clone(), context.files.lock().clone()));
             }
         }
 
diff --git a/src/scheme/sys/syscall.rs b/src/scheme/sys/syscall.rs
index 4d2eea4f..c458d4e7 100644
--- a/src/scheme/sys/syscall.rs
+++ b/src/scheme/sys/syscall.rs
@@ -16,7 +16,7 @@ pub fn resource() -> Result<Vec<u8>> {
             let contexts = context::contexts();
             for (id, context_lock) in contexts.iter() {
                 let context = context_lock.read();
-                rows.push((*id, context.name.lock().clone(), context.syscall.clone()));
+                rows.push((*id, context.name.read().clone(), context.syscall.clone()));
             }
         }
 
diff --git a/src/syscall/mod.rs b/src/syscall/mod.rs
index 779ac68f..eec73002 100644
--- a/src/syscall/mod.rs
+++ b/src/syscall/mod.rs
@@ -82,7 +82,7 @@ pub fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, bp: u
                                 let contexts = crate::context::contexts();
                                 let current = contexts.current().unwrap();
                                 let current = current.read();
-                                let name = current.name.lock();
+                                let name = current.name.read();
                                 println!("{:?} using deprecated fmap(...) call", core::str::from_utf8(&name));
                             }
                             file_op(a, fd, c, d)
@@ -92,7 +92,7 @@ pub fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, bp: u
                                 let contexts = crate::context::contexts();
                                 let current = contexts.current().unwrap();
                                 let current = current.read();
-                                let name = current.name.lock();
+                                let name = current.name.read();
                                 println!("{:?} using deprecated funmap(...) call", core::str::from_utf8(&name));
                             }
                             funmap_old(b)
@@ -208,7 +208,7 @@ pub fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, bp: u
         let contexts = crate::context::contexts();
         if let Some(context_lock) = contexts.current() {
             let context = context_lock.read();
-            let name_raw = context.name.lock();
+            let name_raw = context.name.read();
             let name = unsafe { core::str::from_utf8_unchecked(&name_raw) };
             if name.contains("redoxfs") {
                 if a == SYS_CLOCK_GETTIME || a == SYS_YIELD {
@@ -230,7 +230,7 @@ pub fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, bp: u
         let contexts = crate::context::contexts();
         if let Some(context_lock) = contexts.current() {
             let context = context_lock.read();
-            print!("{} ({}): ", unsafe { core::str::from_utf8_unchecked(&context.name.lock()) }, context.id.into());
+            print!("{} ({}): ", unsafe { core::str::from_utf8_unchecked(&context.name.read()) }, context.id.into());
         }
 
         println!("{}", debug::format_call(a, b, c, d, e, f));
@@ -265,7 +265,7 @@ pub fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, bp: u
         let contexts = crate::context::contexts();
         if let Some(context_lock) = contexts.current() {
             let context = context_lock.read();
-            print!("{} ({}): ", unsafe { core::str::from_utf8_unchecked(&context.name.lock()) }, context.id.into());
+            print!("{} ({}): ", unsafe { core::str::from_utf8_unchecked(&context.name.read()) }, context.id.into());
         }
 
         print!("{} = ", debug::format_call(a, b, c, d, e, f));
diff --git a/src/syscall/process.rs b/src/syscall/process.rs
index 9adad283..b034358d 100644
--- a/src/syscall/process.rs
+++ b/src/syscall/process.rs
@@ -5,7 +5,7 @@ use alloc::vec::Vec;
 use core::alloc::{GlobalAlloc, Layout};
 use core::ops::DerefMut;
 use core::{intrinsics, mem};
-use spin::Mutex;
+use spin::{RwLock, Mutex};
 
 use crate::context::file::FileDescriptor;
 use crate::context::{ContextId, WaitpidKey};
@@ -244,7 +244,7 @@ pub fn clone(flags: CloneFlags, stack_base: usize) -> Result<ContextId> {
             if flags.contains(CLONE_VM) {
                 name = Arc::clone(&context.name);
             } else {
-                name = Arc::new(Mutex::new(context.name.lock().clone()));
+                name = Arc::new(RwLock::new(context.name.read().clone()));
             }
 
             if flags.contains(CLONE_FS) {
@@ -608,7 +608,7 @@ fn empty(context: &mut context::Context, reaping: bool) {
         let grants = mem::replace(&mut *grants, UserGrants::default());
         for grant in grants.inner.into_iter() {
             if reaping {
-                println!("{}: {}: Grant should not exist: {:?}", context.id.into(), unsafe { ::core::str::from_utf8_unchecked(&context.name.lock()) }, grant);
+                println!("{}: {}: Grant should not exist: {:?}", context.id.into(), unsafe { ::core::str::from_utf8_unchecked(&context.name.read()) }, grant);
 
                 let mut new_table = unsafe { InactivePageTable::from_address(context.arch.get_page_utable()) };
                 let mut temporary_page = TemporaryPage::new(Page::containing_address(VirtualAddress::new(crate::USER_TMP_GRANT_OFFSET)));
@@ -652,7 +652,7 @@ fn fexec_noreturn(
                 ptrace::regs_for(&context).map(|s| s.is_singlestep()).unwrap_or(false)
             };
 
-            context.name = Arc::new(Mutex::new(name));
+            context.name = Arc::new(RwLock::new(name));
 
             empty(&mut context, false);
 
@@ -974,7 +974,7 @@ pub fn fexec_kernel(fd: FileHandle, args: Box<[Box<[u8]>]>, vars: Box<[Box<[u8]>
                 println!(
                     "{}: {}: fexec failed to execute {}: {}",
                     context.id.into(),
-                    unsafe { ::core::str::from_utf8_unchecked(&context.name.lock()) },
+                    unsafe { ::core::str::from_utf8_unchecked(&context.name.read()) },
                     fd.into(),
                     err
                 );
-- 
GitLab