mirror of
				https://github.com/bytecodealliance/wasm-micro-runtime.git
				synced 2025-10-31 05:11:19 +00:00 
			
		
		
		
	 6c846acc59
			
		
	
	
		6c846acc59
		
			
		
	
	
	
	
		
			
			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.
		
	
			
		
			
				
	
	
		
			66 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			66 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| /*
 | |
|  * 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;
 | |
| }
 |