-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clean up
- Loading branch information
Showing
5 changed files
with
67 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// SynchronizedLock.swift | ||
// Utilities | ||
// | ||
// Created by Michael Maguire on 5/2/24. | ||
// Copyright © 2024 dYdX Trading Inc. All rights reserved. | ||
// | ||
|
||
|
||
import Darwin | ||
|
||
@propertyWrapper | ||
public struct SynchronizedLock<Value> { | ||
private var value: Value | ||
private var lock = NSLock() | ||
|
||
public var wrappedValue: Value { | ||
get { lock.synchronized { value } } | ||
set { lock.synchronized { value = newValue } } | ||
} | ||
|
||
public init(wrappedValue value: Value) { | ||
self.value = value | ||
} | ||
} | ||
|
||
private extension NSLock { | ||
|
||
@discardableResult | ||
func synchronized<T>(_ block: () -> T) -> T { | ||
lock() | ||
defer { unlock() } | ||
return block() | ||
} | ||
} |
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