-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
a045e59
commit f94a7c3
Showing
5 changed files
with
77 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,33 @@ | ||
public final class AsyncRecursiveLock { | ||
@TaskLocal private static var lockCount = 0 | ||
@TaskLocal private static var locked = false | ||
|
||
private let internalLock = AsyncLock() | ||
|
||
public init() { | ||
} | ||
|
||
public func lock(isolation: isolated (any Actor)? = #isolation) async { | ||
// precondition(lockCount >= 0) | ||
// | ||
// lockCount += 1 | ||
// | ||
// if lockCount == 1 { | ||
// await internalLock.lock() | ||
// } | ||
} | ||
// | ||
public func unlock() { | ||
// lockCount -= 1 | ||
// | ||
// precondition(lockCount >= 0) | ||
// | ||
// if lockCount == 0 { | ||
// internalLock.unlock() | ||
// } | ||
public func withLock<T: Sendable>( | ||
isolation: isolated (any Actor)? = #isolation, | ||
_ block: () async throws -> T | ||
) async rethrows -> T { | ||
if Self.locked { | ||
return try await block() | ||
} | ||
|
||
await internalLock.lock() | ||
|
||
do { | ||
let value = try await Self.$locked.withValue(true) { | ||
try await block() | ||
} | ||
|
||
internalLock.unlock() | ||
|
||
return value | ||
} catch { | ||
internalLock.unlock() | ||
|
||
throw error | ||
} | ||
} | ||
// | ||
// public func withLock<T>( | ||
// isolation: isolated (any Actor)? = #isolation, | ||
// _ block: () async throws -> sending T | ||
// ) async rethrows -> sending T { | ||
// await lock() | ||
// // bug | ||
//// defer { unlock() } | ||
// | ||
// do { | ||
// let value = try await block() | ||
// | ||
// unlock() | ||
// | ||
// return value | ||
// } catch { | ||
// unlock() | ||
// | ||
// throw error | ||
// } | ||
// } | ||
} |
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