work on shared memory accross threads

This commit is contained in:
Jeremy Soller 2016-09-16 17:51:27 -06:00
parent 0b2fd79816
commit 8ee9f1d7da
4 changed files with 111 additions and 56 deletions

View file

@ -3,7 +3,7 @@ use collections::Vec;
use arch;
use super::file::File;
use super::memory::Memory;
use super::memory::{Memory, SharedMemory};
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Status {
@ -26,9 +26,9 @@ pub struct Context {
/// Kernel stack
pub kstack: Option<Box<[u8]>>,
/// Executable image
pub image: Vec<Memory>,
pub image: Vec<SharedMemory>,
/// User heap
pub heap: Option<Memory>,
pub heap: Option<SharedMemory>,
/// User stack
pub stack: Option<Memory>,
/// The open files in the scheme

View file

@ -1,3 +1,6 @@
use alloc::arc::{Arc, Weak};
use spin::Mutex;
use arch::externs::memset;
use arch::paging::{ActivePageTable, InactivePageTable, Page, PageIter, VirtualAddress};
use arch::paging::entry::{self, EntryFlags};
@ -10,6 +13,35 @@ pub struct Memory {
flags: EntryFlags
}
#[derive(Debug)]
pub enum SharedMemory {
Owned(Arc<Mutex<Memory>>),
Borrowed(Weak<Mutex<Memory>>)
}
impl SharedMemory {
pub fn with<F, T>(&self, f: F) -> T where F: FnOnce(&mut Memory) -> T {
match *self {
SharedMemory::Owned(ref memory_lock) => {
let mut memory = memory_lock.lock();
f(&mut *memory)
},
SharedMemory::Borrowed(ref memory_weak) => {
let memory_lock = memory_weak.upgrade().expect("SharedMemory::Borrowed no longer valid");
let mut memory = memory_lock.lock();
f(&mut *memory)
}
}
}
pub fn borrow(&self) -> SharedMemory {
match *self {
SharedMemory::Owned(ref memory_lock) => SharedMemory::Borrowed(Arc::downgrade(memory_lock)),
SharedMemory::Borrowed(ref memory_lock) => SharedMemory::Borrowed(memory_lock.clone())
}
}
}
impl Memory {
pub fn new(start: VirtualAddress, size: usize, flags: EntryFlags, flush: bool, clear: bool) -> Self {
let mut memory = Memory {
@ -23,6 +55,10 @@ impl Memory {
memory
}
pub fn to_shared(self) -> SharedMemory {
SharedMemory::Owned(Arc::new(Mutex::new(self)))
}
pub fn start_address(&self) -> VirtualAddress {
self.start
}