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