 
     fileprivate func get(_ file: SwiftLintFile) -> T {
         let key = file.cacheKey
-        lock.lock()
-        defer { lock.unlock() }
-        if let cachedValue = values[key] {
-            return cachedValue
-        }
-        let value = factory(file)
-        values[key] = value
-        return value
+        return lock.doLocked {
+            if let cachedValue = values[key] {
+                return cachedValue
+            }
+            let value = factory(file)
+            values[key] = value
+            return value
+        }
     }
 
     fileprivate func invalidate(_ file: SwiftLintFile) {
-        doLocked { values.removeValue(forKey: file.cacheKey) }
+        lock.doLocked { values.removeValue(forKey: file.cacheKey) }
     }
 
     fileprivate func clear() {
-        doLocked { values.removeAll(keepingCapacity: false) }
+        lock.doLocked { values.removeAll(keepingCapacity: false) }
     }
 
     fileprivate func set(key: FileCacheKey, value: T) {
-        doLocked { values[key] = value }
+        lock.doLocked { values[key] = value }
     }
 
     fileprivate func unset(key: FileCacheKey) {
-        doLocked { values.removeValue(forKey: key) }
-    }
-
-    private func doLocked(block: () -> Void) {
-        lock.lock()
-        block()
-        lock.unlock()
+        lock.doLocked { values.removeValue(forKey: key) }
     }
 }
 

import Foundation
import SourceKittenFramework
import SwiftParser

        commandsCache.clear()
    }
}

private final class PlatformLock {
#if canImport(Darwin)
    private let primitiveLock: UnsafeMutablePointer<os_unfair_lock>
#else
    private let primitiveLock = NSLock()
#endif

    init() {
#if canImport(Darwin)
        primitiveLock = UnsafeMutablePointer<os_unfair_lock>.allocate(capacity: 1)
        primitiveLock.initialize(to: os_unfair_lock())
#endif
    }

    @discardableResult
    func doLocked<U>(_ closure: () -> U) -> U {
#if canImport(Darwin)
        os_unfair_lock_lock(primitiveLock)
        defer { os_unfair_lock_unlock(primitiveLock) }
        return closure()
#else
        primitiveLock.lock()
        defer { primitiveLock.unlock() }
        return closure()
#endif
    }
}
