1

我正在尝试使用 pthread API 在 Swift 中实现读/写锁,但遇到了一个奇怪的问题。

我的实现主要基于以下内容,并增加了尝试读锁的超时时间。

http://swiftweb.johnholdsworth.com/Deferred/html/ReadWriteLock.html

这是我的实现:

 public final class ReadWriteLock {

        private var lock = pthread_rwlock_t()

        public init() {
            let status = pthread_rwlock_init(&lock, nil)
            assert(status == 0)
        }

        deinit {
            let status = pthread_rwlock_destroy(&lock)
            assert(status == 0)
        }

        @discardableResult
        public func withReadLock<Result>(_ body: () throws -> Result) rethrows -> Result {
            pthread_rwlock_rdlock(&lock)
            defer { pthread_rwlock_unlock(&lock) }
            return try body()
        }

        @discardableResult
        public func withAttemptedReadLock<Result>(_ body: () throws -> Result) rethrows -> Result? {
            guard pthread_rwlock_tryrdlock(&lock) == 0 else { return nil }
            defer { pthread_rwlock_unlock(&lock) }
            return try body()
        }

        @discardableResult
        public func withAttemptedReadLock<Result>(_ timeout: Timeout = .now, body: () throws -> Result) rethrows -> Result? {
            guard timeout != .now else { return try withAttemptedReadLock(body) }

            let expiry = DispatchTime.now().uptimeNanoseconds + timeout.rawValue.uptimeNanoseconds
            var ts = Timeout.interval(1).timespec
            var result: Int32
            repeat {
                result = pthread_rwlock_tryrdlock(&lock)
                guard result != 0 else { break }
                nanosleep(&ts, nil)
            } while DispatchTime.now().uptimeNanoseconds < expiry

            // If the lock was not acquired
            if result != 0 {
                // Try to grab the lock once more
                result = pthread_rwlock_tryrdlock(&lock)
            }
            guard result == 0 else { return nil }
            defer { pthread_rwlock_unlock(&lock) }
            return try body()
        }

        @discardableResult
        public func withWriteLock<Return>(_ body: () throws -> Return) rethrows -> Return {
            pthread_rwlock_wrlock(&lock)
            defer { pthread_rwlock_unlock(&lock) }
            return try body()
        }
    }

/// An amount of time to wait for an event.
public enum Timeout {
    /// Do not wait at all.
    case now
    /// Wait indefinitely.
    case forever
    /// Wait for a given number of seconds.
    case interval(UInt64)
}

public extension Timeout {

    public var timespec: timespec {
        let nano = rawValue.uptimeNanoseconds
        return Darwin.timespec(tv_sec: Int(nano / NSEC_PER_SEC), tv_nsec: Int(nano % NSEC_PER_SEC))
    }

    public var rawValue: DispatchTime {
        switch self {
            case .now:
                return DispatchTime.now()
            case .forever:
                return DispatchTime.distantFuture
            case .interval(let milliseconds):
                return DispatchTime(uptimeNanoseconds: milliseconds * NSEC_PER_MSEC)
        }
    }
}

extension Timeout : Equatable { }

public func ==(lhs: Timeout, rhs: Timeout) -> Bool {
    switch (lhs, rhs) {
        case (.now, .now):
            return true
        case (.forever, .forever):
            return true
        case (let .interval(ms1), let .interval(ms2)):
            return ms1 == ms2
        default:
            return false
    }
}

这是我的单元测试:

func testReadWrite() {
    let rwLock = PThreadReadWriteLock()
    let queue = OperationQueue()
    queue.maxConcurrentOperationCount = 2
    queue.qualityOfService = .userInteractive
    queue.isSuspended = true

    var enterWrite: Double = 0
    var exitWrite: Double = 0
    let writeWait: UInt64 = 500
    // Get write lock
    queue.addOperation {
        enterWrite = Double(Timeout.now.rawValue.uptimeNanoseconds) / Double(NSEC_PER_MSEC)
        rwLock.withWriteLock {
            // Sleep for 1 second
            var ts = Timeout.interval(writeWait).timespec
            var result: Int32
            repeat { result = nanosleep(&ts, &ts) } while result == -1
        }
        exitWrite = Double(Timeout.now.rawValue.uptimeNanoseconds) / Double(NSEC_PER_MSEC)
    }

    var entered = false
    var enterRead: Double = 0
    var exitRead: Double = 0
    let readWait = writeWait + 50
    // Get read lock
    queue.addOperation {
        enterRead = Double(Timeout.now.rawValue.uptimeNanoseconds) / Double(NSEC_PER_MSEC)
        rwLock.withAttemptedReadLock(.interval(readWait)) {
            print("**** Entered! ****")
            entered = true
        }
        exitRead = Double(Timeout.now.rawValue.uptimeNanoseconds) / Double(NSEC_PER_MSEC)
    }

    queue.isSuspended = false
    queue.waitUntilAllOperationsAreFinished()

    let startDifference = abs(enterWrite - enterRead)
    let totalWriteTime = abs(exitWrite - enterWrite)
    let totalReadTime = abs(exitRead - enterRead)
    print("Start Difference: \(startDifference)")
    print("Total Write Time: \(totalWriteTime)")
    print("Total Read Time: \(totalReadTime)")

    XCTAssert(totalWriteTime >= Double(writeWait))
    XCTAssert(totalReadTime >= Double(readWait))
    XCTAssert(totalReadTime >= totalWriteTime)
    XCTAssert(entered)
}

最后,我的单元测试的输出如下:

Start Difference: 0.00136399269104004
Total Write Time: 571.76081609726
Total Read Time: 554.105705976486

当然,测试失败是因为写锁没有及时释放。鉴于我的等待时间只有半秒(500 毫秒),为什么写锁执行和释放大约需要 570 毫秒?

我尝试过开启和关闭优化都无济于事。

我的印象nanosleep是高分辨率睡眠定时器,我希望这里的锁定超时分辨率至少为 5-10 毫秒。

任何人都可以在这里阐明一下吗?

4

1 回答 1

2

OperationQueue事实证明,由于我的单元测试中的长时间睡眠,基础正在执行某种优化。

用 1ms 睡眠替换睡眠功能usleep并迭代,直到超过总时间似乎已经解决了这个问题。

    // Get write lock
    queue.addOperation {
        enterWrite = Double(Timeout.now.rawValue.uptimeNanoseconds) / Double(NSEC_PER_MSEC)
        rwLock.withWriteLock {
            let expiry = DispatchTime.now().uptimeNanoseconds + Timeout.interval(writeWait).rawValue.uptimeNanoseconds
            let interval = Timeout.interval(1)
            repeat {
                interval.sleep()
            } while DispatchTime.now().uptimeNanoseconds < expiry
        }
        exitWrite = Double(Timeout.now.rawValue.uptimeNanoseconds) / Double(NSEC_PER_MSEC)
    }
于 2016-11-24T02:48:26.160 回答