1414import Darwin
1515#elseif canImport(Glibc)
1616import Glibc
17+ #elseif os(WASI)
18+ import WASILibc
1719#elseif os(Windows)
1820import CRT
1921import WinSDK
@@ -36,6 +38,8 @@ public struct _stdlib_thread_barrier_t {
3638#elseif os(Cygwin) || os(FreeBSD) || os(OpenBSD)
3739 var mutex : UnsafeMutablePointer < pthread_mutex_t ? > ?
3840 var cond : UnsafeMutablePointer < pthread_cond_t ? > ?
41+ #elseif os(WASI)
42+ // pthread is currently not available on WASI
3943#else
4044 var mutex : UnsafeMutablePointer < pthread_mutex_t > ?
4145 var cond : UnsafeMutablePointer < pthread_cond_t > ?
@@ -67,6 +71,8 @@ public func _stdlib_thread_barrier_init(
6771
6872 barrier. pointee. cond = UnsafeMutablePointer . allocate ( capacity: 1 )
6973 InitializeConditionVariable ( barrier. pointee. cond!)
74+ #elseif os(WASI)
75+ // WASI environment has only a single thread
7076#else
7177 barrier. pointee. mutex = UnsafeMutablePointer . allocate ( capacity: 1 )
7278 barrier. pointee. cond = UnsafeMutablePointer . allocate ( capacity: 1 )
@@ -82,7 +88,7 @@ public func _stdlib_thread_barrier_init(
8288 return 0
8389}
8490
85- #if !os(Windows)
91+ #if !os(Windows) && !os(WASI)
8692private func _stdlib_thread_barrier_mutex_and_cond_init( _ barrier: UnsafeMutablePointer < _stdlib_thread_barrier_t > ) -> CInt {
8793 guard pthread_mutex_init ( barrier. pointee. mutex!, nil ) == 0 else {
8894 return - 1
@@ -101,17 +107,22 @@ public func _stdlib_thread_barrier_destroy(
101107#if os(Windows)
102108 // Condition Variables do not need to be explicitly destroyed
103109 // Mutexes do not need to be explicitly destroyed
110+ #elseif os(WASI)
111+ // WASI environment has only a single thread
104112#else
105113 guard pthread_cond_destroy ( barrier. pointee. cond!) == 0 &&
106114 pthread_mutex_destroy ( barrier. pointee. mutex!) == 0 else {
107115 fatalError ( " _stdlib_thread_barrier_destroy() failed " )
108116 }
109117#endif
118+
119+ #if !os(WASI)
110120 barrier. pointee. cond!. deinitialize ( count: 1 )
111121 barrier. pointee. cond!. deallocate ( )
112122
113123 barrier. pointee. mutex!. deinitialize ( count: 1 )
114124 barrier. pointee. mutex!. deallocate ( )
125+ #endif
115126
116127 return
117128}
@@ -121,6 +132,8 @@ public func _stdlib_thread_barrier_wait(
121132) -> CInt {
122133#if os(Windows)
123134 AcquireSRWLockExclusive ( barrier. pointee. mutex!)
135+ #elseif os(WASI)
136+ // WASI environment has only a single thread
124137#else
125138 if pthread_mutex_lock ( barrier. pointee. mutex!) != 0 {
126139 return - 1
@@ -135,6 +148,8 @@ public func _stdlib_thread_barrier_wait(
135148 return - 1
136149 }
137150 ReleaseSRWLockExclusive ( barrier. pointee. mutex!)
151+ #elseif os(WASI)
152+ // WASI environment has a only single thread
138153#else
139154 if pthread_cond_wait ( barrier. pointee. cond!, barrier. pointee. mutex!) != 0 {
140155 return - 1
@@ -152,6 +167,8 @@ public func _stdlib_thread_barrier_wait(
152167#if os(Windows)
153168 WakeAllConditionVariable ( barrier. pointee. cond!)
154169 ReleaseSRWLockExclusive ( barrier. pointee. mutex!)
170+ #elseif os(WASI)
171+ // WASI environment has a only single thread
155172#else
156173 if pthread_cond_broadcast ( barrier. pointee. cond!) != 0 {
157174 return - 1
0 commit comments