11/*
22 This source file is part of the Swift System open source project
33
4- Copyright (c) 2020 Apple Inc. and the Swift System project authors
4+ Copyright (c) 2020 - 2024 Apple Inc. and the Swift System project authors
55 Licensed under Apache License v2.0 with Runtime Library Exception
66
77 See https://swift.org/LICENSE.txt for license information
1414
1515#if SYSTEM_PACKAGE_DARWIN
1616import Darwin
17- #elseif os(Linux) || os(FreeBSD) || os(Android)
18- import CSystem
19- import Glibc
2017#elseif os(Windows)
2118import CSystem
2219import ucrt
20+ #elseif canImport(Glibc)
21+ @_implementationOnly import CSystem
22+ import Glibc
23+ #elseif canImport(Musl)
24+ @_implementationOnly import CSystem
25+ import Musl
26+ #elseif canImport(WASILibc)
27+ import WASILibc
28+ #elseif canImport(Android)
29+ @_implementationOnly import CSystem
30+ import Android
2331#else
2432#error("Unsupported Platform")
2533#endif
@@ -47,11 +55,26 @@ internal var system_errno: CInt {
4755 _ = ucrt. _set_errno ( newValue)
4856 }
4957}
50- #else
58+ #elseif canImport(Glibc)
5159internal var system_errno : CInt {
5260 get { Glibc . errno }
5361 set { Glibc . errno = newValue }
5462}
63+ #elseif canImport(Musl)
64+ internal var system_errno : CInt {
65+ get { Musl . errno }
66+ set { Musl . errno = newValue }
67+ }
68+ #elseif canImport(WASILibc)
69+ internal var system_errno : CInt {
70+ get { WASILibc . errno }
71+ set { WASILibc . errno = newValue }
72+ }
73+ #elseif canImport(Android)
74+ internal var system_errno : CInt {
75+ get { Android . errno }
76+ set { Android . errno = newValue }
77+ }
5578#endif
5679
5780// MARK: C stdlib decls
@@ -62,7 +85,10 @@ internal func system_strerror(_ __errnum: Int32) -> UnsafeMutablePointer<Int8>!
6285 strerror ( __errnum)
6386}
6487
65- internal func system_strlen( _ s: UnsafePointer < Int8 > ) -> Int {
88+ internal func system_strlen( _ s: UnsafePointer < CChar > ) -> Int {
89+ strlen ( s)
90+ }
91+ internal func system_strlen( _ s: UnsafeMutablePointer < CChar > ) -> Int {
6692 strlen ( s)
6793}
6894
@@ -80,7 +106,17 @@ internal func system_platform_strlen(_ s: UnsafePointer<CInterop.PlatformChar>)
80106 #endif
81107}
82108
83- // Interop between String and platform string
109+ // memset for raw buffers
110+ // FIXME: Do we really not have something like this in the stdlib already?
111+ internal func system_memset(
112+ _ buffer: UnsafeMutableRawBufferPointer ,
113+ to byte: UInt8
114+ ) {
115+ guard buffer. count > 0 else { return }
116+ memset ( buffer. baseAddress!, CInt ( byte) , buffer. count)
117+ }
118+
119+ // Interop between String and platfrom string
84120extension String {
85121 internal func _withPlatformString< Result> (
86122 _ body: ( UnsafePointer < CInterop . PlatformChar > ) throws -> Result
@@ -130,6 +166,24 @@ extension String {
130166// TLS
131167#if os(Windows)
132168internal typealias _PlatformTLSKey = DWORD
169+ #elseif os(WASI) && (swift(<6.1) || !_runtime(_multithreaded))
170+ // Mock TLS storage for single-threaded WASI
171+ internal final class _PlatformTLSKey {
172+ fileprivate init ( ) { }
173+ }
174+ private final class TLSStorage : @unchecked Sendable {
175+ var storage = [ ObjectIdentifier: UnsafeMutableRawPointer] ( )
176+ }
177+ private let sharedTLSStorage = TLSStorage ( )
178+
179+ func pthread_setspecific( _ key: _PlatformTLSKey , _ p: UnsafeMutableRawPointer ? ) -> Int {
180+ sharedTLSStorage. storage [ ObjectIdentifier ( key) ] = p
181+ return 0
182+ }
183+
184+ func pthread_getspecific( _ key: _PlatformTLSKey ) -> UnsafeMutableRawPointer ? {
185+ sharedTLSStorage. storage [ ObjectIdentifier ( key) ]
186+ }
133187#else
134188internal typealias _PlatformTLSKey = pthread_key_t
135189#endif
@@ -141,6 +195,8 @@ internal func makeTLSKey() -> _PlatformTLSKey {
141195 fatalError ( " Unable to create key " )
142196 }
143197 return raw
198+ #elseif os(WASI) && (swift(<6.1) || !_runtime(_multithreaded))
199+ return _PlatformTLSKey ( )
144200 #else
145201 var raw = pthread_key_t ( )
146202 guard 0 == pthread_key_create ( & raw, nil ) else {
0 commit comments