Skip to content

Commit 47b222e

Browse files
committed
Priv
1 parent a289603 commit 47b222e

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

Sources/LiveKit/Support/Locks.swift

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,17 @@ func createLock() -> some LockType {
3535
return UnfairLock()
3636
}
3737

38-
// MARK: - Unfair lock
39-
40-
//
41-
// Read http://www.russbishop.net/the-law for more information on why this is necessary
42-
//
43-
final class UnfairLock: LockType {
44-
private var _lock: UnsafeMutablePointer<os_unfair_lock>
45-
46-
fileprivate init() {
47-
_lock = UnsafeMutablePointer<os_unfair_lock>.allocate(capacity: 1)
48-
_lock.initialize(to: os_unfair_lock())
49-
}
38+
// MARK: - Mutex
5039

51-
deinit {
52-
_lock.deinitialize(count: 1)
53-
_lock.deallocate()
54-
}
40+
@available(iOS 18.0, macOS 15.0, tvOS 18.0, visionOS 2.0, *)
41+
private final class MutexWrapper: LockType {
42+
private let mutex = Mutex(())
5543

5644
@inline(__always)
5745
func sync<Result>(_ fnc: () throws -> Result) rethrows -> Result {
58-
os_unfair_lock_lock(_lock)
59-
defer { os_unfair_lock_unlock(_lock) }
60-
return try fnc()
46+
try mutex.withLock { _ in
47+
try fnc()
48+
}
6149
}
6250
}
6351

@@ -71,16 +59,28 @@ extension OSAllocatedUnfairLock: LockType where State == Void {
7159
}
7260
}
7361

74-
// MARK: - Mutex
62+
// MARK: - Unfair lock
7563

76-
@available(iOS 18.0, macOS 15.0, tvOS 18.0, visionOS 2.0, *)
77-
private final class MutexWrapper: LockType {
78-
private let mutex = Mutex(())
64+
//
65+
// Read http://www.russbishop.net/the-law for more information on why this is necessary
66+
//
67+
private final class UnfairLock: LockType {
68+
private let _lock: UnsafeMutablePointer<os_unfair_lock>
69+
70+
init() {
71+
_lock = UnsafeMutablePointer<os_unfair_lock>.allocate(capacity: 1)
72+
_lock.initialize(to: os_unfair_lock())
73+
}
74+
75+
deinit {
76+
_lock.deinitialize(count: 1)
77+
_lock.deallocate()
78+
}
7979

8080
@inline(__always)
8181
func sync<Result>(_ fnc: () throws -> Result) rethrows -> Result {
82-
try mutex.withLock { _ in
83-
try fnc()
84-
}
82+
os_unfair_lock_lock(_lock)
83+
defer { os_unfair_lock_unlock(_lock) }
84+
return try fnc()
8585
}
8686
}

0 commit comments

Comments
 (0)