Skip to content

[WebAssembly] Add SWIFT_ONCE_F implementation to Lazy.h #31674

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

Merged
merged 1 commit into from
May 13, 2020
Merged
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
22 changes: 22 additions & 0 deletions include/swift/Basic/Lazy.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@
#include "swift/Basic/Malloc.h"
#include "swift/Basic/type_traits.h"

#if defined(__wasi__)
// Temporary single-threaded stub. Should be replaced with a thread-safe version
// as soon as the WASI SDK allows it. See https://bugs.swift.org/browse/SR-12766.
inline void wasi_call_once(int *flag, void *context, void (*func)(void *)) {
switch (*flag) {
case 0:
*flag = 1;
func(context);
return;
case 1:
return;
default:
assert(false && "wasi_call_once got invalid flag");
abort();
}
}
#endif

namespace swift {

#ifdef __APPLE__
Expand All @@ -38,6 +56,10 @@ namespace swift {
using OnceToken_t = unsigned long;
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \
_swift_once_f(&TOKEN, CONTEXT, FUNC)
#elif defined(__wasi__)
using OnceToken_t = int;
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \
::wasi_call_once(&TOKEN, CONTEXT, FUNC)
#else
using OnceToken_t = std::once_flag;
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \
Expand Down