Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Sources/Lock/AsyncLock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public final class AsyncLock {
state.resumeNextContinuation()
}

public func withLock<T: Sendable>(
public func withLock<T: Sendable, E: Error>(
isolation: isolated (any Actor)? = #isolation,
_ block: @isolated(any) () async throws -> T
) async rethrows -> T {
_ block: @isolated(any) () async throws(E) -> T
) async throws(E) -> T {
await lock()

do {
Expand Down
26 changes: 16 additions & 10 deletions Sources/Lock/AsyncRecursiveLock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ final class AsyncRecursiveLock {
public init() {
}

public func withLock<T: Sendable>(
public func withLock<T: Sendable, E: Error>(
isolation: isolated (any Actor)? = #isolation,
_ block: () async throws -> T
) async rethrows -> T {
_ block: () async throws(E) -> T
) async throws(E) -> T {
let id = ObjectIdentifier(self)
var set = Self.lockedSet

Expand All @@ -23,17 +23,23 @@ final class AsyncRecursiveLock {
return try await block()
}

return try await internalLock.withLock {
try await Self.$lockedSet.withValue(set) {
try await block()
return try await internalLock.withLock { () throws(E) -> T in
do {
return try await Self.$lockedSet.withValue(set) {
try await block()
}
} catch {
/* withValue from TaskLocal does not seem to properly throw typed errors (yet?).
* It does rethrows though, so the forced cast should be valid. */
throw error as! E
}
}
}

// public func withLock<T: Sendable>(
// public func withLock<T: Sendable, E: Error>(
// isolation: isolated (any Actor)? = #isolation,
// _ block: () async throws -> T
// ) async rethrows -> T {
// _ block: () async throws(E) -> T
// ) async throws(E) -> T {
// if Self.locked {
// return try await block()
// }
Expand All @@ -51,7 +57,7 @@ final class AsyncRecursiveLock {
// } catch {
// internalLock.unlock()
//
// throw error
// throw error as! E
// }
// }
}
Loading