-
Notifications
You must be signed in to change notification settings - Fork 750
Implement async termination of blocking thread #2516
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
Merged
Merged
Changes from all commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
53f1655
some logic to terminate blocking op
yamt 5ea4c03
avoid sleeping with a mutex held
yamt 6fbe85c
comment
yamt 63bdb21
fix an inverted condition
yamt 1bfc737
wasm_export.h: add wasm_runtime_{begin|end}_blocking_op
yamt ec28fa2
make set_thread_cancel_flags interrupt blocking op
yamt acfd426
wasi: mark blocking ops explicitly
yamt 3114f90
separate blocking op initialization from thread signal init
yamt 0dbcee3
nuttx: update the file list
yamt f8c8070
wasm_blocking_op.c: this stuff doesn't make sense w/o threads
yamt dee874b
add dummy wasm_runtime_begin_blocking_op and wasm_runtime_end_blockin…
yamt a5c3fe0
fix build
yamt 926ad88
OS_ENABLE_WAKEUP_BLOCKING_OP
yamt 4d9668e
OS_ENABLE_WAKEUP_BLOCKING_OP
yamt 71f390d
call os_end_blocking_op in the intended function
yamt 0e856d2
add a missing os_end_blocking_op
yamt 3ee082d
move blocking op wrapper into a separate file
yamt 62a5e23
wasi: WIP adapt more ops
yamt e41f4e3
wasi: adapt connect, accept
yamt 798a2fe
wasm_runtime_end_blocking_op: preserve errno
yamt ed8851e
apply clang-format
yamt a3fc491
nuttx/wamr.mk: add blocking_op.c
yamt e1379c5
some platforms doesn't seem to have sys/uio.h
yamt 29422d2
wasi: adapt send_to/recv_from
yamt 1a04b48
wasi: adapt socket_addr_resolve
yamt 0fe0f1c
wasi: fix comments
yamt 896cfeb
add a comment
yamt 8775b9a
comment
yamt 0454842
wasi: adapt openat
yamt a770e37
comment
yamt 6643822
add os_set_signal_number_for_blocking_op
yamt 33d23f0
comment
yamt 143a7d6
posix_blocking_op.c: use pthread_sigmask instead of sigprocmask
yamt fd13caf
add a comment about getaddrinfo
yamt 5ac3cb5
comment
yamt 0daf3d7
comments
yamt b5399cd
comment
yamt ecf798a
add a build-time config WAMR_DISABLE_WAKEUP_BLOCKING_OP
yamt 3be54a0
add messages
yamt 778d2b2
add a missing unlock
yamt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| * Copyright (C) 2023 Midokura Japan KK. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| */ | ||
|
|
||
| #include "wasm_runtime_common.h" | ||
|
|
||
| #include "bh_platform.h" | ||
| #include "bh_common.h" | ||
| #include "bh_assert.h" | ||
|
|
||
| #if WASM_ENABLE_THREAD_MGR != 0 && defined(OS_ENABLE_WAKEUP_BLOCKING_OP) | ||
|
|
||
| #define LOCK(env) WASM_SUSPEND_FLAGS_LOCK((env)->wait_lock) | ||
| #define UNLOCK(env) WASM_SUSPEND_FLAGS_UNLOCK((env)->wait_lock) | ||
|
|
||
| #define ISSET(env, bit) \ | ||
| ((WASM_SUSPEND_FLAGS_GET((env)->suspend_flags) & WASM_SUSPEND_FLAG_##bit) \ | ||
| != 0) | ||
| #define SET(env, bit) \ | ||
| WASM_SUSPEND_FLAGS_FETCH_OR((env)->suspend_flags, WASM_SUSPEND_FLAG_##bit) | ||
| #define CLR(env, bit) \ | ||
| WASM_SUSPEND_FLAGS_FETCH_AND((env)->suspend_flags, ~WASM_SUSPEND_FLAG_##bit) | ||
|
|
||
| bool | ||
| wasm_runtime_begin_blocking_op(wasm_exec_env_t env) | ||
| { | ||
| LOCK(env); | ||
| bh_assert(!ISSET(env, BLOCKING)); | ||
| SET(env, BLOCKING); | ||
| if (ISSET(env, TERMINATE)) { | ||
| CLR(env, BLOCKING); | ||
| UNLOCK(env); | ||
| return false; | ||
| } | ||
| UNLOCK(env); | ||
| os_begin_blocking_op(); | ||
| return true; | ||
| } | ||
|
|
||
| void | ||
| wasm_runtime_end_blocking_op(wasm_exec_env_t env) | ||
| { | ||
| int saved_errno = errno; | ||
| LOCK(env); | ||
| bh_assert(ISSET(env, BLOCKING)); | ||
| CLR(env, BLOCKING); | ||
| UNLOCK(env); | ||
| os_end_blocking_op(); | ||
| errno = saved_errno; | ||
| } | ||
|
|
||
| void | ||
| wasm_runtime_interrupt_blocking_op(wasm_exec_env_t env) | ||
| { | ||
| /* | ||
| * ISSET(BLOCKING) here means that the target thread | ||
| * is in somewhere between wasm_begin_blocking_op and | ||
| * wasm_end_blocking_op. | ||
| * keep waking it up until it reaches wasm_end_blocking_op, | ||
| * which clears the BLOCKING bit. | ||
| * | ||
| * this dumb loop is necessary because posix doesn't provide | ||
| * a way to unmask signal and block atomically. | ||
| */ | ||
|
|
||
| LOCK(env); | ||
| SET(env, TERMINATE); | ||
| while (ISSET(env, BLOCKING)) { | ||
| UNLOCK(env); | ||
| os_wakeup_blocking_op(env->handle); | ||
|
|
||
| /* relax a bit */ | ||
| os_usleep(50 * 1000); | ||
| LOCK(env); | ||
| } | ||
| UNLOCK(env); | ||
| } | ||
|
|
||
| #else /* WASM_ENABLE_THREAD_MGR && OS_ENABLE_WAKEUP_BLOCKING_OP */ | ||
|
|
||
| bool | ||
| wasm_runtime_begin_blocking_op(wasm_exec_env_t env) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| void | ||
| wasm_runtime_end_blocking_op(wasm_exec_env_t env) | ||
| {} | ||
|
|
||
| #endif /* WASM_ENABLE_THREAD_MGR && OS_ENABLE_WAKEUP_BLOCKING_OP */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.