88//
99
1010@_implementationOnly import CoreFoundation
11+ #if canImport(Dispatch)
1112import Dispatch
13+ #endif
1214
1315// FileHandle has a .read(upToCount:) method. Just invoking read() will cause an ambiguity warning. Use _read instead.
1416// Same with close()/.close().
@@ -89,6 +91,7 @@ open class FileHandle : NSObject {
8991
9092 private var _closeOnDealloc : Bool
9193
94+ #if canImport(Dispatch)
9295 private var currentBackgroundActivityOwner : AnyObject ? // Guarded by privateAsyncVariablesLock
9396
9497 private var readabilitySource : DispatchSourceProtocol ? // Guarded by privateAsyncVariablesLock
@@ -212,6 +215,7 @@ open class FileHandle : NSObject {
212215 }
213216 }
214217 }
218+ #endif // canImport(Dispatch)
215219
216220 open var availableData : Data {
217221 _checkFileHandle ( )
@@ -625,6 +629,7 @@ open class FileHandle : NSObject {
625629 }
626630
627631 private func performOnQueueIfExists( _ block: ( ) throws -> Void ) throws {
632+ #if canImport(Dispatch)
628633 if let queue = queueIfExists {
629634 var theError : Swift . Error ?
630635 queue. sync {
@@ -636,6 +641,9 @@ open class FileHandle : NSObject {
636641 } else {
637642 try block ( )
638643 }
644+ #else
645+ try block ( )
646+ #endif
639647 }
640648
641649 @available ( swift 5 . 0 )
@@ -650,6 +658,7 @@ open class FileHandle : NSObject {
650658 guard self != FileHandle . _nulldeviceFileHandle else { return }
651659 guard _isPlatformHandleValid else { return }
652660
661+ #if canImport(Dispatch)
653662 privateAsyncVariablesLock. lock ( )
654663 writabilitySource? . cancel ( )
655664 readabilitySource? . cancel ( )
@@ -658,6 +667,7 @@ open class FileHandle : NSObject {
658667 writabilitySource = nil
659668 readabilitySource = nil
660669 privateAsyncVariablesLock. unlock ( )
670+ #endif
661671
662672#if os(Windows)
663673 // SR-13822 - Not Closing the file descriptor on Windows causes a Stack Overflow
@@ -860,6 +870,9 @@ extension FileHandle {
860870 }
861871
862872 open func readInBackgroundAndNotify( forModes modes: [ RunLoop . Mode ] ? ) {
873+ #if !canImport(Dispatch)
874+ NSUnsupported ( )
875+ #else
863876 _checkFileHandle ( )
864877
865878 privateAsyncVariablesLock. lock ( )
@@ -914,13 +927,17 @@ extension FileHandle {
914927 operation ( data, error)
915928 }
916929#endif
930+ #endif // canImport(Dispatch)
917931 }
918932
919933 open func readToEndOfFileInBackgroundAndNotify( ) {
920934 readToEndOfFileInBackgroundAndNotify ( forModes: [ . default] )
921935 }
922936
923937 open func readToEndOfFileInBackgroundAndNotify( forModes modes: [ RunLoop . Mode ] ? ) {
938+ #if !canImport(Dispatch) || !canImport(Dispatch)
939+ NSUnsupported ( )
940+ #else
924941 privateAsyncVariablesLock. lock ( )
925942 guard currentBackgroundActivityOwner == nil else { fatalError ( " No two activities can occur at the same time " ) }
926943
@@ -962,11 +979,12 @@ extension FileHandle {
962979 NotificationQueue . default. enqueue ( Notification ( name: . NSFileHandleReadToEndOfFileCompletion, object: self , userInfo: userInfo) , postingStyle: . asap, coalesceMask: . none, forModes: modes)
963980 }
964981 }
982+ #endif
965983 }
966984
967985 @available ( Windows, unavailable, message: " A SOCKET cannot be treated as a fd " )
968986 open func acceptConnectionInBackgroundAndNotify( ) {
969- #if os(Windows)
987+ #if os(Windows) || !canImport(Dispatch)
970988 NSUnsupported ( )
971989#else
972990 acceptConnectionInBackgroundAndNotify ( forModes: [ . default] )
@@ -975,7 +993,7 @@ extension FileHandle {
975993
976994 @available ( Windows, unavailable, message: " A SOCKET cannot be treated as a fd " )
977995 open func acceptConnectionInBackgroundAndNotify( forModes modes: [ RunLoop . Mode ] ? ) {
978- #if os(Windows)
996+ #if os(Windows) || !canImport(Dispatch)
979997 NSUnsupported ( )
980998#else
981999 let owner = monitor ( forReading: true , resumed: false ) { ( handle, source) in
@@ -1013,6 +1031,9 @@ extension FileHandle {
10131031 }
10141032
10151033 open func waitForDataInBackgroundAndNotify( forModes modes: [ RunLoop . Mode ] ? ) {
1034+ #if !canImport(Dispatch)
1035+ NSUnsupported ( )
1036+ #else
10161037 let owner = monitor ( forReading: true , resumed: false ) { ( handle, source) in
10171038 source. cancel ( )
10181039 DispatchQueue . main. async {
@@ -1030,6 +1051,7 @@ extension FileHandle {
10301051 privateAsyncVariablesLock. unlock ( )
10311052
10321053 owner. resume ( )
1054+ #endif
10331055 }
10341056}
10351057
@@ -1051,6 +1073,8 @@ open class Pipe: NSObject {
10511073 closeOnDealloc: true )
10521074 self . fileHandleForWriting = FileHandle ( handle: hWritePipe!,
10531075 closeOnDealloc: true )
1076+ #elseif os(WASI)
1077+ NSUnsupported ( )
10541078#else
10551079 /// the `pipe` system call creates two `fd` in a malloc'ed area
10561080 let fds = UnsafeMutablePointer< Int32> . allocate( capacity: 2 )
@@ -1077,4 +1101,3 @@ open class Pipe: NSObject {
10771101 super. init ( )
10781102 }
10791103}
1080-
0 commit comments