Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement atomics opcodes for interpreter #344

Merged
merged 1 commit into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/iwasm/common/wasm_shared_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ wasm_runtime_atomic_wait(WASMModuleInstanceCommon *module, void *address,
return is_timeout ? 2 : 0;
}

uint8
uint32
wasm_runtime_atomic_notify(WASMModuleInstanceCommon *module,
void *address, uint32 count)
{
Expand Down
2 changes: 1 addition & 1 deletion core/iwasm/common/wasm_shared_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ uint32
wasm_runtime_atomic_wait(WASMModuleInstanceCommon *module, void *address,
uint64 expect, int64 timeout, bool wait64);

uint8
uint32
wasm_runtime_atomic_notify(WASMModuleInstanceCommon *module,
void *address, uint32 count);

Expand Down
436 changes: 436 additions & 0 deletions core/iwasm/interpreter/wasm_interp_classic.c

Large diffs are not rendered by default.

434 changes: 434 additions & 0 deletions core/iwasm/interpreter/wasm_interp_fast.c

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions core/iwasm/interpreter/wasm_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -6596,6 +6596,9 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
error_buf_size)) {
goto fail;
}
#if WASM_ENABLE_FAST_INTERP != 0
emit_uint32(loader_ctx, mem_offset);
#endif
}
switch (opcode) {
case WASM_OP_ATOMIC_NOTIFY:
Expand Down
3 changes: 3 additions & 0 deletions core/iwasm/interpreter/wasm_mini_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -5348,6 +5348,9 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
CHECK_MEMORY();
read_leb_uint32(p, p_end, align); /* align */
read_leb_uint32(p, p_end, mem_offset); /* offset */
#if WASM_ENABLE_FAST_INTERP != 0
emit_uint32(loader_ctx, mem_offset);
#endif
}
switch (opcode) {
case WASM_OP_ATOMIC_NOTIFY:
Expand Down
10 changes: 10 additions & 0 deletions core/iwasm/interpreter/wasm_opcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,15 @@ typedef enum WASMAtomicEXTOpcode {
}
#endif

/* Opcode prefix controlled by features */
#if WASM_ENABLE_SHARED_MEMORY != 0
#define DEF_ATOMIC_PREFIX_HANDLE(_name) \
_name[WASM_OP_ATOMIC_PREFIX] = \
HANDLE_OPCODE (WASM_OP_ATOMIC_PREFIX); /* 0xfe */
#else
#define DEF_ATOMIC_PREFIX_HANDLE(_name)
#endif

/*
* Macro used to generate computed goto tables for the C interpreter.
*/
Expand Down Expand Up @@ -591,5 +600,6 @@ static type _name[WASM_INSTRUCTION_NUM] = { \
do { \
_name[WASM_OP_MISC_PREFIX] = \
HANDLE_OPCODE (WASM_OP_MISC_PREFIX); /* 0xfc */ \
DEF_ATOMIC_PREFIX_HANDLE(_name) \
} while (0)
#endif /* end of _WASM_OPCODE_H */
8 changes: 8 additions & 0 deletions core/iwasm/interpreter/wasm_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ memories_deinstantiate(WASMModuleInstance *module_inst,
continue;
#endif
#if WASM_ENABLE_SHARED_MEMORY != 0
os_mutex_destroy(&memories[0]->mem_lock);
if (memories[i]->is_shared) {
int32 ref_count =
shared_memory_dec_reference(
Expand Down Expand Up @@ -192,6 +193,11 @@ memory_instantiate(WASMModuleInstance *module_inst,
memory->heap_base_offset = -(int32)heap_size;

#if WASM_ENABLE_SHARED_MEMORY != 0
if (0 != os_mutex_init(&memory->mem_lock)) {
mem_allocator_destroy(memory->heap_handle);
wasm_runtime_free(memory);
return NULL;
}
if (is_shared_memory) {
memory->is_shared = true;
if (!shared_memory_set_memory_inst(
Expand All @@ -200,6 +206,8 @@ memory_instantiate(WASMModuleInstance *module_inst,
set_error_buf(error_buf, error_buf_size,
"Instantiate memory failed:"
"allocate memory failed.");
os_mutex_destroy(&memory->mem_lock);
mem_allocator_destroy(memory->heap_handle);
wasm_runtime_free(memory);
return NULL;
}
Expand Down
6 changes: 6 additions & 0 deletions core/iwasm/interpreter/wasm_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ typedef struct WASMMemoryInstance {
/* to indicate which module instance create it */
WASMModuleInstance *owner;
#endif

#if WASM_ENABLE_SHARED_MEMORY != 0
/* mutex lock for the memory, used in atomic operation */
korp_mutex mem_lock;
#endif

/* Base address, the layout is:
heap_data + memory data
memory data init size is: num_bytes_per_page * cur_page_count
Expand Down
1 change: 0 additions & 1 deletion doc/pthread_library.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ You can also build this program with WASI, but we need to make some changes to w
```
> Note: </br>
>1. Remember to back up the original sysroot files
>2. wasi-sdk 9.0 or above are not supported, please use 7.0 or 8.0

Then build the program with this command:
``` bash
Expand Down