-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d78626
commit 1bc4842
Showing
7 changed files
with
128 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 10 additions & 64 deletions
74
Sources/WalletConnectRelay/WCWebSocketClient/UnfairLock.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,21 @@ | ||
import Foundation | ||
import Combine | ||
|
||
public final class UnfairLock { | ||
@usableFromInline let os_lock: UnsafeMutablePointer<os_unfair_lock> | ||
final class UnfairLock { | ||
private var _lock: UnsafeMutablePointer<os_unfair_lock> | ||
|
||
public init() { | ||
self.os_lock = .allocate(capacity: 1) | ||
os_lock.initialize(to: os_unfair_lock()) | ||
init() { | ||
_lock = UnsafeMutablePointer<os_unfair_lock>.allocate(capacity: 1) | ||
_lock.initialize(to: os_unfair_lock()) | ||
} | ||
|
||
deinit { | ||
os_lock.deallocate() | ||
_lock.deallocate() | ||
} | ||
|
||
@inlinable | ||
@inline(__always) | ||
public func lock() { | ||
os_unfair_lock_lock(os_lock) | ||
} | ||
|
||
@inlinable | ||
@inline(__always) | ||
public func unlock() { | ||
os_unfair_lock_unlock(os_lock) | ||
} | ||
|
||
@discardableResult | ||
@inlinable | ||
@inline(__always) | ||
public func withLock<Result>(body: () throws -> Result) rethrows -> Result { | ||
os_unfair_lock_lock(os_lock) | ||
defer { os_unfair_lock_unlock(os_lock) } | ||
return try body() | ||
} | ||
|
||
@inlinable | ||
@inline(__always) | ||
public func withLock(body: () -> Void) { | ||
os_unfair_lock_lock(os_lock) | ||
defer { os_unfair_lock_unlock(os_lock) } | ||
body() | ||
} | ||
|
||
@inlinable | ||
@inline(__always) | ||
public func assertOwner() { | ||
os_unfair_lock_assert_owner(os_lock) | ||
} | ||
|
||
@inlinable | ||
@inline(__always) | ||
public func assertNotOwner() { | ||
os_unfair_lock_assert_not_owner(os_lock) | ||
} | ||
} | ||
|
||
extension UnfairLock { | ||
private final class LockAssertion: Cancellable { | ||
private var _owner: UnfairLock | ||
|
||
init(owner: UnfairLock) { | ||
self._owner = owner | ||
os_unfair_lock_lock(owner.os_lock) | ||
} | ||
|
||
__consuming func cancel() { | ||
os_unfair_lock_unlock(_owner.os_lock) | ||
} | ||
} | ||
|
||
func acquire() -> Cancellable { | ||
LockAssertion(owner: self) | ||
func locked<ReturnValue>(_ f: () throws -> ReturnValue) rethrows -> ReturnValue { | ||
os_unfair_lock_lock(_lock) | ||
defer { os_unfair_lock_unlock(_lock) } | ||
return try f() | ||
} | ||
} |
Oops, something went wrong.