Skip to content

Commit 86ed0eb

Browse files
ref: Swift NSLock return value (#3741)
Use generics to allow a return value for NSLock synchronized.
1 parent 2de284c commit 86ed0eb

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

Sources/Swift/Extensions/NSLock.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ extension NSLock {
55
/// Executes the closure while acquiring the lock.
66
///
77
/// - Parameter closure: The closure to run.
8-
func synchronized(_ closure: () throws -> Void) rethrows {
9-
self.lock()
8+
///
9+
/// - Returns: The value the closure generated.
10+
func synchronized<T>(_ closure: () throws -> T) rethrows -> T {
1011
defer { self.unlock() }
11-
try closure()
12+
self.lock()
13+
return try closure()
1214
}
1315
}

Tests/SentryTests/Swift/Extensions/NSLockTests.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ final class NSLockTests: XCTestCase {
1010
var value = 0
1111

1212
testConcurrentModifications(asyncWorkItems: 10, writeLoopCount: 9, writeWork: { _ in
13+
let returnValue: Int = lock.synchronized {
14+
value += 1
15+
return 10
16+
}
17+
expect(returnValue) == 10
18+
1319
lock.synchronized {
1420
value += 1
1521
}
1622
})
1723

18-
expect(value) == 100
24+
expect(value) == 200
1925
}
2026

2127
func testUnlockWhenThrowing() throws {

0 commit comments

Comments
 (0)