Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented an asyncify based implementation of asynchronous threading #3710

Merged
merged 72 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 68 commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
00b41ab
Implemented the draft asyncify implementation for deep sleeping - a.k…
john-sharratt Mar 21, 2023
1d90f40
Merge remote-tracking branch 'origin/master' into asynchronous-threading
john-sharratt Mar 21, 2023
beceafe
cargo clippy --fix
john-sharratt Mar 21, 2023
4958ad4
More clippy fixes
john-sharratt Mar 21, 2023
69bb9ff
Fixed the linting errors
john-sharratt Mar 22, 2023
8dbdac2
Fixed some more clippy linting issues
john-sharratt Mar 22, 2023
0eaed79
Added an opt-out on asynchronous threading
john-sharratt Mar 22, 2023
65a850c
Updated the version of spin used for heapless which had been yanked
john-sharratt Mar 22, 2023
ccd852b
Refactored parts of asynchronous threading to fix snapshot tests
john-sharratt Mar 22, 2023
a8cad0c
Fixes a bug which causes libc to thrash the futex_wake_all because it…
john-sharratt Mar 22, 2023
acbdb03
Also fixed the futex_wake once call
john-sharratt Mar 22, 2023
235b57e
Merge branch 'futex-fix' into async-threads-refactor
john-sharratt Mar 23, 2023
9e9cdef
Refactoring of the async threading implementation to fix the snapshot…
john-sharratt Mar 23, 2023
262e049
Merge remote-tracking branch 'origin/master' into asynchronous-threading
john-sharratt Mar 29, 2023
5ffeb68
Fixes for clippy lints
john-sharratt Mar 30, 2023
89d3d3e
More linting fixes
john-sharratt Mar 30, 2023
0ed9ced
More linting fixes
john-sharratt Mar 30, 2023
525c189
Updated all the tests so that there are normal tests without async th…
john-sharratt Mar 30, 2023
748f6ae
Added a comment to retrigger the integration tests
john-sharratt Mar 30, 2023
9737c7d
Merge branch 'master' into asynchronous-threading
john-sharratt Mar 30, 2023
65d63fd
Merge branch 'asynchronous-threading' of github.com:wasmerio/wasmer i…
john-sharratt Mar 30, 2023
d5dece7
Flips from opt-out to opt-in for asynchronous threading
john-sharratt Mar 30, 2023
435bd7f
Merge branch 'master' into asynchronous-threading
john-sharratt Mar 30, 2023
66012f2
Merge branch 'asynchronous-threading' of github.com:wasmerio/wasmer i…
john-sharratt Mar 30, 2023
83e1159
Removed a redundant function(s)
john-sharratt Mar 31, 2023
1855c7f
Merge remote-tracking branch 'origin/master' into asynchronous-threading
john-sharratt Apr 1, 2023
f25c4ce
Merge branch 'master' into asynchronous-threading
john-sharratt Apr 2, 2023
e774697
Merge remote-tracking branch 'origin/master' into asynchronous-threading
john-sharratt Apr 7, 2023
6350cb1
Fixed compile errors after merge
john-sharratt Apr 7, 2023
eb8cfec
Updated the unit tests and fixed many of them
john-sharratt Apr 7, 2023
9867a77
Rewrote the async web server snapshot tests
john-sharratt Apr 7, 2023
c7ad86e
Fixed the signalling system which was causing spurious intr exits
john-sharratt Apr 7, 2023
1118937
Fixed some linting errors
john-sharratt Apr 7, 2023
37ef38e
Merge branch 'master' into asynchronous-threading
john-sharratt Apr 7, 2023
8c20bf6
Merge branch 'master' into asynchronous-threading
john-sharratt Apr 7, 2023
dc7b04f
Merge branch 'master' into asynchronous-threading
john-sharratt Apr 7, 2023
c4d500a
Lint fixes on some of the tests
john-sharratt Apr 7, 2023
4367920
More linting fixes
john-sharratt Apr 7, 2023
49f5360
Merge branch 'master' into asynchronous-threading
john-sharratt Apr 7, 2023
85ad071
Refactored async threading so that it works with the browser
john-sharratt Apr 8, 2023
19c4c62
Fixed the integration tests
john-sharratt Apr 14, 2023
276e0c2
Disable async threading for the browser for now
john-sharratt Apr 14, 2023
bf77986
Merge branch 'master' into asynchronous-threading
john-sharratt Apr 14, 2023
5899c06
Removed some redundant code
john-sharratt Apr 14, 2023
38425b1
Fixed the linting issues
john-sharratt Apr 14, 2023
82eab8c
Renamed some things and simplified the worker.js
john-sharratt Apr 14, 2023
d6d2e25
Converted WasiEnv so that it no longer has the unsafe send on it
john-sharratt Apr 14, 2023
c6cd14b
Fixed some lint errors
john-sharratt Apr 14, 2023
4762c58
Fixed some linting issues
john-sharratt Apr 14, 2023
f7a93b9
More fixes after removing the cloning of inner
john-sharratt Apr 14, 2023
2fe30db
Hidden the method and fixes some regression issues
john-sharratt Apr 14, 2023
cdbc9ce
Fixed deep sleep for the futux waits
john-sharratt Apr 14, 2023
7d8b70e
Fixed a race condition on futex and hidden the instance methods a bit…
john-sharratt Apr 14, 2023
31ce0ab
Fixed a race condition on the futex
john-sharratt Apr 15, 2023
44916a3
Disabled asynchronous threading for spawned threads on the browser fo…
john-sharratt Apr 15, 2023
e9a3a7a
Merge branch 'master' into asynchronous-threading
john-sharratt Apr 15, 2023
13b4d72
Removed the error enhancements so that the API is backwards compatible
john-sharratt Apr 15, 2023
d2ca673
Fixed the linting issues
john-sharratt Apr 15, 2023
4b7ddf1
Moved some of the rewind logic to its own file
john-sharratt Apr 16, 2023
65cda35
Merge branch 'master' into asynchronous-threading
john-sharratt Apr 16, 2023
6789bc9
The tests now run for longer and with a higher timeout so they don't …
john-sharratt Apr 16, 2023
d48f401
Merge branch 'master' into asynchronous-threading
john-sharratt Apr 18, 2023
da12bf1
Fixed linting issues
john-sharratt Apr 19, 2023
6c7e47b
Removed the store_id functions that were added
john-sharratt Apr 19, 2023
59c8a45
Changed the store reference
john-sharratt Apr 19, 2023
a064b9d
Made two of the tests more deterministic
john-sharratt Apr 19, 2023
499eb49
Reverted some unused changes
john-sharratt Apr 19, 2023
a5c03b4
Changes to address comments
john-sharratt Apr 19, 2023
83970e3
Fixed the merge conflicts after merging from master
john-sharratt May 11, 2023
0757f35
Merge branch 'master' into asynchronous-threading
john-sharratt May 13, 2023
f659e44
Fixed review comments
john-sharratt May 13, 2023
cd5bc2a
Fixed a compile error after the backport
john-sharratt May 13, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions lib/api/src/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Memory {

/// Creates a view into the memory that then allows for
/// read and write
pub fn view<'a>(&self, store: &'a impl AsStoreRef) -> MemoryView<'a> {
pub fn view<'a>(&self, store: &'a (impl AsStoreRef + ?Sized)) -> MemoryView<'a> {
MemoryView::new(self, store)
}

