work on shared memory accross threads
This commit is contained in:
parent
0b2fd79816
commit
8ee9f1d7da
4 changed files with 111 additions and 56 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue