Skip to content

Commit d6f351f

Browse files
committed
Foundation: initialize CFSocket on startup
Windows requires the networking subsystem (WinSock) to be initialized. However, the user expects to be able to use the networking APIs in Foundation without performing any one-time initialization. Furthermore, due to the macro usage in the initialization path, it is easier to perform the initialization in C than in Swift. CoreFoundation has a SPI for this (`__CFSocketInitializeWinSock`).
1 parent 63dd948 commit d6f351f

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,10 @@ _stat_with_btime(const char *filename, struct stat *buffer, struct timespec *bti
566566
#warning "Enabling statx"
567567
#endif
568568

569+
#if DEPLOYMENT_TARGET_WINDOWS
570+
CF_EXPORT void __CFSocketInitializeWinSock(void);
571+
#endif
572+
569573
_CF_EXPORT_SCOPE_END
570574

571575
#endif /* __COREFOUNDATION_FORSWIFTFOUNDATIONONLY__ */

Foundation/NSSwiftRuntime.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ internal func __CFSwiftGetBaseClass() -> UnsafeRawPointer {
150150
@usableFromInline
151151
@_cdecl("__CFInitializeSwift")
152152
internal func __CFInitializeSwift() {
153-
153+
#if os(Windows)
154+
__CFInitializeWinSock()
155+
#endif
156+
154157
_CFRuntimeBridgeTypeToClass(CFStringGetTypeID(), unsafeBitCast(_NSCFString.self, to: UnsafeRawPointer.self))
155158
_CFRuntimeBridgeTypeToClass(CFArrayGetTypeID(), unsafeBitCast(_NSCFArray.self, to: UnsafeRawPointer.self))
156159
_CFRuntimeBridgeTypeToClass(CFDictionaryGetTypeID(), unsafeBitCast(_NSCFDictionary.self, to: UnsafeRawPointer.self))

0 commit comments

Comments
 (0)