Disconnects Store state fields from Compiler (#1761)
* Moves CodeMemory, VMInterrupts and SignatureRegistry from Compiler * CompiledModule holds CodeMemory and GdbJitImageRegistration * Store keeps track of its JIT code * Makes "jit_int.rs" stuff Send+Sync * Adds the threads example.
This commit is contained in:
@@ -71,7 +71,7 @@ int main(int argc, const char* argv[]) {
|
||||
printf("Instantiating module...\n");
|
||||
wasm_instance_t* instance = NULL;
|
||||
wasm_trap_t *trap = NULL;
|
||||
error = wasmtime_instance_new(module, NULL, 0, &instance, &trap);
|
||||
error = wasmtime_instance_new(store, module, NULL, 0, &instance, &trap);
|
||||
if (error != NULL || trap != NULL)
|
||||
exit_with_error("failed to instantiate", error, trap);
|
||||
wasm_module_delete(module);
|
||||
|
||||
@@ -17,8 +17,8 @@ fn main() -> Result<()> {
|
||||
// debugged in GDB.
|
||||
let engine = Engine::new(Config::new().debug_info(true));
|
||||
let store = Store::new(&engine);
|
||||
let module = Module::from_file(&store, "target/wasm32-unknown-unknown/debug/fib.wasm")?;
|
||||
let instance = Instance::new(&module, &[])?;
|
||||
let module = Module::from_file(&engine, "target/wasm32-unknown-unknown/debug/fib.wasm")?;
|
||||
let instance = Instance::new(&store, &module, &[])?;
|
||||
|
||||
// Invoke `fib` export
|
||||
let fib = instance
|
||||
|
||||
@@ -65,7 +65,7 @@ int main() {
|
||||
wasm_byte_vec_delete(&wasm);
|
||||
wasm_trap_t *trap = NULL;
|
||||
wasm_instance_t *instance = NULL;
|
||||
error = wasmtime_instance_new(module, NULL, 0, &instance, &trap);
|
||||
error = wasmtime_instance_new(store, module, NULL, 0, &instance, &trap);
|
||||
if (instance == NULL)
|
||||
exit_with_error("failed to instantiate", error, trap);
|
||||
|
||||
|
||||
@@ -11,8 +11,8 @@ fn main() -> Result<()> {
|
||||
// `Module` which is attached to a `Store` cache. After we've got that we
|
||||
// can instantiate it.
|
||||
let store = Store::default();
|
||||
let module = Module::from_file(&store, "examples/gcd.wat")?;
|
||||
let instance = Instance::new(&module, &[])?;
|
||||
let module = Module::from_file(store.engine(), "examples/gcd.wat")?;
|
||||
let instance = Instance::new(&store, &module, &[])?;
|
||||
|
||||
// Invoke `gcd` export
|
||||
let gcd = instance
|
||||
|
||||
@@ -87,7 +87,7 @@ int main() {
|
||||
wasm_trap_t *trap = NULL;
|
||||
wasm_instance_t *instance = NULL;
|
||||
const wasm_extern_t *imports[] = { wasm_func_as_extern(hello) };
|
||||
error = wasmtime_instance_new(module, imports, 1, &instance, &trap);
|
||||
error = wasmtime_instance_new(store, module, imports, 1, &instance, &trap);
|
||||
if (instance == NULL)
|
||||
exit_with_error("failed to instantiate", error, trap);
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ fn main() -> Result<()> {
|
||||
|
||||
// Compile the wasm binary into an in-memory instance of a `Module`.
|
||||
println!("Compiling module...");
|
||||
let module = Module::from_file(&store, "examples/hello.wat")?;
|
||||
let module = Module::from_file(store.engine(), "examples/hello.wat")?;
|
||||
|
||||
// Here we handle the imports of the module, which in this case is our
|
||||
// `HelloCallback` type and its associated implementation of `Callback.
|
||||
@@ -30,7 +30,7 @@ fn main() -> Result<()> {
|
||||
// Note that this is where the wasm `start` function, if any, would run.
|
||||
println!("Instantiating module...");
|
||||
let imports = [hello_func.into()];
|
||||
let instance = Instance::new(&module, &imports)?;
|
||||
let instance = Instance::new(&store, &module, &imports)?;
|
||||
|
||||
// Next we poke around a bit to extract the `run` function from the module.
|
||||
println!("Extracting export...");
|
||||
|
||||
@@ -93,7 +93,7 @@ int main() {
|
||||
wasm_byte_vec_delete(&wasm);
|
||||
if (error != NULL)
|
||||
exit_with_error("failed to compile module", error, NULL);
|
||||
error = wasmtime_instance_new(module, NULL, 0, &instance, &trap);
|
||||
error = wasmtime_instance_new(store, module, NULL, 0, &instance, &trap);
|
||||
if (instance == NULL)
|
||||
exit_with_error("failed to instantiate", error, trap);
|
||||
|
||||
|
||||
@@ -14,8 +14,8 @@ fn main() -> Result<()> {
|
||||
let interrupt_handle = store.interrupt_handle()?;
|
||||
|
||||
// Compile and instantiate a small example with an infinite loop.
|
||||
let module = Module::from_file(&store, "examples/interrupt.wat")?;
|
||||
let instance = Instance::new(&module, &[])?;
|
||||
let module = Module::from_file(&engine, "examples/interrupt.wat")?;
|
||||
let instance = Instance::new(&store, &module, &[])?;
|
||||
let run = instance
|
||||
.get_func("run")
|
||||
.ok_or(anyhow::format_err!("failed to find `run` function export"))?
|
||||
|
||||
@@ -7,7 +7,8 @@ use wasmtime::*;
|
||||
use wasmtime_wasi::{Wasi, WasiCtx};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let store = Store::default();
|
||||
let engine = Engine::default();
|
||||
let store = Store::new(&engine);
|
||||
|
||||
// First set up our linker which is going to be linking modules together. We
|
||||
// want our linker to have wasi available, so we set that up here as well.
|
||||
@@ -16,8 +17,8 @@ fn main() -> Result<()> {
|
||||
wasi.add_to_linker(&mut linker)?;
|
||||
|
||||
// Load and compile our two modules
|
||||
let linking1 = Module::from_file(&store, "examples/linking1.wat")?;
|
||||
let linking2 = Module::from_file(&store, "examples/linking2.wat")?;
|
||||
let linking1 = Module::from_file(&engine, "examples/linking1.wat")?;
|
||||
let linking2 = Module::from_file(&engine, "examples/linking2.wat")?;
|
||||
|
||||
// Instantiate our first module which only uses WASI, then register that
|
||||
// instance with the linker since the next linking will use it.
|
||||
|
||||
@@ -167,7 +167,7 @@ int main(int argc, const char* argv[]) {
|
||||
printf("Instantiating module...\n");
|
||||
wasm_instance_t* instance = NULL;
|
||||
wasm_trap_t *trap = NULL;
|
||||
error = wasmtime_instance_new(module, NULL, 0, &instance, &trap);
|
||||
error = wasmtime_instance_new(store, module, NULL, 0, &instance, &trap);
|
||||
if (!instance)
|
||||
exit_with_error("failed to instantiate", error, trap);
|
||||
|
||||
|
||||
@@ -13,8 +13,8 @@ fn main() -> Result<()> {
|
||||
// Create our `Store` context and then compile a module and create an
|
||||
// instance from the compiled module all in one go.
|
||||
let wasmtime_store = Store::default();
|
||||
let module = Module::from_file(&wasmtime_store, "examples/memory.wat")?;
|
||||
let instance = Instance::new(&module, &[])?;
|
||||
let module = Module::from_file(wasmtime_store.engine(), "examples/memory.wat")?;
|
||||
let instance = Instance::new(&wasmtime_store, &module, &[])?;
|
||||
|
||||
// Load up our exports from the instance
|
||||
let memory = instance
|
||||
|
||||
@@ -115,7 +115,7 @@ int main(int argc, const char* argv[]) {
|
||||
const wasm_extern_t* imports[] = {wasm_func_as_extern(callback_func)};
|
||||
wasm_instance_t* instance = NULL;
|
||||
wasm_trap_t* trap = NULL;
|
||||
error = wasmtime_instance_new(module, imports, 1, &instance, &trap);
|
||||
error = wasmtime_instance_new(store, module, imports, 1, &instance, &trap);
|
||||
if (!instance)
|
||||
exit_with_error("failed to instantiate", error, trap);
|
||||
|
||||
|
||||
@@ -12,11 +12,12 @@ use wasmtime::*;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
println!("Initializing...");
|
||||
let store = Store::default();
|
||||
let engine = Engine::default();
|
||||
let store = Store::new(&engine);
|
||||
|
||||
// Compile.
|
||||
println!("Compiling module...");
|
||||
let module = Module::from_file(&store, "examples/multi.wat")?;
|
||||
let module = Module::from_file(&engine, "examples/multi.wat")?;
|
||||
|
||||
// Create external print functions.
|
||||
println!("Creating callback...");
|
||||
@@ -35,7 +36,7 @@ fn main() -> Result<()> {
|
||||
|
||||
// Instantiate.
|
||||
println!("Instantiating module...");
|
||||
let instance = Instance::new(&module, &[callback_func.into()])?;
|
||||
let instance = Instance::new(&store, &module, &[callback_func.into()])?;
|
||||
|
||||
// Extract exports.
|
||||
println!("Extracting export...");
|
||||
|
||||
184
examples/threads.c
Normal file
184
examples/threads.c
Normal file
@@ -0,0 +1,184 @@
|
||||
#ifndef _WIN32
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#include <wasm.h>
|
||||
#include <wasmtime.h>
|
||||
|
||||
#define own
|
||||
|
||||
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap);
|
||||
|
||||
const int N_THREADS = 10;
|
||||
const int N_REPS = 3;
|
||||
|
||||
// A function to be called from Wasm code.
|
||||
own wasm_trap_t* callback(const wasm_val_t args[], wasm_val_t results[]) {
|
||||
assert(args[0].kind == WASM_I32);
|
||||
printf("> Thread %d running\n", args[0].of.i32);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
typedef struct {
|
||||
wasm_engine_t* engine;
|
||||
wasm_shared_module_t* module;
|
||||
int id;
|
||||
} thread_args;
|
||||
|
||||
void* run(void* args_abs) {
|
||||
thread_args* args = (thread_args*)args_abs;
|
||||
|
||||
// Rereate store and module.
|
||||
own wasm_store_t* store = wasm_store_new(args->engine);
|
||||
own wasm_module_t* module = wasm_module_obtain(store, args->module);
|
||||
|
||||
// Run the example N times.
|
||||
for (int i = 0; i < N_REPS; ++i) {
|
||||
usleep(100000);
|
||||
|
||||
// Create imports.
|
||||
own wasm_functype_t* func_type = wasm_functype_new_1_0(wasm_valtype_new_i32());
|
||||
own wasm_func_t* func = wasm_func_new(store, func_type, callback);
|
||||
wasm_functype_delete(func_type);
|
||||
|
||||
wasm_val_t val = {.kind = WASM_I32, .of = {.i32 = (int32_t)args->id}};
|
||||
own wasm_globaltype_t* global_type =
|
||||
wasm_globaltype_new(wasm_valtype_new_i32(), WASM_CONST);
|
||||
own wasm_global_t* global = wasm_global_new(store, global_type, &val);
|
||||
wasm_globaltype_delete(global_type);
|
||||
|
||||
// Instantiate.
|
||||
const wasm_extern_t* imports[] = {
|
||||
wasm_func_as_extern(func), wasm_global_as_extern(global),
|
||||
};
|
||||
own wasm_instance_t* instance =
|
||||
wasm_instance_new(store, module, imports, NULL);
|
||||
if (!instance) {
|
||||
printf("> Error instantiating module!\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
wasm_func_delete(func);
|
||||
wasm_global_delete(global);
|
||||
|
||||
// Extract export.
|
||||
own wasm_extern_vec_t exports;
|
||||
wasm_instance_exports(instance, &exports);
|
||||
if (exports.size == 0) {
|
||||
printf("> Error accessing exports!\n");
|
||||
return NULL;
|
||||
}
|
||||
const wasm_func_t *run_func = wasm_extern_as_func(exports.data[0]);
|
||||
if (run_func == NULL) {
|
||||
printf("> Error accessing export!\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
wasm_instance_delete(instance);
|
||||
|
||||
// Call.
|
||||
if (wasm_func_call(run_func, NULL, NULL)) {
|
||||
printf("> Error calling function!\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
wasm_extern_vec_delete(&exports);
|
||||
}
|
||||
|
||||
wasm_module_delete(module);
|
||||
wasm_store_delete(store);
|
||||
|
||||
free(args_abs);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
// Initialize.
|
||||
wasm_engine_t* engine = wasm_engine_new();
|
||||
|
||||
// Load our input file to parse it next
|
||||
FILE* file = fopen("examples/threads.wat", "r");
|
||||
if (!file) {
|
||||
printf("> Error loading file!\n");
|
||||
return 1;
|
||||
}
|
||||
fseek(file, 0L, SEEK_END);
|
||||
size_t file_size = ftell(file);
|
||||
fseek(file, 0L, SEEK_SET);
|
||||
wasm_byte_vec_t wat;
|
||||
wasm_byte_vec_new_uninitialized(&wat, file_size);
|
||||
if (fread(wat.data, file_size, 1, file) != 1) {
|
||||
printf("> Error loading module!\n");
|
||||
return 1;
|
||||
}
|
||||
fclose(file);
|
||||
|
||||
// Parse the wat into the binary wasm format
|
||||
wasm_byte_vec_t binary;
|
||||
wasmtime_error_t *error = wasmtime_wat2wasm(&wat, &binary);
|
||||
if (error != NULL)
|
||||
exit_with_error("failed to parse wat", error, NULL);
|
||||
wasm_byte_vec_delete(&wat);
|
||||
|
||||
// Compile and share.
|
||||
own wasm_store_t* store = wasm_store_new(engine);
|
||||
own wasm_module_t* module = wasm_module_new(store, &binary);
|
||||
if (!module) {
|
||||
printf("> Error compiling module!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
wasm_byte_vec_delete(&binary);
|
||||
|
||||
own wasm_shared_module_t* shared = wasm_module_share(module);
|
||||
|
||||
wasm_module_delete(module);
|
||||
wasm_store_delete(store);
|
||||
|
||||
// Spawn threads.
|
||||
pthread_t threads[N_THREADS];
|
||||
for (int i = 0; i < N_THREADS; i++) {
|
||||
thread_args* args = malloc(sizeof(thread_args));
|
||||
args->id = i;
|
||||
args->engine = engine;
|
||||
args->module = shared;
|
||||
printf("Initializing thread %d...\n", i);
|
||||
pthread_create(&threads[i], NULL, &run, args);
|
||||
}
|
||||
|
||||
for (int i = 0; i < N_THREADS; i++) {
|
||||
printf("Waiting for thread: %d\n", i);
|
||||
pthread_join(threads[i], NULL);
|
||||
}
|
||||
|
||||
wasm_shared_module_delete(shared);
|
||||
wasm_engine_delete(engine);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) {
|
||||
fprintf(stderr, "error: %s\n", message);
|
||||
wasm_byte_vec_t error_message;
|
||||
if (error != NULL) {
|
||||
wasmtime_error_message(error, &error_message);
|
||||
} else {
|
||||
wasm_trap_message(trap, &error_message);
|
||||
}
|
||||
fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
|
||||
wasm_byte_vec_delete(&error_message);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
#else
|
||||
// TODO implement example for Windows
|
||||
int main(int argc, const char *argv[]) {
|
||||
return 0;
|
||||
}
|
||||
#endif // _WIN32
|
||||
70
examples/threads.rs
Normal file
70
examples/threads.rs
Normal file
@@ -0,0 +1,70 @@
|
||||
// You can execute this example with `cargo run --example threads`
|
||||
|
||||
use anyhow::{format_err, Result};
|
||||
use std::thread;
|
||||
use std::time;
|
||||
use wasmtime::*;
|
||||
|
||||
const N_THREADS: i32 = 10;
|
||||
const N_REPS: i32 = 3;
|
||||
|
||||
fn print_message(_: Caller<'_>, args: &[Val], _: &mut [Val]) -> Result<(), Trap> {
|
||||
println!("> Thread {} is running", args[0].unwrap_i32());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run(engine: &Engine, module: Module, id: i32) -> Result<()> {
|
||||
let store = Store::new(&engine);
|
||||
|
||||
// Create external print functions.
|
||||
println!("Creating callback...");
|
||||
let callback_type = FuncType::new(Box::new([ValType::I32]), Box::new([]));
|
||||
let callback_func = Func::new(&store, callback_type, print_message);
|
||||
|
||||
let id_type = GlobalType::new(ValType::I32, Mutability::Const);
|
||||
let id_global = Global::new(&store, id_type, Val::I32(id))?;
|
||||
|
||||
// Instantiate.
|
||||
println!("Instantiating module...");
|
||||
let instance = Instance::new(&store, &module, &[callback_func.into(), id_global.into()])?;
|
||||
|
||||
// Extract exports.
|
||||
println!("Extracting export...");
|
||||
let g = instance
|
||||
.get_func("run")
|
||||
.ok_or(format_err!("failed to find export `eun`"))?;
|
||||
|
||||
for _ in 0..N_REPS {
|
||||
thread::sleep(time::Duration::from_millis(100));
|
||||
// Call `$run`.
|
||||
drop(g.call(&[])?);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
println!("Initializing...");
|
||||
let engine = Engine::default();
|
||||
|
||||
// Compile.
|
||||
println!("Compiling module...");
|
||||
let module = Module::from_file(&engine, "examples/threads.wat")?;
|
||||
|
||||
let mut children = Vec::new();
|
||||
for id in 0..N_THREADS {
|
||||
let engine = engine.clone();
|
||||
let module = module.clone();
|
||||
children.push(thread::spawn(move || {
|
||||
run(&engine, module, id).expect("Success");
|
||||
}));
|
||||
}
|
||||
|
||||
for (i, child) in children.into_iter().enumerate() {
|
||||
if let Err(_) = child.join() {
|
||||
println!("Thread #{} errors", i);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
5
examples/threads.wat
Normal file
5
examples/threads.wat
Normal file
@@ -0,0 +1,5 @@
|
||||
(module
|
||||
(func $message (import "" "hello") (param i32))
|
||||
(global $id (import "" "id") i32)
|
||||
(func (export "run") (call $message (global.get $id)))
|
||||
)
|
||||
@@ -18,7 +18,7 @@ fn main() -> Result<()> {
|
||||
wasi.add_to_linker(&mut linker)?;
|
||||
|
||||
// Instantiate our module with the imports we've created, and run it.
|
||||
let module = Module::from_file(&store, "target/wasm32-wasi/debug/wasi.wasm")?;
|
||||
let module = Module::from_file(store.engine(), "target/wasm32-wasi/debug/wasi.wasm")?;
|
||||
linker.module("", &module)?;
|
||||
linker.get_default("")?.get0::<()>()?()?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user