8
8
//
9
9
10
10
@_implementationOnly import CoreFoundation
11
+ #if canImport(Dispatch)
11
12
import Dispatch
13
+ #endif
12
14
13
15
// FileHandle has a .read(upToCount:) method. Just invoking read() will cause an ambiguity warning. Use _read instead.
14
16
// Same with close()/.close().
@@ -90,6 +92,7 @@ open class FileHandle : NSObject {
90
92
91
93
private var _closeOnDealloc : Bool
92
94
95
+ #if canImport(Dispatch)
93
96
private var currentBackgroundActivityOwner : AnyObject ? // Guarded by privateAsyncVariablesLock
94
97
95
98
private var readabilitySource : DispatchSourceProtocol ? // Guarded by privateAsyncVariablesLock
@@ -213,6 +216,7 @@ open class FileHandle : NSObject {
213
216
}
214
217
}
215
218
}
219
+ #endif // canImport(Dispatch)
216
220
217
221
open var availableData : Data {
218
222
_checkFileHandle ( )
@@ -626,6 +630,7 @@ open class FileHandle : NSObject {
626
630
}
627
631
628
632
private func performOnQueueIfExists( _ block: ( ) throws -> Void ) throws {
633
+ #if canImport(Dispatch)
629
634
if let queue = queueIfExists {
630
635
var theError : Swift . Error ?
631
636
queue. sync {
@@ -637,6 +642,9 @@ open class FileHandle : NSObject {
637
642
} else {
638
643
try block ( )
639
644
}
645
+ #else
646
+ try block ( )
647
+ #endif
640
648
}
641
649
642
650
@available ( swift 5 . 0 )
@@ -651,6 +659,7 @@ open class FileHandle : NSObject {
651
659
guard self != FileHandle . _nulldeviceFileHandle else { return }
652
660
guard _isPlatformHandleValid else { return }
653
661
662
+ #if canImport(Dispatch)
654
663
privateAsyncVariablesLock. lock ( )
655
664
writabilitySource? . cancel ( )
656
665
readabilitySource? . cancel ( )
@@ -659,6 +668,7 @@ open class FileHandle : NSObject {
659
668
writabilitySource = nil
660
669
readabilitySource = nil
661
670
privateAsyncVariablesLock. unlock ( )
671
+ #endif
662
672
663
673
#if os(Windows)
664
674
// SR-13822 - Not Closing the file descriptor on Windows causes a Stack Overflow
@@ -861,6 +871,9 @@ extension FileHandle {
861
871
}
862
872
863
873
open func readInBackgroundAndNotify( forModes modes: [ RunLoop . Mode ] ? ) {
874
+ #if !canImport(Dispatch)
875
+ NSUnsupported ( )
876
+ #else
864
877
_checkFileHandle ( )
865
878
866
879
privateAsyncVariablesLock. lock ( )
@@ -915,13 +928,17 @@ extension FileHandle {
915
928
operation ( data, error)
916
929
}
917
930
#endif
931
+ #endif // canImport(Dispatch)
918
932
}
919
933
920
934
open func readToEndOfFileInBackgroundAndNotify( ) {
921
935
readToEndOfFileInBackgroundAndNotify ( forModes: [ . default] )
922
936
}
923
937
924
938
open func readToEndOfFileInBackgroundAndNotify( forModes modes: [ RunLoop . Mode ] ? ) {
939
+ #if !canImport(Dispatch) || !canImport(Dispatch)
940
+ NSUnsupported ( )
941
+ #else
925
942
privateAsyncVariablesLock. lock ( )
926
943
guard currentBackgroundActivityOwner == nil else { fatalError ( " No two activities can occur at the same time " ) }
927
944
@@ -963,11 +980,12 @@ extension FileHandle {
963
980
NotificationQueue . default. enqueue ( Notification ( name: . NSFileHandleReadToEndOfFileCompletion, object: self , userInfo: userInfo) , postingStyle: . asap, coalesceMask: . none, forModes: modes)
964
981
}
965
982
}
983
+ #endif
966
984
}
967
985
968
986
@available ( Windows, unavailable, message: " A SOCKET cannot be treated as a fd " )
969
987
open func acceptConnectionInBackgroundAndNotify( ) {
970
- #if os(Windows)
988
+ #if os(Windows) || !canImport(Dispatch)
971
989
NSUnsupported ( )
972
990
#else
973
991
acceptConnectionInBackgroundAndNotify ( forModes: [ . default] )
@@ -976,7 +994,7 @@ extension FileHandle {
976
994
977
995
@available ( Windows, unavailable, message: " A SOCKET cannot be treated as a fd " )
978
996
open func acceptConnectionInBackgroundAndNotify( forModes modes: [ RunLoop . Mode ] ? ) {
979
- #if os(Windows)
997
+ #if os(Windows) || !canImport(Dispatch)
980
998
NSUnsupported ( )
981
999
#else
982
1000
let owner = monitor ( forReading: true , resumed: false ) { ( handle, source) in
@@ -1014,6 +1032,9 @@ extension FileHandle {
1014
1032
}
1015
1033
1016
1034
open func waitForDataInBackgroundAndNotify( forModes modes: [ RunLoop . Mode ] ? ) {
1035
+ #if !canImport(Dispatch)
1036
+ NSUnsupported ( )
1037
+ #else
1017
1038
let owner = monitor ( forReading: true , resumed: false ) { ( handle, source) in
1018
1039
source. cancel ( )
1019
1040
DispatchQueue . main. async {
@@ -1031,6 +1052,7 @@ extension FileHandle {
1031
1052
privateAsyncVariablesLock. unlock ( )
1032
1053
1033
1054
owner. resume ( )
1055
+ #endif
1034
1056
}
1035
1057
}
1036
1058
@@ -1052,6 +1074,8 @@ open class Pipe: NSObject {
1052
1074
closeOnDealloc: true )
1053
1075
self . fileHandleForWriting = FileHandle ( handle: hWritePipe!,
1054
1076
closeOnDealloc: true )
1077
+ #elseif os(WASI)
1078
+ NSUnsupported ( )
1055
1079
#else
1056
1080
/// the `pipe` system call creates two `fd` in a malloc'ed area
1057
1081
let fds = UnsafeMutablePointer< Int32> . allocate( capacity: 2 )
@@ -1078,4 +1102,3 @@ open class Pipe: NSObject {
1078
1102
super. init ( )
1079
1103
}
1080
1104
}
1081
-
0 commit comments