-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Added --thread_safe option to webidl_binder.py #22772
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
Conversation
tools/webidl_binder.py
Outdated
| parser = argparse.ArgumentParser() | ||
| parser.add_argument('--wasm64', action='store_true', default=False, | ||
| help='Build for wasm64') | ||
| parser.add_argument('--thread_safe', action='store_true', default=False, |
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.
can you use dashes in the option name (e.g. "--thread-safe" )
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.
Fixed.
tools/webidl_binder.py
Outdated
| if non_pointer: | ||
| return_prefix += '&' | ||
| if thread_safe: | ||
| storage_attribute = 'thread_local ' |
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.
Maybe we should just always emit this, when TLS gets lowered away when not building with thread support?
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.
We could, and you're right that if you build without -pthread the resulting code is the same:
https://godbolt.org/z/6cE6T8a47
When adding -pthread there is an extra instruction which adds a bit of overhead:
I cannot predict if people are ok with this overhead. Their library may use multiple threads internally, but maybe they don't interact with the library from multiple threads. In that case there's no benefit in adding the thread_local attribute.
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 think if you are doing a pthread build we should just default to being thread safe.
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.
Ok, in that case the change becomes really simple. I've pushed the new code.
f2a2e2a to
7caaa88
Compare
See discussion at: emscripten-core/emscripten#22772
| return_prefix += '&' | ||
| if copy: | ||
| pre += ' static %s temp;\n' % type_to_c(return_type, non_pointing=True) | ||
| pre += ' static thread_local %s temp;\n' % type_to_c(return_type, non_pointing=True) |
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.
Please add a comment, maybe something like "Avoid sharing this static temp var between threads, which could race." Otherwise looks great.
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.
Done.
This emits all temporaries as 'static thread_local <type> temp;' instead of 'static <type> temp;' and prevents race conditions when you use the resulting library in a multithreaded environment. When compiling without -pthread, the thread_local attribute is ignored and the generated code is as it was before.
This pulls in: - emscripten-core/emscripten#22770 - emscripten-core/emscripten#22772 and gets rid of the custom copy of webidl_binder.py

This emits all temporaries as 'static thread_local temp;' instead of 'static temp;' and prevents race conditions when you use the resulting library in a multithreaded environment.
E.g. if you create an IDL file like:
Previously the generated cpp file would look like:
Which has a race condition when you call into the
Crossfunction from multiple JavaScript threads as they all use the sametempvariable.With this new parameter the resulting code will look like:
Which guarantees that no other threads overwrite
temp.