Implement module instance context APIs (#2436)
Introduce module instance context APIs which can set one or more contexts created
by the embedder for a wasm module instance:
```C
wasm_runtime_create_context_key
wasm_runtime_destroy_context_key
wasm_runtime_set_context
wasm_runtime_set_context_spread
wasm_runtime_get_context
```
And make libc-wasi use it and set wasi context as the first context bound to the wasm
module instance.
Also add samples.
Refer to https://github.com/bytecodealliance/wasm-micro-runtime/issues/2460.
This commit is contained in:
65
samples/inst-context-threads/wasm-apps/testapp.c
Normal file
65
samples/inst-context-threads/wasm-apps/testapp.c
Normal file
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (C) 2023 Midokura Japan KK. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
void
|
||||
set_context(int32_t n) __attribute__((import_module("env")))
|
||||
__attribute__((import_name("set_context")));
|
||||
|
||||
int32_t
|
||||
get_context() __attribute__((import_module("env")))
|
||||
__attribute__((import_name("get_context")));
|
||||
|
||||
void *
|
||||
start(void *vp)
|
||||
{
|
||||
int32_t v;
|
||||
|
||||
printf("thread started\n");
|
||||
|
||||
printf("confirming the initial state on thread\n");
|
||||
v = get_context();
|
||||
assert(v == -1);
|
||||
|
||||
printf("setting the context on thread\n");
|
||||
set_context(1234);
|
||||
|
||||
printf("confirming the context on thread\n");
|
||||
v = get_context();
|
||||
assert(v == 1234);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
pthread_t t1;
|
||||
int32_t v;
|
||||
int ret;
|
||||
|
||||
printf("confirming the initial state on main\n");
|
||||
v = get_context();
|
||||
assert(v == -1);
|
||||
|
||||
printf("creating a thread\n");
|
||||
ret = pthread_create(&t1, NULL, start, NULL);
|
||||
assert(ret == 0);
|
||||
void *val;
|
||||
ret = pthread_join(t1, &val);
|
||||
assert(ret == 0);
|
||||
printf("joined the thread\n");
|
||||
|
||||
printf("confirming the context propagated from the thread on main\n");
|
||||
v = get_context();
|
||||
assert(v == 1234);
|
||||
|
||||
printf("success\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user