Skip to content

Commit 57b7a0a

Browse files
committed
Revert "Update NSURLSession to be buildable in Xcode and gate all access to dispatch with DEPLOYMENT_ENABLE_LIBDISPATCH"
This reverts commit 73ba103.
1 parent 73ba103 commit 57b7a0a

File tree

7 files changed

+41
-253
lines changed

7 files changed

+41
-253
lines changed

Foundation.xcodeproj/project.pbxproj

Lines changed: 4 additions & 73 deletions
Large diffs are not rendered by default.

Foundation/NSURLSession/EasyHandle.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
// -----------------------------------------------------------------------------
1919

2020
import CoreFoundation
21+
import Dispatch
22+
23+
2124

2225
extension URLSessionTask {
2326
/// Minimal wrapper around the [curl easy interface](https://curl.haxx.se/libcurl/c/)

Foundation/NSURLSession/HTTPBodySource.swift

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
// -----------------------------------------------------------------------------
1818

1919
import CoreFoundation
20-
#if DEPLOYMENT_ENABLE_LIBDISPATCH
2120
import Dispatch
2221

22+
23+
2324
internal extension Data {
2425
/// Turn `dispatch_data_t` into `NSData`
2526
init(dispatchData: DispatchData) {
@@ -54,8 +55,7 @@ internal func split(dispatchData data: DispatchData, atPosition position: Int) -
5455
return (head, tail)*/
5556
return (data.subdata(in: 0..<position), data.subdata(in: position..<data.count))
5657
}
57-
#endif
58-
58+
5959
/// A (non-blocking) source for HTTP body data.
6060
internal protocol _HTTPBodySource: class {
6161
/// Get the next chunck of data.
@@ -66,9 +66,7 @@ internal protocol _HTTPBodySource: class {
6666
func getNextChunk(withLength length: Int) -> _HTTPBodySourceDataChunk
6767
}
6868
internal enum _HTTPBodySourceDataChunk {
69-
#if DEPLOYMENT_ENABLE_LIBDISPATCH
7069
case data(DispatchData)
71-
#endif
7270
/// The source is depleted.
7371
case done
7472
/// Retry later to get more data.
@@ -78,12 +76,10 @@ internal enum _HTTPBodySourceDataChunk {
7876

7977
/// A HTTP body data source backed by `dispatch_data_t`.
8078
internal final class _HTTPBodyDataSource {
81-
#if DEPLOYMENT_ENABLE_LIBDISPATCH
82-
var data: DispatchData!
79+
var data: DispatchData!
8380
init(data: DispatchData) {
8481
self.data = data
8582
}
86-
#endif
8783
}
8884

8985
extension _HTTPBodyDataSource : _HTTPBodySource {
@@ -92,7 +88,6 @@ extension _HTTPBodyDataSource : _HTTPBodySource {
9288
}
9389

9490
func getNextChunk(withLength length: Int) -> _HTTPBodySourceDataChunk {
95-
#if DEPLOYMENT_ENABLE_LIBDISPATCH
9691
let remaining = data.count
9792
if remaining == 0 {
9893
return .done
@@ -105,9 +100,6 @@ extension _HTTPBodyDataSource : _HTTPBodySource {
105100
data = remainder
106101
return .data(chunk)
107102
}
108-
#else
109-
fatalError("NSURLSession requires libdispatch")
110-
#endif
111103
}
112104
}
113105

@@ -125,10 +117,8 @@ extension _HTTPBodyDataSource : _HTTPBodySource {
125117
/// have to be thread safe.
126118
internal final class _HTTPBodyFileSource {
127119
fileprivate let fileURL: URL
128-
#if DEPLOYMENT_ENABLE_LIBDISPATCH
129-
fileprivate let channel: DispatchIO
130-
fileprivate let workQueue: DispatchQueue
131-
#endif
120+
fileprivate let channel: DispatchIO
121+
fileprivate let workQueue: DispatchQueue
132122
fileprivate let dataAvailableHandler: () -> ()
133123
fileprivate var hasActiveReadHandler = false
134124
fileprivate var availableChunk: _Chunk = .empty
@@ -143,7 +133,6 @@ internal final class _HTTPBodyFileSource {
143133
/// no data may be available even if there's more data in the file.
144134
/// if `getNextChunk(withLength:)` returns `.retryLater`, this handler
145135
/// will be called once data becomes available.
146-
#if DEPLOYMENT_ENABLE_LIBDISPATCH
147136
init(fileURL: URL, workQueue: DispatchQueue, dataAvailableHandler: @escaping () -> ()) {
148137
guard fileURL.isFileURL else { fatalError("The body data URL must be a file URL.") }
149138
self.fileURL = fileURL
@@ -156,24 +145,16 @@ internal final class _HTTPBodyFileSource {
156145
self.channel = DispatchIO(type: .stream, path: fileSystemRepresentation, oflag: O_RDONLY, mode: 0, queue: workQueue, cleanupHandler: {_ in })
157146
self.channel.setLimit(highWater: CFURLSessionMaxWriteSize)
158147
}
159-
#else
160-
init(fileURL: URL, dataAvailableHandler: @escaping () -> ()) {
161-
self.fileURL = fileURL
162-
self.dataAvailableHandler = dataAvailableHandler
163-
}
164-
#endif
165148

166149
fileprivate enum _Chunk {
167150
/// Nothing has been read, yet
168151
case empty
169152
/// An error has occured while reading
170153
case errorDetected(Int)
171154
/// Data has been read
172-
#if DEPLOYMENT_ENABLE_LIBDISPATCH
173155
case data(DispatchData)
174156
/// All data has been read from the file (EOF).
175157
case done(DispatchData?)
176-
#endif
177158
}
178159
}
179160

@@ -191,7 +172,6 @@ fileprivate extension _HTTPBodyFileSource {
191172
hasActiveReadHandler = true
192173

193174
let lengthToRead = desiredBufferLength - availableByteCount
194-
#if DEPLOYMENT_ENABLE_LIBDISPATCH
195175
channel.read(offset: 0, length: lengthToRead, queue: workQueue) { (done: Bool, data: DispatchData?, errno: Int32) in
196176
let wasEmpty = self.availableByteCount == 0
197177
self.hasActiveReadHandler = !done
@@ -213,9 +193,8 @@ fileprivate extension _HTTPBodyFileSource {
213193
self.dataAvailableHandler()
214194
}
215195
}
216-
#endif
217196
}
218-
#if DEPLOYMENT_ENABLE_LIBDISPATCH
197+
219198
fileprivate func append(data: DispatchData, endOfFile: Bool) {
220199
switch availableChunk {
221200
case .empty:
@@ -229,26 +208,20 @@ fileprivate extension _HTTPBodyFileSource {
229208
fatalError("Trying to append data, but end-of-file was already detected.")
230209
}
231210
}
232-
#endif
233-
211+
234212
fileprivate var availableByteCount: Int {
235-
#if DEPLOYMENT_ENABLE_LIBDISPATCH
236213
switch availableChunk {
237214
case .empty: return 0
238215
case .errorDetected: return 0
239216
case .data(let d): return d.count
240217
case .done(.some(let d)): return d.count
241218
case .done(.none): return 0
242219
}
243-
#else
244-
return 0
245-
#endif
246220
}
247221
}
248222

249223
extension _HTTPBodyFileSource : _HTTPBodySource {
250-
func getNextChunk(withLength length: Int) -> _HTTPBodySourceDataChunk {
251-
#if DEPLOYMENT_ENABLE_LIBDISPATCH
224+
func getNextChunk(withLength length: Int) -> _HTTPBodySourceDataChunk {
252225
switch availableChunk {
253226
case .empty:
254227
readNextChunk()
@@ -279,8 +252,5 @@ extension _HTTPBodyFileSource : _HTTPBodySource {
279252
case .done(.none):
280253
return .done
281254
}
282-
#else
283-
fatalError("NSURLSession requires libdispatch")
284-
#endif
285255
}
286256
}

Foundation/NSURLSession/MultiHandle.swift

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
// -----------------------------------------------------------------------------
1919

2020
import CoreFoundation
21-
#if DEPLOYMENT_ENABLE_LIBDISPATCH
2221
import Dispatch
23-
#endif
2422

2523

2624

@@ -38,21 +36,18 @@ extension URLSession {
3836
/// - SeeAlso: URLSessionTask._EasyHandle
3937
internal final class _MultiHandle {
4038
let rawHandle = CFURLSessionMultiHandleInit()
41-
#if DEPLOYMENT_ENABLE_LIBDISPATCH
4239
let queue: DispatchQueue
4340
//let queue = DispatchQueue(label: "MultiHandle.isolation", attributes: .serial)
4441
let group = DispatchGroup()
45-
#endif
4642
fileprivate var easyHandles: [URLSessionTask._EasyHandle] = []
4743
fileprivate var timeoutSource: _TimeoutSource? = nil
48-
#if DEPLOYMENT_ENABLE_LIBDISPATCH
44+
4945
init(configuration: URLSession._Configuration, workQueue: DispatchQueue) {
5046
//queue.setTarget(queue: workQueue)
5147
queue = DispatchQueue(label: "MultiHandle.isolation", target: workQueue)
5248
setupCallbacks()
5349
configure(with: configuration)
5450
}
55-
#endif
5651
deinit {
5752
// C.f.: <https://curl.haxx.se/libcurl/c/curl_multi_cleanup.html>
5853
easyHandles.forEach {
@@ -122,12 +117,10 @@ fileprivate extension URLSession._MultiHandle {
122117
socketSources = nil
123118
}
124119
if let ss = socketSources {
125-
#if DEPLOYMENT_ENABLE_LIBDISPATCH
126120
let handler = DispatchWorkItem { [weak self] in
127121
self?.performAction(for: socket)
128122
}
129123
ss.createSources(with: action, fileDescriptor: Int(socket), queue: queue, handler: handler)
130-
#endif
131124
}
132125
return 0
133126
}
@@ -308,11 +301,8 @@ fileprivate extension URLSession._MultiHandle._SocketRegisterAction {
308301
///
309302
/// Used to implement the timeout of `URLSession.MultiHandle`.
310303
fileprivate class _TimeoutSource {
311-
#if DEPLOYMENT_ENABLE_LIBDISPATCH
312-
let rawSource: DispatchSource
313-
#endif
304+
let rawSource: DispatchSource
314305
let milliseconds: Int
315-
#if DEPLOYMENT_ENABLE_LIBDISPATCH
316306
init(queue: DispatchQueue, milliseconds: Int, handler: DispatchWorkItem) {
317307
self.milliseconds = milliseconds
318308
self.rawSource = DispatchSource.makeTimerSource(queue: queue) as! DispatchSource
@@ -328,17 +318,13 @@ fileprivate class _TimeoutSource {
328318
deinit {
329319
rawSource.cancel()
330320
}
331-
#else
332-
init() { milliseconds = 0 }
333-
#endif
334321
}
335322
fileprivate extension URLSession._MultiHandle {
336323
/// <https://curl.haxx.se/libcurl/c/CURLMOPT_TIMERFUNCTION.html>
337324
func updateTimeoutTimer(to value: Int) {
338325
updateTimeoutTimer(to: _Timeout(timeout: value))
339326
}
340327
func updateTimeoutTimer(to timeout: _Timeout) {
341-
#if DEPLOYMENT_ENABLE_LIBDISPATCH
342328
// Set up a timeout timer based on the given value:
343329
switch timeout {
344330
case .none:
@@ -356,7 +342,6 @@ fileprivate extension URLSession._MultiHandle {
356342
timeoutSource = _TimeoutSource(queue: queue, milliseconds: milliseconds, handler: block)
357343
}
358344
}
359-
#endif
360345
}
361346
enum _Timeout {
362347
case milliseconds(Int)
@@ -387,7 +372,6 @@ fileprivate extension URLSession._MultiHandle._Timeout {
387372
///
388373
/// - SeeAlso: URLSession.MultiHandle.SocketRegisterAction
389374
fileprivate class _SocketSources {
390-
#if DEPLOYMENT_ENABLE_LIBDISPATCH
391375
var readSource: DispatchSource?
392376
var writeSource: DispatchSource?
393377

@@ -417,10 +401,8 @@ fileprivate class _SocketSources {
417401
}
418402
writeSource = nil
419403
}
420-
#endif
421404
}
422405
extension _SocketSources {
423-
#if DEPLOYMENT_ENABLE_LIBDISPATCH
424406
/// Create a read and/or write source as specified by the action.
425407
func createSources(with action: URLSession._MultiHandle._SocketRegisterAction, fileDescriptor fd: Int, queue: DispatchQueue, handler: DispatchWorkItem) {
426408
if action.needsReadSource {
@@ -430,7 +412,6 @@ extension _SocketSources {
430412
createWriteSource(fileDescriptor: fd, queue: queue, handler: handler)
431413
}
432414
}
433-
#endif
434415
}
435416
extension _SocketSources {
436417
/// Unwraps the `SocketSources`

0 commit comments

Comments
 (0)