Add comments explaining the unsafe impls and simplify the code, too

This commit is contained in:
Mark McCaskey
2019-09-17 18:35:12 -07:00
parent 83c3909b00
commit 9c205e05a2
6 changed files with 15 additions and 13 deletions

View File

@ -54,6 +54,7 @@ pub struct LocalBacking {
pub(crate) internals: Internals, pub(crate) internals: Internals,
} }
// Manually implemented because LocalBacking contains raw pointers directly
unsafe impl Send for LocalBacking {} unsafe impl Send for LocalBacking {}
impl LocalBacking { impl LocalBacking {
@ -463,6 +464,7 @@ pub struct ImportBacking {
pub(crate) vm_globals: BoxedMap<ImportedGlobalIndex, *mut vm::LocalGlobal>, pub(crate) vm_globals: BoxedMap<ImportedGlobalIndex, *mut vm::LocalGlobal>,
} }
// manually implemented because ImportBacking contains raw pointers directly
unsafe impl Send for ImportBacking {} unsafe impl Send for ImportBacking {}
impl ImportBacking { impl ImportBacking {

View File

@ -11,6 +11,7 @@ pub enum Context {
Internal, Internal,
} }
// Manually implemented because context contains a raw pointer to Ctx
unsafe impl Send for Context {} unsafe impl Send for Context {}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -28,6 +29,7 @@ pub enum Export {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct FuncPointer(*const vm::Func); pub struct FuncPointer(*const vm::Func);
// Manually implemented because FuncPointer contains a raw pointer to Ctx
unsafe impl Send for FuncPointer {} unsafe impl Send for FuncPointer {}
impl FuncPointer { impl FuncPointer {

View File

@ -107,17 +107,6 @@ impl ImportObject {
} }
} }
/*pub fn get_namespace(
&self,
namespace: &str,
) -> Option<
MutexGuardRef<HashMap<String, Box<dyn LikeNamespace + Send>>, &(dyn LikeNamespace + Send)>,
> {
MutexGuardRef::new(self.map.lock().unwrap())
.try_map(|mg| mg.get(namespace).map(|ns| &ns.as_ref()).ok_or(()))
.ok()
}*/
/// Apply a function on the namespace if it exists /// Apply a function on the namespace if it exists
/// If your function can fail, consider using `maybe_with_namespace` /// If your function can fail, consider using `maybe_with_namespace`
pub fn with_namespace<Func, InnerRet>(&self, namespace: &str, f: Func) -> Option<InnerRet> pub fn with_namespace<Func, InnerRet>(&self, namespace: &str, f: Func) -> Option<InnerRet>

View File

@ -25,6 +25,7 @@ pub(crate) struct InstanceInner {
pub(crate) vmctx: *mut vm::Ctx, pub(crate) vmctx: *mut vm::Ctx,
} }
// manually implemented because InstanceInner contains a raw pointer to Ctx
unsafe impl Send for InstanceInner {} unsafe impl Send for InstanceInner {}
impl Drop for InstanceInner { impl Drop for InstanceInner {

View File

@ -213,7 +213,8 @@ struct UnsharedMemoryInternal {
local: Cell<vm::LocalMemory>, local: Cell<vm::LocalMemory>,
} }
unsafe impl Send for UnsharedMemoryInternal {} // Manually implemented because UnsharedMemoryInternal uses `Cell` and is used in an Arc;
// this is safe because the lock for storage can be used to protect (seems like a weak reason: PLEASE REVIEW!)
unsafe impl Sync for UnsharedMemoryInternal {} unsafe impl Sync for UnsharedMemoryInternal {}
impl UnsharedMemory { impl UnsharedMemory {
@ -291,7 +292,8 @@ pub struct SharedMemoryInternal {
lock: Mutex<()>, lock: Mutex<()>,
} }
unsafe impl Send for SharedMemoryInternal {} // Manually implemented because SharedMemoryInternal uses `Cell` and is used in Arc;
// this is safe because of `lock`; accesing `local` without locking `lock` is not safe (Maybe we could put the lock on Local then?)
unsafe impl Sync for SharedMemoryInternal {} unsafe impl Sync for SharedMemoryInternal {}
impl SharedMemory { impl SharedMemory {

View File

@ -474,6 +474,7 @@ pub struct ImportedFunc {
pub vmctx: *mut Ctx, pub vmctx: *mut Ctx,
} }
// manually implemented because ImportedFunc contains raw pointers directly; `Func` is marked Send (But `Ctx` actually isn't! (TODO: review this, shouldn't `Ctx` be Send?))
unsafe impl Send for ImportedFunc {} unsafe impl Send for ImportedFunc {}
impl ImportedFunc { impl ImportedFunc {
@ -503,6 +504,7 @@ pub struct LocalTable {
pub table: *mut (), pub table: *mut (),
} }
// manually implemented because LocalTable contains raw pointers directly
unsafe impl Send for LocalTable {} unsafe impl Send for LocalTable {}
impl LocalTable { impl LocalTable {
@ -534,6 +536,9 @@ pub struct LocalMemory {
pub memory: *mut (), pub memory: *mut (),
} }
// manually implemented because LocalMemory contains raw pointers
unsafe impl Send for LocalMemory {}
impl LocalMemory { impl LocalMemory {
#[allow(clippy::erasing_op)] // TODO #[allow(clippy::erasing_op)] // TODO
pub fn offset_base() -> u8 { pub fn offset_base() -> u8 {
@ -584,6 +589,7 @@ pub struct Anyfunc {
pub sig_id: SigId, pub sig_id: SigId,
} }
// manually implemented because Anyfunc contains raw pointers directly
unsafe impl Send for Anyfunc {} unsafe impl Send for Anyfunc {}
impl Anyfunc { impl Anyfunc {