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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion stdlib/public/runtime/ThreadLocalStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ static_assert(std::is_same<__swift_thread_key_t, DWORD>::value,
# define SWIFT_THREAD_KEY_CREATE _stdlib_thread_key_create
# define SWIFT_THREAD_GETSPECIFIC FlsGetValue
# define SWIFT_THREAD_SETSPECIFIC(key, value) (FlsSetValue(key, value) == FALSE)

# elif defined(__wasi__)
# define SWIFT_THREAD_KEY_CREATE _stdlib_thread_key_create
# define SWIFT_THREAD_GETSPECIFIC _stdlib_thread_getspecific
# define SWIFT_THREAD_SETSPECIFIC _stdlib_thread_setspecific
# else
// Otherwise use the pthread API.
# include <pthread.h>
Expand Down
38 changes: 38 additions & 0 deletions stdlib/public/stubs/ThreadLocalStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,44 @@ _stdlib_thread_key_create(__swift_thread_key_t * _Nonnull key,

#endif

#if defined(__wasi__)
// WebAssembly doesn't have threading standardized yet, so TLS is emulated
// here, see https://bugs.swift.org/browse/SR-12774.
#include <map>
using __swift_thread_key_destructor = void (*)(void *);

struct _stdlib_tls_element_t {
const void *value;
__swift_thread_key_destructor destructor;
};

using _stdlib_tls_map_t = std::map<__swift_thread_key_t, _stdlib_tls_element_t>;
static void *_stdlib_tls_map;

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?

auto *map = (_stdlib_tls_map_t *)_stdlib_tls_map;
*key = map->size();
_stdlib_tls_element_t element = { nullptr, destructor };
map->insert(std::make_pair(*key, element));
return 0;
}

static inline void *_stdlib_thread_getspecific(__swift_thread_key_t key) {
auto *map = (_stdlib_tls_map_t *)_stdlib_tls_map;
return const_cast<void *>(map->operator[](key).value);
}

static inline int _stdlib_thread_setspecific(__swift_thread_key_t key, const void *value) {
auto *map = (_stdlib_tls_map_t *)_stdlib_tls_map;
map->operator[](key).value = value;
return 0;
}

#endif

#if SWIFT_TLS_HAS_RESERVED_PTHREAD_SPECIFIC

SWIFT_RUNTIME_STDLIB_INTERNAL
Expand Down