Skip to content

Runtime: emulate TLS with a map for WASI target #31694

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

Closed
wants to merge 1 commit into from

Conversation

MaxDesiatov
Copy link
Contributor

Runtime thread-local storage has to be emulated for WebAssembly/WASI as WebAssembly currently doesn't support threads.

Related to SR-9307.

(cc @compnerd @kateinoigakukun)

@compnerd
Copy link
Member

Why cant you use -femulated-tls?

@compnerd
Copy link
Member

CC: @mikeash

@MaxDesiatov
Copy link
Contributor Author

I didn't know about -femulated-tls, need to try that 😄

static inline int _stdlib_thread_key_create(__swift_thread_key_t *key,
__swift_thread_key_destructor destructor) {
if (!_stdlib_tls_map)
_stdlib_tls_map = new _stdlib_tls_map_t();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
_stdlib_tls_map = new _stdlib_tls_map_t();
_stdlib_tls_map = new _stdlib_tls_map_t();

Nit: if still pursuing this PR, would you mind running the changes through the clang-format tool?

@MaxDesiatov
Copy link
Contributor Author

@compnerd I had another look at -femulated-tls and it's unclear to me how SWIFT_THREAD_GETSPECIFIC should be defined with -femulated-tls? We would still have to implement pthread_getspecific stubs, as wasi-sdk has no pthreads. Would that be substantially different from what is proposed in this PR?

@mikeash
Copy link
Contributor

mikeash commented May 11, 2020

I think the best long-term plan would be to add a mode to ThreadLocalStorage.cpp that can use a thread-local variable, like we have in Exclusivity.cpp. I'm not sure why we only have that in one of those files, maybe they just evolved separately. That would allow you to use -femulated-tls. Not sure if you want to do that here or save it for another time, though.

@compnerd
Copy link
Member

Using -femulated-tls means that you don't need to provide the mapping for the TLS access. Although it will be slower than real TLS, its shifting the responsibility to a well tested library implementation (compiler-rt). With the emulated TLS, you do not use the pthread local API but rather __thread annotations and let the compiler DTRT.

@MaxDesiatov MaxDesiatov changed the base branch from master to main September 24, 2020 08:42
@filip-sakel
Copy link
Contributor

Sorry to revive this, but is TLS still an issue with built-in concurrency?

@mikeash
Copy link
Contributor

mikeash commented Dec 12, 2022

The runtime still needs thread-local storage for various things even if application code is using higher-level constructs. For example, retrieving a task-local variable in user code works by using TLS to fetch the current task running on the current thread, then retrieving the thread-local variable from that task.

Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

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

The map access needs locking, which I believe is missing or am I misreading the patch?

Can we get away with just using -femul-tls when building for WASI? That should let us use a tested implementation for the mapping from compiler-Ty.

@kateinoigakukun
Copy link
Member

Closing in favor of cleaner SWIFT_THREADING_NONE way

@kateinoigakukun kateinoigakukun deleted the maxd/wasi-runtime-tls branch October 3, 2023 21:31
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.

6 participants