Implement Send for everything except Memory

This commit is contained in:
Mark McCaskey
2019-09-17 11:45:13 -07:00
parent 17a0e78cef
commit 9e9343878d
6 changed files with 41 additions and 21 deletions

View File

@@ -25,6 +25,8 @@ pub enum Export {
Global(Global),
}
unsafe impl Send for Export {}
#[derive(Debug, Clone)]
pub struct FuncPointer(*const vm::Func);

View File

@@ -4,11 +4,14 @@ use crate::{
types::{GlobalDescriptor, Type, Value},
vm,
};
use std::{cell::RefCell, fmt, sync::Arc};
use std::{
fmt,
sync::{Arc, Mutex},
};
pub struct Global {
desc: GlobalDescriptor,
storage: Arc<RefCell<vm::LocalGlobal>>,
storage: Arc<Mutex<vm::LocalGlobal>>,
}
impl Global {
@@ -56,7 +59,7 @@ impl Global {
Self {
desc,
storage: Arc::new(RefCell::new(local_global)),
storage: Arc::new(Mutex::new(local_global)),
}
}
@@ -83,7 +86,8 @@ impl Global {
Value::V128(x) => x,
},
};
*self.storage.borrow_mut() = local_global;
let mut storage = self.storage.lock().unwrap();
*storage = local_global;
} else {
panic!("Wrong type for setting this global")
}
@@ -94,7 +98,8 @@ impl Global {
/// Get the value held by this global.
pub fn get(&self) -> Value {
let data = self.storage.borrow().data;
let storage = self.storage.lock().unwrap();
let data = storage.data;
match self.desc.ty {
Type::I32 => Value::I32(data as i32),
@@ -105,8 +110,10 @@ impl Global {
}
}
// TODO: think about this and if this should now be unsafe
pub(crate) fn vm_local_global(&mut self) -> *mut vm::LocalGlobal {
&mut *self.storage.borrow_mut()
let mut storage = self.storage.lock().unwrap();
&mut *storage
}
}

View File

@@ -207,11 +207,9 @@ impl Extend<(String, String, Export)> for ImportObject {
}
pub struct Namespace {
map: HashMap<String, Box<dyn IsExport>>,
map: HashMap<String, Box<dyn IsExport + Send>>,
}
unsafe impl Send for Namespace {}
impl Namespace {
pub fn new() -> Self {
Self {
@@ -219,10 +217,10 @@ impl Namespace {
}
}
pub fn insert<S, E>(&mut self, name: S, export: E) -> Option<Box<dyn IsExport>>
pub fn insert<S, E>(&mut self, name: S, export: E) -> Option<Box<dyn IsExport + Send>>
where
S: Into<String>,
E: IsExport + 'static,
E: IsExport + Send + 'static,
{
self.map.insert(name.into(), Box::new(export))
}

View File

@@ -5,7 +5,10 @@ use crate::{
types::{ElementType, TableDescriptor},
vm,
};
use std::{cell::RefCell, fmt, ptr, rc::Rc};
use std::{
fmt, ptr,
sync::{Arc, Mutex},
};
mod anyfunc;
@@ -25,7 +28,7 @@ pub enum TableStorage {
pub struct Table {
desc: TableDescriptor,
storage: Rc<RefCell<(TableStorage, vm::LocalTable)>>,
storage: Arc<Mutex<(TableStorage, vm::LocalTable)>>,
}
impl Table {
@@ -71,7 +74,7 @@ impl Table {
Ok(Self {
desc,
storage: Rc::new(RefCell::new((storage, local))),
storage: Arc::new(Mutex::new((storage, local))),
})
}
@@ -82,7 +85,8 @@ impl Table {
/// Set the element at index.
pub fn set(&self, index: u32, element: Element) -> Result<(), ()> {
match &mut *self.storage.borrow_mut() {
let mut storage = self.storage.lock().unwrap();
match &mut *storage {
(TableStorage::Anyfunc(ref mut anyfunc_table), _) => {
match element {
Element::Anyfunc(anyfunc) => anyfunc_table.set(index, anyfunc),
@@ -96,14 +100,16 @@ impl Table {
where
F: FnOnce(&mut [vm::Anyfunc]) -> R,
{
match &mut *self.storage.borrow_mut() {
let mut storage = self.storage.lock().unwrap();
match &mut *storage {
(TableStorage::Anyfunc(ref mut anyfunc_table), _) => f(anyfunc_table.internal_buffer()),
}
}
/// The current size of this table.
pub fn size(&self) -> u32 {
match &*self.storage.borrow() {
let storage = self.storage.lock().unwrap();
match &*storage {
(TableStorage::Anyfunc(ref anyfunc_table), _) => anyfunc_table.current_size(),
}
}
@@ -114,7 +120,8 @@ impl Table {
return Ok(self.size());
}
match &mut *self.storage.borrow_mut() {
let mut storage = self.storage.lock().unwrap();
match &mut *storage {
(TableStorage::Anyfunc(ref mut anyfunc_table), ref mut local) => anyfunc_table
.grow(delta, local)
.ok_or(GrowError::TableGrowError),
@@ -122,7 +129,8 @@ impl Table {
}
pub fn vm_local_table(&mut self) -> *mut vm::LocalTable {
&mut self.storage.borrow_mut().1
let mut storage = self.storage.lock().unwrap();
&mut storage.1
}
}
@@ -136,7 +144,7 @@ impl Clone for Table {
fn clone(&self) -> Self {
Self {
desc: self.desc,
storage: Rc::clone(&self.storage),
storage: Arc::clone(&self.storage),
}
}
}

View File

@@ -165,7 +165,8 @@ pub struct Func<'a, Args = (), Rets = (), Inner: Kind = Wasm> {
_phantom: PhantomData<(&'a (), Args, Rets)>,
}
unsafe impl<'a, Args, Rets> Send for Func<'a, Args, Rets> {}
unsafe impl<'a, Args, Rets> Send for Func<'a, Args, Rets, Wasm> {}
unsafe impl<'a, Args, Rets> Send for Func<'a, Args, Rets, Host> {}
impl<'a, Args, Rets> Func<'a, Args, Rets, Wasm>
where

View File

@@ -501,6 +501,8 @@ pub struct LocalTable {
pub table: *mut (),
}
unsafe impl Send for LocalTable {}
impl LocalTable {
#[allow(clippy::erasing_op)] // TODO
pub fn offset_base() -> u8 {
@@ -580,6 +582,8 @@ pub struct Anyfunc {
pub sig_id: SigId,
}
unsafe impl Send for Anyfunc {}
impl Anyfunc {
pub fn null() -> Self {
Self {