-
Notifications
You must be signed in to change notification settings - Fork 636
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
Improve WASI thread id allocator to reuse identifiers #1809
Improve WASI thread id allocator to reuse identifiers #1809
Conversation
core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c
Outdated
Show resolved
Hide resolved
core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c
Outdated
Show resolved
Hide resolved
core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c
Outdated
Show resolved
Hide resolved
core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c
Outdated
Show resolved
Hide resolved
core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c
Outdated
Show resolved
Hide resolved
26f72a6
to
3b4ff81
Compare
core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lgtm
I'm doing some additional refactoring to have both |
3b4ff81
to
7e32b7d
Compare
core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c
Outdated
Show resolved
Hide resolved
core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c
Outdated
Show resolved
Hide resolved
core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c
Outdated
Show resolved
Hide resolved
core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c
Outdated
Show resolved
Hide resolved
if (avail_tids.pos == 0) { // Resize stack and push new thread ids | ||
uint32 old_size = avail_tids.size; | ||
uint32 new_size = avail_tids.size * 2; | ||
if (new_size / 2 != avail_tids.size) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[minor] you might just compare old_size
with the new_size
to avoid additional division.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used the division to make it similar to L54. I'm not sure a simple comparison would work for L54 (maybe when overflowing/wrapping the result can be bigger than the initial value?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, one way to detect integer overflow here is to check if new_size < avail_tids.size
. It's not a major callout though.
core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c
Outdated
Show resolved
Hide resolved
core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c
Outdated
Show resolved
Hide resolved
7e32b7d
to
6c25253
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks ok to me
goto return_id; | ||
} | ||
int32 *tmp = | ||
(int32 *)wasm_runtime_realloc(avail_tids.ids, realloc_size); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't need a cast
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I get rid of it? I put it (and same for the malloc) for consistency with the rest of the codebase
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@eloparco Can you resolve the conflicts?
6c25253
to
867954a
Compare
@wenyongh fixed the conflicts, I think we can merge it now |
…ytecodealliance#1809) This PR allows reusing thread ids once they are released. That is done by using a stack data structure to keep track of the used ids. When a thread is created, it takes an available identifier from the stack. When the thread exits, it returns the id to the stack of available identifiers.
At the moment, when created, a thread receives an incremental number as an identifier.
This PR allows reusing thread ids once they are released. That is done by using a stack data structure to keep track of the used ids.
When a thread is created, it takes an available identifier from the stack. When the thread exits, it returns the id to the stack of available identifiers.