Struct wasmtime::SharedMemory

source ·
pub struct SharedMemory(_, _);
Expand description

A constructor for externally-created shared memory.

The threads proposal adds the concept of “shared memory” to WebAssembly. This is much the same as a Wasm linear memory (i.e., Memory), but can be used concurrently by multiple agents. Because these agents may execute in different threads, SharedMemory must be thread-safe.

When the threads proposal is enabled, there are multiple ways to construct shared memory:

  1. for imported shared memory, e.g., (import "env" "memory" (memory 1 1 shared)), the user must supply a SharedMemory with the externally-created memory as an import to the instance–e.g., shared_memory.into().
  2. for private or exported shared memory, e.g., (export "env" "memory" (memory 1 1 shared)), Wasmtime will create the memory internally during instantiation–access using Instance::get_shared_memory().

Examples

let mut config = Config::new();
config.wasm_threads(true);
let engine = Engine::new(&config)?;
let mut store = Store::new(&engine, ());

let shared_memory = SharedMemory::new(&engine, MemoryType::shared(1, 2))?;
let module = Module::new(&engine, r#"(module (memory (import "" "") 1 2 shared))"#)?;
let instance = Instance::new(&mut store, &module, &[shared_memory.into()])?;
// ...

Implementations§

Construct a SharedMemory by providing both the minimum and maximum number of 64K-sized pages. This call allocates the necessary pages on the system.

Return the type of the shared memory.

Returns the size, in WebAssembly pages, of this wasm memory.

Returns the byte length of this memory.

The returned value will be a multiple of the wasm page size, 64k.

For more information and examples see the documentation on the Memory type.

Return access to the available portion of the shared memory.

The slice returned represents the region of accessible memory at the time that this function was called. The contents of the returned slice will reflect concurrent modifications happening on other threads.

Safety

The returned slice is valid for the entire duration of the lifetime of this instance of SharedMemory. The base pointer of a shared memory does not change. This SharedMemory may grow further after this function has been called, but the slice returned will not grow.

Concurrent modifications may be happening to the data returned on other threads. The UnsafeCell<u8> represents that safe access to the contents of the slice is not possible through normal loads and stores.

The memory returned must be accessed safely through the Atomic* types in the std::sync::atomic module. Casting to those types must currently be done unsafely.

Grows this WebAssembly memory by delta pages.

This will attempt to add delta more pages of memory on to the end of this Memory instance. If successful this may relocate the memory and cause Memory::data_ptr to return a new value. Additionally any unsafely constructed slices into this memory may no longer be valid.

On success returns the number of pages this memory previously had before the growth succeeded.

Errors

Returns an error if memory could not be grown, for example if it exceeds the maximum limits of this memory. A ResourceLimiter is another example of preventing a memory to grow.

Equivalent of the WebAssembly memory.atomic.notify instruction for this shared memory.

This method allows embedders to notify threads blocked on the specified addr, an index into wasm linear memory. Threads could include wasm threads blocked on a memory.atomic.wait* instruction or embedder threads blocked on SharedMemory::atomic_wait32, for example.

The count argument is the number of threads to wake up.

This function returns the number of threads awoken.

Errors

This function will return an error if addr is not within bounds or not aligned to a 4-byte boundary.

Equivalent of the WebAssembly memory.atomic.wait32 instruction for this shared memory.

This method allows embedders to block the current thread until notified via the memory.atomic.notify instruction or the SharedMemory::atomic_notify method, enabling synchronization with the wasm guest as desired.

The expected argument is the expected 32-bit value to be stored at the byte address addr specified. The addr specified is an index into this linear memory.

The optional timeout argument is the point in time after which the calling thread is guaranteed to be woken up. Blocking will not occur past this point.

This function returns one of three possible values:

  • WaitResult::Ok - this function, loaded the value at addr, found it was equal to expected, and then blocked (all as one atomic operation). The thread was then awoken with a memory.atomic.notify instruction or the SharedMemory::atomic_notify method.
  • WaitResult::Mismatch - the value at addr was loaded but was not equal to expected so the thread did not block and immediately returned.
  • WaitResult::TimedOut - all the steps of Ok happened, except this thread was woken up due to a timeout.

This function will not return due to spurious wakeups.

Errors

This function will return an error if addr is not within bounds or not aligned to a 4-byte boundary.

Equivalent of the WebAssembly memory.atomic.wait64 instruction for this shared memory.

For more information see SharedMemory::atomic_wait32.

Errors

Returns the same error as SharedMemory::atomic_wait32 except that the specified address must be 8-byte aligned instead of 4-byte aligned.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.