Skip to content

Commit 4bf1bfd

Browse files
committed
Rename NSURLSession queues
Include unique session ID
1 parent 9d3b855 commit 4bf1bfd

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

Foundation/NSURLSession.swift

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,6 @@
158158
/// timeoutIntervalForRequest even if the request's timeoutInterval has not
159159
/// been set explicitly?
160160
///
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-
///
165161
/// - TODO: We could re-use EasyHandles once they're complete. That'd be a
166162
/// performance optimization. Not sure how much that'd help. The NSURLSession
167163
/// would have to keep a pool of unused handles.
@@ -176,18 +172,22 @@ import CoreFoundation
176172
import Dispatch
177173

178174

179-
175+
private var sessionCounter = Int32(0)
176+
private func nextSessionIdentifier() -> Int32 {
177+
return OSAtomicIncrement32Barrier(&sessionCounter)
178+
}
180179
public let NSURLSessionTransferSizeUnknown: Int64 = -1
181180

182181
public class NSURLSession : NSObject {
183182
private let _configuration: NSURLSession.Configuration
184183
private let multiHandle: MultiHandle
185184
private var nextTaskIdentifier = 1
186-
private let workQueue = dispatch_queue_create("NSURLSession", DISPATCH_QUEUE_SERIAL)
185+
private let workQueue: dispatch_queue_t
187186
/// This queue is used to make public attributes on `NSURLSessionTask` instances thread safe.
188187
/// - 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
190189
private let taskRegistry = NSURLSession.TaskRegistry()
190+
private let identifier: Int32
191191

192192
/*
193193
* The shared session uses the currently set global NSURLCache,
@@ -204,6 +204,9 @@ public class NSURLSession : NSObject {
204204
*/
205205
public /*not inherited*/ init(configuration: NSURLSessionConfiguration) {
206206
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)
207210
self.delegateQueue = NSOperationQueue()
208211
self.delegate = nil
209212
//TODO: Make sure this one can't be written to?
@@ -216,6 +219,9 @@ public class NSURLSession : NSObject {
216219
}
217220
public /*not inherited*/ init(configuration: NSURLSessionConfiguration, delegate: NSURLSessionDelegate?, delegateQueue queue: NSOperationQueue?) {
218221
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)
219225
self.delegateQueue = queue ?? NSOperationQueue()
220226
self.delegate = delegate
221227
//TODO: Make sure this one can't be written to?

0 commit comments

Comments
 (0)