Skip to content
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