Skip to content

Commit 4b9fca5

Browse files
authored
Merge pull request swiftlang#31674 from swiftwasm/maxd/wasi-call-once
[WebAssembly] Add SWIFT_ONCE_F implementation to Lazy.h
2 parents 76b6e3e + ec264a8 commit 4b9fca5

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

include/swift/Basic/Lazy.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@
2424
#include "swift/Basic/Malloc.h"
2525
#include "swift/Basic/type_traits.h"
2626

27+
#if defined(__wasi__)
28+
// Temporary single-threaded stub. Should be replaced with a thread-safe version
29+
// as soon as the WASI SDK allows it. See https://bugs.swift.org/browse/SR-12766.
30+
inline void wasi_call_once(int *flag, void *context, void (*func)(void *)) {
31+
switch (*flag) {
32+
case 0:
33+
*flag = 1;
34+
func(context);
35+
return;
36+
case 1:
37+
return;
38+
default:
39+
assert(false && "wasi_call_once got invalid flag");
40+
abort();
41+
}
42+
}
43+
#endif
44+
2745
namespace swift {
2846

2947
#ifdef __APPLE__
@@ -38,6 +56,10 @@ namespace swift {
3856
using OnceToken_t = unsigned long;
3957
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \
4058
_swift_once_f(&TOKEN, CONTEXT, FUNC)
59+
#elif defined(__wasi__)
60+
using OnceToken_t = int;
61+
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \
62+
::wasi_call_once(&TOKEN, CONTEXT, FUNC)
4163
#else
4264
using OnceToken_t = std::once_flag;
4365
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \

0 commit comments

Comments
 (0)