Switch Context::grants to RwLock
This commit is contained in:
@@ -10,7 +10,7 @@ use core::{
|
||||
cmp::Ordering,
|
||||
mem,
|
||||
};
|
||||
use spin::{Mutex, RwLock};
|
||||
use spin::RwLock;
|
||||
|
||||
use crate::arch::{interrupt::InterruptStack, paging::PAGE_SIZE};
|
||||
use crate::common::unique::Unique;
|
||||
@@ -233,7 +233,7 @@ pub struct Context {
|
||||
/// User Thread local storage
|
||||
pub tls: Option<Tls>,
|
||||
/// User grants
|
||||
pub grants: Arc<Mutex<UserGrants>>,
|
||||
pub grants: Arc<RwLock<UserGrants>>,
|
||||
/// The name of the context
|
||||
pub name: Arc<RwLock<Box<str>>>,
|
||||
/// The current working directory
|
||||
@@ -291,7 +291,7 @@ impl Context {
|
||||
stack: None,
|
||||
sigstack: None,
|
||||
tls: None,
|
||||
grants: Arc::new(Mutex::new(UserGrants::default())),
|
||||
grants: Arc::new(RwLock::new(UserGrants::default())),
|
||||
name: Arc::new(RwLock::new(String::new().into_boxed_str())),
|
||||
cwd: Arc::new(RwLock::new(Vec::new())),
|
||||
files: Arc::new(RwLock::new(Vec::new())),
|
||||
|
||||
@@ -40,7 +40,7 @@ impl Scheme for MemoryScheme {
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let context = context_lock.read();
|
||||
|
||||
let mut grants = context.grants.lock();
|
||||
let mut grants = context.grants.write();
|
||||
|
||||
let region = grants.find_free_at(VirtualAddress::new(map.address), map.size, map.flags)?.round();
|
||||
|
||||
|
||||
@@ -90,7 +90,7 @@ pub fn resource() -> Result<Vec<u8>> {
|
||||
if let Some(ref sigstack) = context.sigstack {
|
||||
memory += sigstack.size();
|
||||
}
|
||||
for grant in context.grants.lock().iter() {
|
||||
for grant in context.grants.read().iter() {
|
||||
if grant.is_owned() {
|
||||
memory += grant.size();
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ impl UserInner {
|
||||
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)));
|
||||
|
||||
let mut grants = context.grants.lock();
|
||||
let mut grants = context.grants.write();
|
||||
|
||||
let from_address = round_down_pages(address);
|
||||
let offset = address - from_address;
|
||||
@@ -157,7 +157,7 @@ impl UserInner {
|
||||
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)));
|
||||
|
||||
let mut grants = context.grants.lock();
|
||||
let mut grants = context.grants.write();
|
||||
|
||||
if let Some(region) = grants.contains(VirtualAddress::new(address)).map(Region::from) {
|
||||
grants.take(®ion).unwrap().unmap_inactive(&mut new_table, &mut temporary_page);
|
||||
|
||||
@@ -80,7 +80,7 @@ pub fn inner_physmap(physical_address: usize, size: usize, flags: PhysmapFlags)
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let context = context_lock.read();
|
||||
|
||||
let mut grants = context.grants.lock();
|
||||
let mut grants = context.grants.write();
|
||||
|
||||
let from_address = (physical_address/4096) * 4096;
|
||||
let offset = physical_address - from_address;
|
||||
@@ -135,7 +135,7 @@ pub fn inner_physunmap(virtual_address: usize) -> Result<usize> {
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let context = context_lock.read();
|
||||
|
||||
let mut grants = context.grants.lock();
|
||||
let mut grants = context.grants.write();
|
||||
|
||||
if let Some(region) = grants.contains(VirtualAddress::new(virtual_address)).map(Region::from) {
|
||||
grants.take(®ion).unwrap().unmap();
|
||||
|
||||
@@ -455,7 +455,7 @@ pub fn funmap_old(virtual_address: usize) -> Result<usize> {
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let context = context_lock.read();
|
||||
|
||||
let mut grants = context.grants.lock();
|
||||
let mut grants = context.grants.write();
|
||||
|
||||
if let Some(region) = grants.contains(VirtualAddress::new(virtual_address)).map(Region::from) {
|
||||
let mut grant = grants.take(®ion).unwrap();
|
||||
@@ -503,7 +503,7 @@ pub fn funmap(virtual_address: usize, length: usize) -> Result<usize> {
|
||||
let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
|
||||
let context = context_lock.read();
|
||||
|
||||
let mut grants = context.grants.lock();
|
||||
let mut grants = context.grants.write();
|
||||
|
||||
let conflicting: Vec<Region> = grants.conflicts(requested).map(Region::from).collect();
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ use alloc::{
|
||||
use core::alloc::{GlobalAlloc, Layout};
|
||||
use core::ops::DerefMut;
|
||||
use core::{intrinsics, mem};
|
||||
use spin::{RwLock, Mutex};
|
||||
use spin::RwLock;
|
||||
|
||||
use crate::context::file::FileDescriptor;
|
||||
use crate::context::{ContextId, WaitpidKey};
|
||||
@@ -237,11 +237,11 @@ pub fn clone(flags: CloneFlags, stack_base: usize) -> Result<ContextId> {
|
||||
grants = Arc::clone(&context.grants);
|
||||
} else {
|
||||
let mut grants_set = UserGrants::default();
|
||||
for grant in context.grants.lock().iter() {
|
||||
for grant in context.grants.read().iter() {
|
||||
let start = VirtualAddress::new(grant.start_address().data() + crate::USER_TMP_GRANT_OFFSET - crate::USER_GRANT_OFFSET);
|
||||
grants_set.insert(grant.secret_clone(start));
|
||||
}
|
||||
grants = Arc::new(Mutex::new(grants_set));
|
||||
grants = Arc::new(RwLock::new(grants_set));
|
||||
}
|
||||
|
||||
if flags.contains(CLONE_VM) {
|
||||
@@ -288,7 +288,7 @@ pub fn clone(flags: CloneFlags, stack_base: usize) -> Result<ContextId> {
|
||||
|
||||
// If not cloning virtual memory, use fmap to re-obtain every grant where possible
|
||||
if !flags.contains(CLONE_VM) {
|
||||
let mut grants = grants.lock();
|
||||
let mut grants = grants.write();
|
||||
|
||||
let mut to_remove = BTreeSet::new();
|
||||
|
||||
@@ -420,7 +420,7 @@ pub fn clone(flags: CloneFlags, stack_base: usize) -> Result<ContextId> {
|
||||
context.image = image;
|
||||
|
||||
// Copy grant mapping
|
||||
if ! grants.lock().is_empty() {
|
||||
if ! grants.read().is_empty() {
|
||||
let frame = active_utable.p4()[crate::USER_GRANT_PML4].pointed_frame().expect("user grants not mapped");
|
||||
let flags = active_utable.p4()[crate::USER_GRANT_PML4].flags();
|
||||
active_utable.with(&mut new_utable, &mut temporary_upage, |mapper| {
|
||||
@@ -466,7 +466,7 @@ pub fn clone(flags: CloneFlags, stack_base: usize) -> Result<ContextId> {
|
||||
|
||||
// Move grants
|
||||
{
|
||||
let mut grants = grants.lock();
|
||||
let mut grants = grants.write();
|
||||
let old_grants = mem::replace(&mut *grants, UserGrants::default());
|
||||
|
||||
for mut grant in old_grants.inner.into_iter() {
|
||||
@@ -606,7 +606,7 @@ fn empty(context: &mut context::Context, reaping: bool) {
|
||||
drop(context.tls.take());
|
||||
}
|
||||
|
||||
let mut grants = context.grants.lock();
|
||||
let mut grants = context.grants.write();
|
||||
if Arc::strong_count(&context.grants) == 1 {
|
||||
let grants = mem::replace(&mut *grants, UserGrants::default());
|
||||
for grant in grants.inner.into_iter() {
|
||||
|
||||
Reference in New Issue
Block a user