158
158
/// timeoutIntervalForRequest even if the request's timeoutInterval has not
159
159
/// been set explicitly?
160
160
///
161
- /// - TODO: The queue names are not unique. We might want to use an atomic
162
- /// counter to give each NSURLSession a unique identifier and add that. Or use
163
- /// the memory address.
164
- ///
165
161
/// - TODO: We could re-use EasyHandles once they're complete. That'd be a
166
162
/// performance optimization. Not sure how much that'd help. The NSURLSession
167
163
/// would have to keep a pool of unused handles.
@@ -176,18 +172,22 @@ import CoreFoundation
176
172
import Dispatch
177
173
178
174
179
-
175
+ private var sessionCounter = Int32 ( 0 )
176
+ private func nextSessionIdentifier( ) -> Int32 {
177
+ return OSAtomicIncrement32Barrier ( & sessionCounter)
178
+ }
180
179
public let NSURLSessionTransferSizeUnknown : Int64 = - 1
181
180
182
181
public class NSURLSession : NSObject {
183
182
private let _configuration : NSURLSession . Configuration
184
183
private let multiHandle : MultiHandle
185
184
private var nextTaskIdentifier = 1
186
- private let workQueue = dispatch_queue_create ( " NSURLSession " , DISPATCH_QUEUE_SERIAL )
185
+ private let workQueue : dispatch_queue_t
187
186
/// This queue is used to make public attributes on `NSURLSessionTask` instances thread safe.
188
187
/// - Note: It's a **concurrent** queue.
189
- private let taskAttributesIsolation = dispatch_queue_create ( " NSURLSession.taskAttributes " , DISPATCH_QUEUE_CONCURRENT )
188
+ private let taskAttributesIsolation : dispatch_queue_t
190
189
private let taskRegistry = NSURLSession . TaskRegistry ( )
190
+ private let identifier : Int32
191
191
192
192
/*
193
193
* The shared session uses the currently set global NSURLCache,
@@ -204,6 +204,9 @@ public class NSURLSession : NSObject {
204
204
*/
205
205
public /*not inherited*/ init ( configuration: NSURLSessionConfiguration ) {
206
206
initializeLibcurl ( )
207
+ identifier = nextSessionIdentifier ( )
208
+ self . workQueue = dispatch_queue_create ( " NSURLSession< \( identifier) > " , DISPATCH_QUEUE_SERIAL)
209
+ self . taskAttributesIsolation = dispatch_queue_create ( " NSURLSession< \( identifier) >.taskAttributes " , DISPATCH_QUEUE_CONCURRENT)
207
210
self . delegateQueue = NSOperationQueue ( )
208
211
self . delegate = nil
209
212
//TODO: Make sure this one can't be written to?
@@ -216,6 +219,9 @@ public class NSURLSession : NSObject {
216
219
}
217
220
public /*not inherited*/ init ( configuration: NSURLSessionConfiguration , delegate: NSURLSessionDelegate ? , delegateQueue queue: NSOperationQueue ? ) {
218
221
initializeLibcurl ( )
222
+ identifier = nextSessionIdentifier ( )
223
+ self . workQueue = dispatch_queue_create ( " NSURLSession< \( identifier) > " , DISPATCH_QUEUE_SERIAL)
224
+ self . taskAttributesIsolation = dispatch_queue_create ( " NSURLSession< \( identifier) >.taskAttributes " , DISPATCH_QUEUE_CONCURRENT)
219
225
self . delegateQueue = queue ?? NSOperationQueue ( )
220
226
self . delegate = delegate
221
227
//TODO: Make sure this one can't be written to?
0 commit comments