Skip to content

Commit

Permalink
swift-mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
swhitty committed Sep 8, 2024
1 parent 47e1616 commit e7f38fe
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 275 deletions.
2 changes: 1 addition & 1 deletion FlyingFox/Sources/URLSession+Async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ extension URLSession {

// Ports macOS Foundation method to Linux
func data(for request: URLRequest) async throws -> (Data, URLResponse) {
let state = AllocatedLock(initialState: (isCancelled: false, task: URLSessionDataTask?.none))
let state = Mutex((isCancelled: false, task: URLSessionDataTask?.none))
return try await withTaskCancellationHandler {
try await withCheckedThrowingContinuation { continuation in
let task = dataTask(with: request) { data, response, error in
Expand Down
10 changes: 5 additions & 5 deletions FlyingFox/Tests/HTTPLoggingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct HTTPLoggingTests {
@Test
func disabledLogger_DoesNotExecuteDebugClosure() {
let logger = DisabledLogger()
let didLog = AllocatedLock(initialState: false)
let didLog = Mutex(false)

logger.logDebug({
didLog.withLock { $0 = true }
Expand All @@ -103,7 +103,7 @@ struct HTTPLoggingTests {
@Test
func disabledLogger_DoesNotExecuteInfoClosure() {
let logger = DisabledLogger()
let didLog = AllocatedLock(initialState: false)
let didLog = Mutex(false)

logger.logInfo({
didLog.withLock { $0 = true }
Expand All @@ -116,7 +116,7 @@ struct HTTPLoggingTests {
@Test
func disabledLogger_DoesNotExecuteWarningClosure() {
let logger = DisabledLogger()
let didLog = AllocatedLock(initialState: false)
let didLog = Mutex(false)

logger.logWarning({
didLog.withLock { $0 = true }
Expand All @@ -129,7 +129,7 @@ struct HTTPLoggingTests {
@Test
func disabledLogger_DoesNotExecuteErrorClosure() {
let logger = DisabledLogger()
let didLog = AllocatedLock(initialState: false)
let didLog = Mutex(false)

logger.logError({
didLog.withLock { $0 = true }
Expand All @@ -142,7 +142,7 @@ struct HTTPLoggingTests {
@Test
func disabledLogger_DoesNotExecuteCriticalClosure() {
let logger = DisabledLogger()
let didLog = AllocatedLock(initialState: false)
let didLog = Mutex(false)

logger.logCritical({
didLog.withLock { $0 = true }
Expand Down
2 changes: 1 addition & 1 deletion FlyingSocks/Sources/AsyncBufferedEmptySequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ package struct AsyncBufferedEmptySequence<Element: Sendable>: Sendable, AsyncBuf
if completeImmediately {
return nil
}
let state = AllocatedLock(initialState: State())
let state = Mutex(State())
return await withTaskCancellationHandler {
await withCheckedContinuation { (continuation: CheckedContinuation<Element?, Never>) in
let shouldCancel = state.withLock {
Expand Down
4 changes: 2 additions & 2 deletions FlyingSocks/Sources/ConsumingAsyncSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ extension ConsumingAsyncSequence {

final class SharedBuffer: @unchecked Sendable {

private(set) var state: AllocatedLock<State>
private(set) var state: Mutex<State>

init(_ sequence: Base) {
self.state = AllocatedLock(initialState: .initial(sequence))
self.state = Mutex(.initial(sequence))
}

enum State: @unchecked Sendable {
Expand Down
19 changes: 15 additions & 4 deletions FlyingSocks/Sources/IdentifiableContinuation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@
/// - body: A closure that takes a `IdentifiableContinuation` parameter.
/// - handler: Cancellation closure executed when the current Task is cancelled. Handler is always called _after_ the body closure is compeled.
/// - Returns: The value continuation is resumed with.
@inlinable
package func withIdentifiableContinuation<T>(
isolation: isolated (any Actor)? = #isolation,
function: String = #function,
body: (IdentifiableContinuation<T, Never>) -> Void,
onCancel handler: @Sendable (IdentifiableContinuation<T, Never>.ID) -> Void
) async -> T {
let id = IdentifiableContinuation<T, Never>.ID()
let state = AllocatedLock(initialState: (isStarted: false, isCancelled: false))
let state = Mutex((isStarted: false, isCancelled: false))
nonisolated(unsafe) let body = body
return await withTaskCancellationHandler {
await withCheckedContinuation(isolation: isolation, function: function) {
Expand Down Expand Up @@ -90,14 +91,15 @@ package func withIdentifiableContinuation<T>(
/// - body: A closure that takes a `IdentifiableContinuation` parameter.
/// - handler: Cancellation closure executed when the current Task is cancelled. Handler is always called _after_ the body closure is compeled.
/// - Returns: The value continuation is resumed with.
@inlinable
package func withIdentifiableThrowingContinuation<T>(
isolation: isolated (any Actor)? = #isolation,
function: String = #function,
body: (IdentifiableContinuation<T, any Error>) -> Void,
onCancel handler: @Sendable (IdentifiableContinuation<T, any Error>.ID) -> Void
) async throws -> T {
let id = IdentifiableContinuation<T, any Error>.ID()
let state = AllocatedLock(initialState: (isStarted: false, isCancelled: false))
let state = Mutex((isStarted: false, isCancelled: false))
nonisolated(unsafe) let body = body
return try await withTaskCancellationHandler {
try await withCheckedThrowingContinuation(isolation: isolation, function: function) {
Expand Down Expand Up @@ -138,14 +140,15 @@ package func withIdentifiableThrowingContinuation<T>(
/// - handler: Cancellation closure executed when the current Task is cancelled. Handler is always called _after_ the body closure is compeled.
/// - Returns: The value continuation is resumed with.
@_unsafeInheritExecutor
@inlinable
package func withIdentifiableContinuation<T>(
isolation: isolated some Actor,
function: String = #function,
body: (IdentifiableContinuation<T, Never>) -> Void,
onCancel handler: @Sendable (IdentifiableContinuation<T, Never>.ID) -> Void
) async -> T {
let id = IdentifiableContinuation<T, Never>.ID()
let state = AllocatedLock(initialState: (isStarted: false, isCancelled: false))
let state = Mutex((isStarted: false, isCancelled: false))
return await withTaskCancellationHandler {
await withCheckedContinuation(function: function) {
let continuation = IdentifiableContinuation(id: id, continuation: $0)
Expand Down Expand Up @@ -186,14 +189,15 @@ package func withIdentifiableContinuation<T>(
/// - handler: Cancellation closure executed when the current Task is cancelled. Handler is always called _after_ the body closure is compeled.
/// - Returns: The value continuation is resumed with.
@_unsafeInheritExecutor
@inlinable
package func withIdentifiableThrowingContinuation<T>(
isolation: isolated some Actor,
function: String = #function,
body: (IdentifiableContinuation<T, any Error>) -> Void,
onCancel handler: @Sendable (IdentifiableContinuation<T, any Error>.ID) -> Void
) async throws -> T {
let id = IdentifiableContinuation<T, any Error>.ID()
let state = AllocatedLock(initialState: (isStarted: false, isCancelled: false))
let state = Mutex((isStarted: false, isCancelled: false))
return try await withTaskCancellationHandler {
try await withCheckedThrowingContinuation(function: function) {
let continuation = IdentifiableContinuation(id: id, continuation: $0)
Expand All @@ -219,23 +223,30 @@ package func withIdentifiableThrowingContinuation<T>(
}
#endif

@usableFromInline
package struct IdentifiableContinuation<T, E>: Sendable, Identifiable where E: Error {

@usableFromInline
package let id: ID

@usableFromInline
package final class ID: Hashable, Sendable {

@usableFromInline
init() { }

@usableFromInline
package func hash(into hasher: inout Hasher) {
ObjectIdentifier(self).hash(into: &hasher)
}

@usableFromInline
package static func == (lhs: IdentifiableContinuation<T, E>.ID, rhs: IdentifiableContinuation<T, E>.ID) -> Bool {
lhs === rhs
}
}

@usableFromInline
init(id: ID, continuation: CheckedContinuation<T, E>) {
self.id = id
self.continuation = continuation
Expand Down
Loading

0 comments on commit e7f38fe

Please sign in to comment.