Expand Down Expand Up @@ -119,15 +119,6 @@ impl Memory {
self.0.grow(store, delta)
}

/// Copies the memory to a new store and returns a memory reference to it
pub fn copy_to_store(
john-sharratt marked this conversation as resolved.
Show resolved Hide resolved
&self,
store: &impl AsStoreRef,
new_store: &mut impl AsStoreMut,
) -> Result<Self, MemoryError> {
Ok(Self(self.0.copy_to_store(store, new_store)?))
}

pub(crate) fn from_vm_extern(store: &mut impl AsStoreMut, vm_extern: VMExternMemory) -> Self {
Self(memory_impl::Memory::from_vm_extern(store, vm_extern))
}
Expand All @@ -143,34 +134,44 @@ impl Memory {
}

/// Attempts to clone this memory (if its clonable) in a new store
pub fn clone_in_store(
/// (cloned memory will be shared between those that clone it)
pub fn share_in_store(
&self,
store: &impl AsStoreRef,
new_store: &mut impl AsStoreMut,
) -> Option<Self> {
) -> Result<Self, MemoryError> {
if !self.ty(store).shared {
john-sharratt marked this conversation as resolved.
Show resolved Hide resolved
// We should only be able to duplicate in a new store if the memory is shared
return None;
return Err(MemoryError::InvalidMemory {
reason: "memory is not a shared memory type".to_string(),
});
}
self.0
.try_clone(&store)
.map(|new_memory| Self::new_from_existing(new_store, new_memory))
.ok_or_else(|| MemoryError::Generic("memory is not clonable".to_string()))
}

/// Attempts to duplicate this memory (if its clonable) in a new store
pub fn duplicate_in_store(
john-sharratt marked this conversation as resolved.
Show resolved Hide resolved
/// (copied memory)
pub fn copy_to_store(
&self,
store: &impl AsStoreRef,
new_store: &mut impl AsStoreMut,
) -> Option<Self> {
) -> Result<Self, MemoryError> {
if !self.ty(store).shared {
// We should only be able to duplicate in a new store if the memory is shared
return None;
return Err(MemoryError::InvalidMemory {
reason: "memory is not a shared memory type".to_string(),
});
}
self.0
.try_clone(&store)
.and_then(|mut memory| memory.duplicate().ok())
.and_then(|mut memory| memory.copy().ok())
.map(|new_memory| Self::new_from_existing(new_store, new_memory.into()))
.ok_or_else(|| {
MemoryError::Generic("memory is not clonable or could not be copied".to_string())
})
}

/// To `VMExtern`.
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/externals/memory_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::sys::externals::memory_view as memory_view_impl;
pub struct MemoryView<'a>(pub(crate) memory_view_impl::MemoryView<'a>);

impl<'a> MemoryView<'a> {
pub(crate) fn new(memory: &Memory, store: &'a impl AsStoreRef) -> Self {
pub(crate) fn new(memory: &Memory, store: &'a (impl AsStoreRef + ?Sized)) -> Self {
MemoryView(memory_view_impl::MemoryView::new(&memory.0, store))
}

Expand Down
34 changes: 1 addition & 33 deletions lib/api/src/js/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tracing::warn;

use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasmer_types::{Pages, WASM_PAGE_SIZE};
use wasmer_types::Pages;

use super::memory_view::MemoryView;

Expand Down Expand Up @@ -136,33 +136,6 @@ impl Memory {
Ok(Pages(new_pages))
}

pub fn copy_to_store(
&self,
store: &impl AsStoreRef,
new_store: &mut impl AsStoreMut,
) -> Result<Self, MemoryError> {
// Create the new memory using the parameters of the existing memory
let view = self.view(store);
let ty = self.ty(store);
let amount = view.data_size() as usize;

let new_memory = Self::new(new_store, ty)?;
let new_view_size = new_memory.view(&new_store).data_size() as usize;
if amount > new_view_size {
let delta = amount - new_view_size;
let pages = ((delta - 1) / WASM_PAGE_SIZE) + 1;
new_memory.grow(new_store, Pages(pages as u32))?;
}
let new_view = new_memory.view(&new_store);

// Copy the bytes
view.copy_to_memory(amount as u64, &new_view)
.map_err(|err| MemoryError::Generic(err.to_string()))?;

// Return the new memory
Ok(new_memory)
}

pub(crate) fn from_vm_extern(_store: &mut impl AsStoreMut, internal: VMMemory) -> Self {
Self { handle: internal }
}
Expand All @@ -174,11 +147,6 @@ impl Memory {
pub fn is_from_store(&self, _store: &impl AsStoreRef) -> bool {
true
}

#[allow(unused)]
pub fn duplicate(&mut self, _store: &impl AsStoreRef) -> Result<VMMemory, MemoryError> {
self.handle.duplicate()
}
}

impl std::cmp::PartialEq for Memory {
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/js/externals/memory_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct MemoryView<'a> {
}

impl<'a> MemoryView<'a> {
pub(crate) fn new(memory: &Memory, _store: &'a impl AsStoreRef) -> Self {
pub(crate) fn new(memory: &Memory, _store: &'a (impl AsStoreRef + ?Sized)) -> Self {
Self::new_raw(&memory.handle.memory)
}

Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/js/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl VMMemory {
}

/// Copies this memory to a new memory
pub fn duplicate(&mut self) -> Result<VMMemory, wasmer_types::MemoryError> {
pub fn copy(&mut self) -> Result<VMMemory, wasmer_types::MemoryError> {
let new_memory = crate::js::externals::memory::Memory::js_memory_from_type(&self.ty)?;

let src = crate::js::externals::memory_view::MemoryView::new_raw(&self.memory);
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub type WasmPtr64<T> = WasmPtr<T, Memory64>;
#[repr(transparent)]
pub struct WasmPtr<T, M: MemorySize = Memory32> {
offset: M::Offset,
_phantom: PhantomData<*mut T>,
john-sharratt marked this conversation as resolved.
Show resolved Hide resolved
_phantom: PhantomData<T>,
}

impl<T, M: MemorySize> WasmPtr<T, M> {
Expand Down
1 change: 1 addition & 0 deletions lib/api/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ impl fmt::Debug for Store {
}

/// A temporary handle to a [`Store`].
#[derive(Debug)]
pub struct StoreRef<'a> {
pub(crate) inner: &'a StoreInner,
}
Expand Down
34 changes: 0 additions & 34 deletions lib/api/src/sys/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ impl Memory {
self.handle.get(store.as_store_ref().objects()).ty()
}

/// Creates a view into the memory that then allows for
/// read and write
pub fn view<'a>(&self, store: &'a impl AsStoreRef) -> MemoryView<'a> {
MemoryView::new(self, store)
}

pub fn grow<IntoPages>(
&self,
store: &mut impl AsStoreMut,
Expand All @@ -56,34 +50,6 @@ impl Memory {
self.handle.get_mut(store.objects_mut()).grow(delta.into())
}

pub fn copy_to_store(
&self,
store: &impl AsStoreRef,
new_store: &mut impl AsStoreMut,
) -> Result<Self, MemoryError> {
// Create the new memory using the parameters of the existing memory
let view = self.view(store);
let ty = self.ty(store);
let amount = view.data_size() as usize;

let new_memory = Self::new(new_store, ty)?;
let mut new_view = new_memory.view(&new_store);
let new_view_size = new_view.data_size() as usize;
if amount > new_view_size {
let delta = amount - new_view_size;
let pages = ((delta - 1) / wasmer_types::WASM_PAGE_SIZE) + 1;
new_memory.grow(new_store, Pages(pages as u32))?;
new_view = new_memory.view(&new_store);
}

// Copy the bytes
view.copy_to_memory(amount as u64, &new_view)
.map_err(|err| MemoryError::Generic(err.to_string()))?;

// Return the new memory
Ok(new_memory)
}

pub(crate) fn from_vm_extern(store: &impl AsStoreRef, vm_extern: VMExternMemory) -> Self {
Self {
handle: unsafe {
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/sys/externals/memory_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct MemoryView<'a> {
}

impl<'a> MemoryView<'a> {
pub(crate) fn new(memory: &Memory, store: &'a impl AsStoreRef) -> Self {
pub(crate) fn new(memory: &Memory, store: &'a (impl AsStoreRef + ?Sized)) -> Self {
let size = memory.handle.get(store.as_store_ref().objects()).size();

let definition = memory.handle.get(store.as_store_ref().objects()).vmmemory();
Expand Down
4 changes: 3 additions & 1 deletion lib/api/src/sys/tunables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub use wasmer_compiler::BaseTunables;
#[cfg(test)]
mod tests {
use super::*;
#[allow(unused)]
use crate::sys::NativeEngineExt;
use crate::TableType;
use std::cell::UnsafeCell;
Expand Down Expand Up @@ -123,7 +124,7 @@ mod tests {
None
}

fn duplicate(&mut self) -> Result<Box<dyn LinearMemory + 'static>, MemoryError> {
fn copy(&mut self) -> Result<Box<dyn LinearMemory + 'static>, MemoryError> {
let mem = self.mem.clone();
Ok(Box::new(Self {
memory_definition: Some(UnsafeCell::new(VMMemoryDefinition {
Expand Down Expand Up @@ -243,6 +244,7 @@ mod tests {
let compiler = Cranelift::default();

let tunables = TinyTunables {};
#[allow(deprecated)]
let mut engine = Engine::new(compiler.into(), Default::default(), Default::default());
engine.set_tunables(tunables);
let mut store = Store::new(engine);
Expand Down
Loading