MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / main

Function main

examples/async.cc:165–291  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

163} // namespace
164
165int main() {
166 // A thread that will async perform host function calls.
167 std::thread printer_thread([]() {
168 int32_t value_to_print = printer_state.get_value_to_print();
169 std::cout << "received value to print!" << std::endl;
170 std::this_thread::sleep_for(std::chrono::milliseconds(500));
171 std::cout << "printing: " << value_to_print << std::endl;
172 std::this_thread::sleep_for(std::chrono::milliseconds(500));
173 std::cout << "signaling that value is printed" << std::endl;
174 printer_state.set_print_success();
175 });
176
177 handle<wasmtime_error_t, wasmtime_error_delete> error;
178
179 auto engine = create_engine();
180 auto store = create_store(engine.get());
181 // This pointer is unowned.
182 auto *context = wasmtime_store_context(store.get());
183 // Configure the store to periodically yield control
184 wasmtime_context_set_fuel(context, 100000);
185 wasmtime_context_fuel_async_yield_interval(context, /*interval=*/10000);
186
187 auto compiled_module =
188 compile_wat_module_from_file(engine.get(), "examples/async.wat");
189
190 auto linker = create_linker(engine.get());
191 static std::string host_module_name = "host";
192 static std::string host_func_name = "print";
193
194 // Declare our async host function's signature and definition.
195 wasm_valtype_vec_t arg_types;
196 wasm_valtype_vec_t result_types;
197 wasm_valtype_vec_new_uninitialized(&arg_types, 1);
198 arg_types.data[0] = wasm_valtype_new_i32();
199 wasm_valtype_vec_new_empty(&result_types);
200 handle<wasm_functype_t, wasm_functype_delete> functype{
201 wasm_functype_new(&arg_types, &result_types)};
202
203 error.reset(wasmtime_linker_define_async_func(
204 linker.get(), host_module_name.data(), host_module_name.size(),
205 host_func_name.data(), host_func_name.size(), functype.get(),
206 [](void *, wasmtime_caller_t *, const wasmtime_val_t *args, size_t,
207 wasmtime_val_t *, size_t, wasm_trap_t **trap_ret,
208 wasmtime_async_continuation_t *continuation_ret) {
209 std::cout << "invoking async host function" << std::endl;
210 printer_state.set_value_to_print(args[0].of.i32);
211
212 continuation_ret->callback = &poll_print_finished_state;
213 continuation_ret->env = new async_call_env{trap_ret};
214 continuation_ret->finalizer = [](void *env) {
215 std::cout << "deleting async_call_env" << std::endl;
216 delete static_cast<async_call_env *>(env);
217 };
218 },
219 /*env=*/nullptr, /*finalizer=*/nullptr));
220 if (error) {
221 exit_with_error("failed to define host function", error.get(), nullptr);
222 }

Callers

nothing calls this directly

Calls 15

create_storeFunction · 0.85
wasmtime_store_contextFunction · 0.85
create_linkerFunction · 0.85
wasm_functype_newFunction · 0.85
assertFunction · 0.85

Tested by

no test coverage detected