mirror of
https://github.com/apple/swift-log
synced 2026-11-22 03:40:39 +00:00
Ports changes to `Lock` from swift-nio to swift-log. ### Motivation: The copy-paste of `Lock` in swift-log should be updated with changes from swift-nio. ### Modifications: - Rolls back changed [made previously for FreeBSD](https://github.com/apple/swift-log/pull/387/changes#diff-7b1bd45403dd1a7418287bd60682bf05ba2e4bf75dca1c0ca55a86d0477c9af5L73) in favor of a consolidated implementation in swift-nio that will be in place once https://github.com/apple/swift-nio/pull/3494 merges. @kkebo Please confirm this works. After both of our PR's merge, swift-log and swift-nio will have the same implementation as far as FreeBSD is concerned. - Brought the following recent changes from swift-nio over to swift-log: - https://github.com/apple/swift-nio/pull/3482 - https://github.com/apple/swift-nio/pull/3480 - https://github.com/apple/swift-nio/pull/3483 - Removed stale `// SRWLOCK does not need to be free'd` comment. ### Result: `Lock` implementation will have the same implementation as swift-nio (assuming https://github.com/apple/swift-nio/pull/3494 merges).
75 lines
2.0 KiB
Swift
75 lines
2.0 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Logging API open source project
|
|
//
|
|
// Copyright (c) 2018-2026 Apple Inc. and the Swift Logging API project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of Swift Logging API project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2017-2026 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import Testing
|
|
|
|
@testable import Logging
|
|
|
|
#if canImport(Dispatch)
|
|
import Dispatch
|
|
private let canImportDispatch = true
|
|
#else
|
|
private let canImportDispatch = false
|
|
#endif
|
|
|
|
@Suite("Lock Test Suite")
|
|
struct LockTestSuite {
|
|
@Test(.enabled(if: canImportDispatch)) func testLockMutualExclusion() {
|
|
#if canImport(Dispatch)
|
|
let l = Lock()
|
|
|
|
nonisolated(unsafe) var x = 1
|
|
let q = DispatchQueue(label: "q")
|
|
let g = DispatchGroup()
|
|
let sem1 = DispatchSemaphore(value: 0)
|
|
let sem2 = DispatchSemaphore(value: 0)
|
|
|
|
l.lock()
|
|
|
|
q.async(group: g) {
|
|
sem1.signal()
|
|
l.lock()
|
|
x = 2
|
|
l.unlock()
|
|
sem2.signal()
|
|
}
|
|
|
|
sem1.wait()
|
|
#expect(DispatchTimeoutResult.timedOut == g.wait(timeout: .now() + 0.1))
|
|
#expect(1 == x)
|
|
|
|
l.unlock()
|
|
sem2.wait()
|
|
|
|
l.lock()
|
|
#expect(2 == x)
|
|
l.unlock()
|
|
#endif // canImport(Dispatch)
|
|
}
|
|
}
|