Skip to content

Commit b2248f9

Browse files
authored
address Lock deprecationwarnings (#340)
motivation: we recently deprecated TSCBasic::Lock changes: * use NSLock instead of TSCBasic::Lock * add internal extension as necessary for a locked block
1 parent f0938b6 commit b2248f9

File tree

8 files changed

+35
-14
lines changed

8 files changed

+35
-14
lines changed

Sources/TSCBasic/FileSystem.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,11 +609,11 @@ public class InMemoryFileSystem: FileSystem {
609609
/// FIXME: Using a single lock for this is a performance problem, but in
610610
/// reality, the only practical use for InMemoryFileSystem is for unit
611611
/// tests.
612-
private let lock = Lock()
612+
private let lock = NSLock()
613613
/// A map that keeps weak references to all locked files.
614614
private var lockFiles = Dictionary<AbsolutePath, WeakReference<DispatchQueue>>()
615615
/// Used to access lockFiles in a thread safe manner.
616-
private let lockFilesLock = Lock()
616+
private let lockFilesLock = NSLock()
617617

618618
/// Exclusive file system lock vended to clients through `withLock()`.
619619
// Used to ensure that DispatchQueues are releassed when they are no longer in use.

Sources/TSCBasic/LazyCache.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11+
import class Foundation.NSLock
12+
1113
// FIXME: This wrapper could benefit from local static variables, in which case
1214
// we could embed the cache object inside the accessor.
1315
//
@@ -32,7 +34,7 @@ public struct LazyCache<Class, T> {
3234
// FIXME: It would be nice to avoid a per-instance lock, but this type isn't
3335
// intended for creating large numbers of instances of. We also really want
3436
// a reader-writer lock or something similar here.
35-
private var lock = Lock()
37+
private var lock = NSLock()
3638
let body: (Class) -> () -> T
3739
var cachedValue: T?
3840

Sources/TSCBasic/Lock.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ public struct Lock {
3636
}
3737
}
3838

39+
// for internal usage
40+
extension NSLock {
41+
internal func withLock<T> (_ body: () throws -> T) rethrows -> T {
42+
self.lock()
43+
defer { self.unlock() }
44+
return try body()
45+
}
46+
}
47+
3948
enum ProcessLockError: Error {
4049
case unableToAquireLock(errno: Int32)
4150
}

Sources/TSCBasic/Process.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11-
import class Foundation.ProcessInfo
1211
import protocol Foundation.CustomNSError
1312
import var Foundation.NSLocalizedDescriptionKey
13+
import class Foundation.NSLock
14+
import class Foundation.ProcessInfo
1415

1516
#if os(Windows)
1617
import Foundation
@@ -208,7 +209,7 @@ public final class Process {
208209
public typealias LoggingHandler = (String) -> Void
209210

210211
private static var _loggingHandler: LoggingHandler?
211-
private static let loggingHandlerLock = Lock()
212+
private static let loggingHandlerLock = NSLock()
212213

213214
/// Global logging handler. Use with care! preferably use instance level instead of setting one globally.
214215
public static var loggingHandler: LoggingHandler? {
@@ -237,7 +238,7 @@ public final class Process {
237238

238239
// the log and setter are only required to backward support verbose setter.
239240
// remove and make loggingHandler a let property once verbose is deprecated
240-
private let loggingHandlerLock = Lock()
241+
private let loggingHandlerLock = NSLock()
241242
public private(set) var loggingHandler: LoggingHandler? {
242243
get {
243244
self.loggingHandlerLock.withLock {
@@ -290,7 +291,7 @@ public final class Process {
290291

291292
// process execution mutable state
292293
private var state: State = .idle
293-
private let stateLock = Lock()
294+
private let stateLock = NSLock()
294295

295296
private static let sharedCompletionQueue = DispatchQueue(label: "org.swift.tools-support-core.process-completion")
296297
private var completionQueue = Process.sharedCompletionQueue
@@ -311,7 +312,7 @@ public final class Process {
311312

312313
// ideally we would use the state for this, but we need to access it while the waitForExit is locking state
313314
private var _launched = false
314-
private let launchedLock = Lock()
315+
private let launchedLock = NSLock()
315316

316317
public var launched: Bool {
317318
return self.launchedLock.withLock {
@@ -330,7 +331,7 @@ public final class Process {
330331
/// Key: Executable name or path.
331332
/// Value: Path to the executable, if found.
332333
private static var validatedExecutablesMap = [String: AbsolutePath?]()
333-
private static let validatedExecutablesMapLock = Lock()
334+
private static let validatedExecutablesMapLock = NSLock()
334335

335336
/// Create a new process instance.
336337
///
@@ -727,7 +728,7 @@ public final class Process {
727728
}
728729
} else {
729730
var pending: Result<[UInt8], Swift.Error>?
730-
let pendingLock = Lock()
731+
let pendingLock = NSLock()
731732

732733
let outputClosures = outputRedirection.outputClosures
733734

Sources/TSCUtility/Platform.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public enum Platform: Equatable {
2828
public static var currentPlatform = Platform.findCurrentPlatform(localFileSystem)
2929

3030
/// Returns the cache directories used in Darwin.
31-
private static var darwinCacheDirectoriesLock = Lock()
31+
private static var darwinCacheDirectoriesLock = NSLock()
3232
private static var _darwinCacheDirectories: [AbsolutePath]? = .none
3333

3434
/// Attempt to match `uname` with recognized platforms.

Sources/TSCUtility/misc.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,12 @@ public func measure<T>(_ label: String = "", _ f: () throws -> (T)) rethrows ->
4242
print("\(label): Time taken", endTime)
4343
return result
4444
}
45+
46+
// for internal usage
47+
extension NSLock {
48+
internal func withLock<T> (_ body: () throws -> T) rethrows -> T {
49+
self.lock()
50+
defer { self.unlock() }
51+
return try body()
52+
}
53+
}

Tests/TSCBasicTests/LockTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import TSCBasic
1414
import TSCTestSupport
1515

1616
class LockTests: XCTestCase {
17+
@available(*, deprecated)
1718
func testBasics() {
1819
// FIXME: Make this a more interesting test once we have concurrency primitives.
1920
let lock = TSCBasic.Lock()

Tests/TSCBasicTests/SynchronizedQueueTests.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11+
@testable import TSCBasic
1112
import XCTest
1213

13-
import TSCBasic
14-
1514
class SyncronizedQueueTests: XCTestCase {
1615
func testSingleProducerConsumer() {
1716
let queue = SynchronizedQueue<Int?>()
@@ -46,7 +45,7 @@ class SyncronizedQueueTests: XCTestCase {
4645
let queueElementsTwo = Set(100..<500)
4746

4847
var consumed = Set<Int>()
49-
let consumedLock = TSCBasic.Lock()
48+
let consumedLock = NSLock()
5049

5150
// Create two producers.
5251
let producers = [queueElementsOne, queueElementsTwo].map { queueElements in

0 commit comments

Comments
 (0)