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

Improve WASI thread id allocator to reuse identifiers #1809

Conversation

eloparco
Copy link
Contributor

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.

@eloparco eloparco force-pushed the eloparco/thread-id-allocator branch 2 times, most recently from 26f72a6 to 3b4ff81 Compare December 14, 2022 13:27
Copy link
Collaborator

@loganek loganek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

@eloparco
Copy link
Contributor Author

I'm doing some additional refactoring to have both pos and size as unsigned so that comparisons are safe. I just need to get pos to point to the first empty position in the stack rather than at the top element.

@eloparco eloparco force-pushed the eloparco/thread-id-allocator branch from 3b4ff81 to 7e32b7d Compare December 14, 2022 16:54
@loganek loganek mentioned this pull request Dec 14, 2022
19 tasks
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) {
Copy link
Collaborator

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.

Copy link
Contributor Author

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?)

Copy link
Collaborator

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.

@eloparco eloparco force-pushed the eloparco/thread-id-allocator branch from 7e32b7d to 6c25253 Compare December 16, 2022 01:06
Copy link
Collaborator

@yamt yamt left a 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);
Copy link
Collaborator

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

Copy link
Contributor Author

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

@eloparco eloparco changed the title feat(wasi-threads): improve thread id allocator to reuse identifiers Improve WASI thread id allocator to reuse identifiers Dec 16, 2022
@eloparco eloparco requested a review from wenyongh December 19, 2022 02:07
Copy link
Contributor

@wenyongh wenyongh left a 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?

@eloparco eloparco force-pushed the eloparco/thread-id-allocator branch from 6c25253 to 867954a Compare December 19, 2022 10:05
@eloparco
Copy link
Contributor Author

@wenyongh fixed the conflicts, I think we can merge it now

@wenyongh wenyongh merged commit 0c85cb1 into bytecodealliance:dev/wasi_threads Dec 19, 2022
victoryang00 pushed a commit to victoryang00/wamr-aot-gc-checkpoint-restore that referenced this pull request May 27, 2024
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants