Skip to content

Commit daf756a

Browse files
committed
@inlineable
1 parent 3f5543d commit daf756a

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

Sources/AllocatedLock.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@
3737
@available(macOS, deprecated: 13.0, message: "use OSAllocatedUnfairLock directly")
3838
public struct AllocatedLock<State> {
3939

40-
private let storage: Storage
40+
@usableFromInline
41+
let storage: Storage
4142

4243
public init(initialState: State) {
4344
self.storage = Storage(initialState: initialState)
4445
}
4546

47+
@inlinable
4648
public func withLock<R>(_ body: @Sendable (inout State) throws -> R) rethrows -> R where R: Sendable {
4749
storage.lock()
4850
defer { storage.unlock() }
@@ -56,14 +58,17 @@ public extension AllocatedLock where State == Void {
5658
self.storage = Storage(initialState: ())
5759
}
5860

61+
@inlinable
5962
func lock() {
6063
storage.lock()
6164
}
6265

66+
@inlinable
6367
func unlock() {
6468
storage.unlock()
6569
}
6670

71+
@inlinable
6772
func withLock<R>(_ body: @Sendable () throws -> R) rethrows -> R where R: Sendable {
6873
storage.lock()
6974
defer { storage.unlock() }
@@ -74,9 +79,12 @@ public extension AllocatedLock where State == Void {
7479
#if canImport(Darwin)
7580
@_implementationOnly import os
7681

77-
private extension AllocatedLock {
82+
extension AllocatedLock {
83+
@usableFromInline
7884
final class Storage {
7985
private let _lock: os_unfair_lock_t
86+
87+
@usableFromInline
8088
var state: State
8189

8290
init(initialState: State) {
@@ -85,10 +93,12 @@ private extension AllocatedLock {
8593
self.state = initialState
8694
}
8795

96+
@usableFromInline
8897
func lock() {
8998
os_unfair_lock_lock(_lock)
9099
}
91100

101+
@usableFromInline
92102
func unlock() {
93103
os_unfair_lock_unlock(_lock)
94104
}
@@ -103,10 +113,12 @@ private extension AllocatedLock {
103113
#elseif canImport(Glibc)
104114
@_implementationOnly import Glibc
105115

106-
private extension AllocatedLock {
116+
extension AllocatedLock {
117+
@usableFromInline
107118
final class Storage {
108119
private let _lock: UnsafeMutablePointer<pthread_mutex_t>
109120

121+
@usableFromInline
110122
var state: State
111123

112124
init(initialState: State) {
@@ -118,11 +130,13 @@ private extension AllocatedLock {
118130
self.state = initialState
119131
}
120132

133+
@usableFromInline
121134
func lock() {
122135
let err = pthread_mutex_lock(_lock)
123136
precondition(err == 0, "pthread_mutex_lock error: \(err)")
124137
}
125138

139+
@usableFromInline
126140
func unlock() {
127141
let err = pthread_mutex_unlock(_lock)
128142
precondition(err == 0, "pthread_mutex_unlock error: \(err)")

Tests/AllocatedLockTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ final class AllocatedLockTests: XCTestCase {
5050
XCTAssertEqual(total, 50005000)
5151
}
5252

53+
func testLock_ReturnsValue() async {
54+
let lock = AllocatedLock()
55+
let value = lock.withLock { true }
56+
XCTAssertTrue(value)
57+
}
58+
5359
@MainActor
5460
func testLock_Blocks() async {
5561
let lock = AllocatedLock()

0 commit comments

Comments
 (0)