Skip to content

Commit b697c53

Browse files
itaiferberparkera
authored andcommitted
Update locking mechanisms to match API exposed by Foundation (#545)
1 parent dce4233 commit b697c53

11 files changed

+43
-36
lines changed

Foundation/NSCache.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ open class Cache: NSObject {
2323
}
2424

2525
private var _entries = Dictionary<UnsafeRawPointer, NSCacheEntry>()
26-
private let _lock = Lock()
26+
private let _lock = NSLock()
2727
private var _totalCost = 0
2828
private var _byCost: NSCacheEntry?
2929

Foundation/NSConcreteValue.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ internal class NSConcreteValue : NSValue {
6868
}
6969

7070
private static var _cachedTypeInfo = Dictionary<String, TypeInfo>()
71-
private static var _cachedTypeInfoLock = Lock()
71+
private static var _cachedTypeInfoLock = NSLock()
7272

7373
private var _typeInfo : TypeInfo
7474
private var _storage : UnsafeMutableRawPointer

Foundation/NSData.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ extension NSData {
339339
try self.init(contentsOfFile: url.path!, options: readOptionsMask)
340340
} else {
341341
let session = URLSession(configuration: URLSessionConfiguration.defaultSessionConfiguration())
342-
let cond = Condition()
342+
let cond = NSCondition()
343343
var resError: NSError?
344344
var resData: Data?
345345
let task = session.dataTaskWithURL(url, completionHandler: { (data: Data?, response: URLResponse?, error: NSError?) -> Void in

Foundation/NSKeyedArchiver.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ open class NSKeyedArchiver : NSCoder {
9393
}
9494

9595
private static var _classNameMap = Dictionary<String, String>()
96-
private static var _classNameMapLock = Lock()
96+
private static var _classNameMapLock = NSLock()
9797

9898
private var _stream : AnyObject
9999
private var _flags = ArchiverFlags(rawValue: 0)

Foundation/NSKeyedUnarchiver.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ open class NSKeyedUnarchiver : NSCoder {
3232
}
3333

3434
private static var _classNameMap : Dictionary<String, AnyClass> = [:]
35-
private static var _classNameMapLock = Lock()
35+
private static var _classNameMapLock = NSLock()
3636

3737
open weak var delegate: NSKeyedUnarchiverDelegate?
3838

Foundation/NSLock.swift

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ import Glibc
1616

1717
import CoreFoundation
1818

19-
public protocol Locking {
20-
19+
public protocol NSLocking {
2120
func lock()
2221
func unlock()
2322
}
2423

25-
open class Lock: NSObject, Locking {
24+
open class NSLock: NSObject, NSLocking {
2625
internal var mutex = UnsafeMutablePointer<pthread_mutex_t>.allocate(capacity: 1)
2726

2827
public override init() {
@@ -43,23 +42,27 @@ open class Lock: NSObject, Locking {
4342
pthread_mutex_unlock(mutex)
4443
}
4544

46-
open func tryLock() -> Bool {
45+
open func `try`() -> Bool {
4746
return pthread_mutex_trylock(mutex) == 0
4847
}
4948

49+
open func lock(before limit: Date) {
50+
NSUnimplemented()
51+
}
52+
5053
open var name: String?
5154
}
5255

53-
extension Lock {
56+
extension NSLock {
5457
internal func synchronized<T>(_ closure: () -> T) -> T {
5558
self.lock()
5659
defer { self.unlock() }
5760
return closure()
5861
}
5962
}
6063

61-
open class NSConditionLock : NSObject, Locking {
62-
internal var _cond = Condition()
64+
open class NSConditionLock : NSObject, NSLocking {
65+
internal var _cond = NSCondition()
6366
internal var _value: Int
6467
internal var _thread: pthread_t?
6568

@@ -72,7 +75,7 @@ open class NSConditionLock : NSObject, Locking {
7275
}
7376

7477
open func lock() {
75-
let _ = lockBeforeDate(Date.distantFuture)
78+
let _ = lock(before: Date.distantFuture)
7679
}
7780

7881
open func unlock() {
@@ -86,30 +89,30 @@ open class NSConditionLock : NSObject, Locking {
8689
return _value
8790
}
8891

89-
open func lockWhenCondition(_ condition: Int) {
90-
let _ = lockWhenCondition(condition, beforeDate: Date.distantFuture)
92+
open func lock(whenCondition condition: Int) {
93+
let _ = lock(whenCondition: condition, before: Date.distantFuture)
9194
}
9295

93-
open func tryLock() -> Bool {
94-
return lockBeforeDate(Date.distantPast)
96+
open func `try`() -> Bool {
97+
return lock(before: Date.distantPast)
9598
}
9699

97-
open func tryLockWhenCondition(_ condition: Int) -> Bool {
98-
return lockWhenCondition(condition, beforeDate: Date.distantPast)
100+
open func tryLock(whenCondition condition: Int) -> Bool {
101+
return lock(whenCondition: condition, before: Date.distantPast)
99102
}
100103

101-
open func unlockWithCondition(_ condition: Int) {
104+
open func unlock(withCondition condition: Int) {
102105
_cond.lock()
103106
_thread = nil
104107
_value = condition
105108
_cond.broadcast()
106109
_cond.unlock()
107110
}
108111

109-
open func lockBeforeDate(_ limit: Date) -> Bool {
112+
open func lock(before limit: Date) -> Bool {
110113
_cond.lock()
111114
while _thread == nil {
112-
if !_cond.waitUntilDate(limit) {
115+
if !_cond.wait(until: limit) {
113116
_cond.unlock()
114117
return false
115118
}
@@ -119,10 +122,10 @@ open class NSConditionLock : NSObject, Locking {
119122
return true
120123
}
121124

122-
open func lockWhenCondition(_ condition: Int, beforeDate limit: Date) -> Bool {
125+
open func lock(whenCondition condition: Int, before limit: Date) -> Bool {
123126
_cond.lock()
124127
while _thread != nil || _value != condition {
125-
if !_cond.waitUntilDate(limit) {
128+
if !_cond.wait(until: limit) {
126129
_cond.unlock()
127130
return false
128131
}
@@ -135,7 +138,7 @@ open class NSConditionLock : NSObject, Locking {
135138
open var name: String?
136139
}
137140

138-
open class RecursiveLock: NSObject, Locking {
141+
open class NSRecursiveLock: NSObject, NSLocking {
139142
internal var mutex = UnsafeMutablePointer<pthread_mutex_t>.allocate(capacity: 1)
140143

141144
public override init() {
@@ -161,14 +164,18 @@ open class RecursiveLock: NSObject, Locking {
161164
pthread_mutex_unlock(mutex)
162165
}
163166

164-
open func tryLock() -> Bool {
167+
open func `try`() -> Bool {
165168
return pthread_mutex_trylock(mutex) == 0
166169
}
170+
171+
open func lock(before limit: Date) {
172+
NSUnimplemented()
173+
}
167174

168175
open var name: String?
169176
}
170177

171-
open class Condition: NSObject, Locking {
178+
open class NSCondition: NSObject, NSLocking {
172179
internal var mutex = UnsafeMutablePointer<pthread_mutex_t>.allocate(capacity: 1)
173180
internal var cond = UnsafeMutablePointer<pthread_cond_t>.allocate(capacity: 1)
174181

@@ -198,7 +205,7 @@ open class Condition: NSObject, Locking {
198205
pthread_cond_wait(cond, mutex)
199206
}
200207

201-
open func waitUntilDate(_ limit: Date) -> Bool {
208+
open func wait(until limit: Date) -> Bool {
202209
let lim = limit.timeIntervalSinceReferenceDate
203210
let ti = lim - CFAbsoluteTimeGetCurrent()
204211
if ti < 0.0 {

Foundation/NSNotification.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private let _defaultCenter: NotificationCenter = NotificationCenter()
159159
open class NotificationCenter: NSObject {
160160

161161
private var _observers: [NSNotificationReceiver]
162-
private let _observersLock = Lock()
162+
private let _observersLock = NSLock()
163163

164164
public required override init() {
165165
_observers = [NSNotificationReceiver]()

Foundation/NSOperation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private func pthread_main_np() -> Int32 {
1818
#endif
1919

2020
open class Operation: NSObject {
21-
let lock = Lock()
21+
let lock = NSLock()
2222
internal weak var _queue: OperationQueue?
2323
internal var _cancelled = false
2424
internal var _executing = false
@@ -294,7 +294,7 @@ internal struct _OperationList {
294294
}
295295

296296
open class OperationQueue: NSObject {
297-
let lock = Lock()
297+
let lock = NSLock()
298298
#if DEPLOYMENT_ENABLE_LIBDISPATCH
299299
var __concurrencyGate: DispatchSemaphore?
300300
var __underlyingQueue: DispatchQueue?

Foundation/NSTask.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private func WEXITSTATUS(_ status: CInt) -> CInt {
2828

2929
private var managerThreadRunLoop : RunLoop? = nil
3030
private var managerThreadRunLoopIsRunning = false
31-
private var managerThreadRunLoopIsRunningCondition = Condition()
31+
private var managerThreadRunLoopIsRunningCondition = NSCondition()
3232

3333
#if os(OSX) || os(iOS)
3434
internal let kCFSocketDataCallBack = CFSocketCallBackType.dataCallBack.rawValue
@@ -95,7 +95,7 @@ open class Task: NSObject {
9595
private static func setup() {
9696
struct Once {
9797
static var done = false
98-
static let lock = Lock()
98+
static let lock = NSLock()
9999
}
100100
Once.lock.synchronized {
101101
if !Once.done {
@@ -179,7 +179,7 @@ open class Task: NSObject {
179179

180180
fileprivate weak var runLoop : RunLoop? = nil
181181

182-
private var processLaunchedCondition = Condition()
182+
private var processLaunchedCondition = NSCondition()
183183

184184
// actions
185185
open func launch() {

Foundation/NSValue.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
open class NSValue : NSObject, NSCopying, NSSecureCoding, NSCoding {
1111

1212
private static var SideTable = [ObjectIdentifier : NSValue]()
13-
private static var SideTableLock = Lock()
13+
private static var SideTableLock = NSLock()
1414

1515
internal override init() {
1616
super.init()

TestFoundation/TestNSThread.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class TestNSThread : XCTestCase {
3535

3636
func test_threadStart() {
3737
var started = false
38-
let condition = Condition()
38+
let condition = NSCondition()
3939
let thread = Thread() {
4040
condition.lock()
4141
started = true

0 commit comments

Comments
 (0)