Skip to content

Commit

Permalink
Merge pull request #86 from swhitty/socket-pool-queue-continuation
Browse files Browse the repository at this point in the history
SocketPool + IdentifiableContinuation on Darwin
  • Loading branch information
swhitty authored May 3, 2024
2 parents dc7fba6 + 17ce23e commit 1381625
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions FlyingSocks/Sources/SocketPool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ public final actor SocketPool<Queue: EventQueue>: AsyncSocketPool {
}
}

#if canImport(Darwin)
private func getNotifications() async throws -> [EventNotification] {
try Task.checkCancellation()
return try await withIdentifiableThrowingContinuation(isolation: self) { continuation in
dispatchQueue.async { [queue] in
let result = Result {
try queue.getNotifications()
}
continuation.resume(with: result)
}
} onCancel: { _ in
Task { await self.closeQueue() }
}
}
#else
// EPOLL does not throw error when queue FD is closed while currently waiting for events.
private func getNotifications() async throws -> [EventNotification] {
let continuation = CancellingContinuation<[EventNotification], any Swift.Error>()
dispatchQueue.async { [queue] in
Expand All @@ -123,6 +139,11 @@ public final actor SocketPool<Queue: EventQueue>: AsyncSocketPool {
}
return try await continuation.value
}
#endif

private func closeQueue() {
try? queue.close()
}

private func processNotifications(_ notifications: [EventNotification]) {
for notification in notifications {
Expand Down

0 comments on commit 1381625

Please sign in to comment.