Skip to content

Commit e7f38fe

Browse files
committed
swift-mutex
1 parent 47e1616 commit e7f38fe

12 files changed

+251
-275
lines changed

FlyingFox/Sources/URLSession+Async.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ extension URLSession {
3939

4040
// Ports macOS Foundation method to Linux
4141
func data(for request: URLRequest) async throws -> (Data, URLResponse) {
42-
let state = AllocatedLock(initialState: (isCancelled: false, task: URLSessionDataTask?.none))
42+
let state = Mutex((isCancelled: false, task: URLSessionDataTask?.none))
4343
return try await withTaskCancellationHandler {
4444
try await withCheckedThrowingContinuation { continuation in
4545
let task = dataTask(with: request) { data, response, error in

FlyingFox/Tests/HTTPLoggingTests.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ struct HTTPLoggingTests {
9090
@Test
9191
func disabledLogger_DoesNotExecuteDebugClosure() {
9292
let logger = DisabledLogger()
93-
let didLog = AllocatedLock(initialState: false)
93+
let didLog = Mutex(false)
9494

9595
logger.logDebug({
9696
didLog.withLock { $0 = true }
@@ -103,7 +103,7 @@ struct HTTPLoggingTests {
103103
@Test
104104
func disabledLogger_DoesNotExecuteInfoClosure() {
105105
let logger = DisabledLogger()
106-
let didLog = AllocatedLock(initialState: false)
106+
let didLog = Mutex(false)
107107

108108
logger.logInfo({
109109
didLog.withLock { $0 = true }
@@ -116,7 +116,7 @@ struct HTTPLoggingTests {
116116
@Test
117117
func disabledLogger_DoesNotExecuteWarningClosure() {
118118
let logger = DisabledLogger()
119-
let didLog = AllocatedLock(initialState: false)
119+
let didLog = Mutex(false)
120120

121121
logger.logWarning({
122122
didLog.withLock { $0 = true }
@@ -129,7 +129,7 @@ struct HTTPLoggingTests {
129129
@Test
130130
func disabledLogger_DoesNotExecuteErrorClosure() {
131131
let logger = DisabledLogger()
132-
let didLog = AllocatedLock(initialState: false)
132+
let didLog = Mutex(false)
133133

134134
logger.logError({
135135
didLog.withLock { $0 = true }
@@ -142,7 +142,7 @@ struct HTTPLoggingTests {
142142
@Test
143143
func disabledLogger_DoesNotExecuteCriticalClosure() {
144144
let logger = DisabledLogger()
145-
let didLog = AllocatedLock(initialState: false)
145+
let didLog = Mutex(false)
146146

147147
logger.logCritical({
148148
didLog.withLock { $0 = true }

FlyingSocks/Sources/AsyncBufferedEmptySequence.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ package struct AsyncBufferedEmptySequence<Element: Sendable>: Sendable, AsyncBuf
4848
if completeImmediately {
4949
return nil
5050
}
51-
let state = AllocatedLock(initialState: State())
51+
let state = Mutex(State())
5252
return await withTaskCancellationHandler {
5353
await withCheckedContinuation { (continuation: CheckedContinuation<Element?, Never>) in
5454
let shouldCancel = state.withLock {

FlyingSocks/Sources/ConsumingAsyncSequence.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ extension ConsumingAsyncSequence {
6767

6868
final class SharedBuffer: @unchecked Sendable {
6969

70-
private(set) var state: AllocatedLock<State>
70+
private(set) var state: Mutex<State>
7171

7272
init(_ sequence: Base) {
73-
self.state = AllocatedLock(initialState: .initial(sequence))
73+
self.state = Mutex(.initial(sequence))
7474
}
7575

7676
enum State: @unchecked Sendable {

FlyingSocks/Sources/IdentifiableContinuation.swift

+15-4
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@
4444
/// - body: A closure that takes a `IdentifiableContinuation` parameter.
4545
/// - handler: Cancellation closure executed when the current Task is cancelled. Handler is always called _after_ the body closure is compeled.
4646
/// - Returns: The value continuation is resumed with.
47+
@inlinable
4748
package func withIdentifiableContinuation<T>(
4849
isolation: isolated (any Actor)? = #isolation,
4950
function: String = #function,
5051
body: (IdentifiableContinuation<T, Never>) -> Void,
5152
onCancel handler: @Sendable (IdentifiableContinuation<T, Never>.ID) -> Void
5253
) async -> T {
5354
let id = IdentifiableContinuation<T, Never>.ID()
54-
let state = AllocatedLock(initialState: (isStarted: false, isCancelled: false))
55+
let state = Mutex((isStarted: false, isCancelled: false))
5556
nonisolated(unsafe) let body = body
5657
return await withTaskCancellationHandler {
5758
await withCheckedContinuation(isolation: isolation, function: function) {
@@ -90,14 +91,15 @@ package func withIdentifiableContinuation<T>(
9091
/// - body: A closure that takes a `IdentifiableContinuation` parameter.
9192
/// - handler: Cancellation closure executed when the current Task is cancelled. Handler is always called _after_ the body closure is compeled.
9293
/// - Returns: The value continuation is resumed with.
94+
@inlinable
9395
package func withIdentifiableThrowingContinuation<T>(
9496
isolation: isolated (any Actor)? = #isolation,
9597
function: String = #function,
9698
body: (IdentifiableContinuation<T, any Error>) -> Void,
9799
onCancel handler: @Sendable (IdentifiableContinuation<T, any Error>.ID) -> Void
98100
) async throws -> T {
99101
let id = IdentifiableContinuation<T, any Error>.ID()
100-
let state = AllocatedLock(initialState: (isStarted: false, isCancelled: false))
102+
let state = Mutex((isStarted: false, isCancelled: false))
101103
nonisolated(unsafe) let body = body
102104
return try await withTaskCancellationHandler {
103105
try await withCheckedThrowingContinuation(isolation: isolation, function: function) {
@@ -138,14 +140,15 @@ package func withIdentifiableThrowingContinuation<T>(
138140
/// - handler: Cancellation closure executed when the current Task is cancelled. Handler is always called _after_ the body closure is compeled.
139141
/// - Returns: The value continuation is resumed with.
140142
@_unsafeInheritExecutor
143+
@inlinable
141144
package func withIdentifiableContinuation<T>(
142145
isolation: isolated some Actor,
143146
function: String = #function,
144147
body: (IdentifiableContinuation<T, Never>) -> Void,
145148
onCancel handler: @Sendable (IdentifiableContinuation<T, Never>.ID) -> Void
146149
) async -> T {
147150
let id = IdentifiableContinuation<T, Never>.ID()
148-
let state = AllocatedLock(initialState: (isStarted: false, isCancelled: false))
151+
let state = Mutex((isStarted: false, isCancelled: false))
149152
return await withTaskCancellationHandler {
150153
await withCheckedContinuation(function: function) {
151154
let continuation = IdentifiableContinuation(id: id, continuation: $0)
@@ -186,14 +189,15 @@ package func withIdentifiableContinuation<T>(
186189
/// - handler: Cancellation closure executed when the current Task is cancelled. Handler is always called _after_ the body closure is compeled.
187190
/// - Returns: The value continuation is resumed with.
188191
@_unsafeInheritExecutor
192+
@inlinable
189193
package func withIdentifiableThrowingContinuation<T>(
190194
isolation: isolated some Actor,
191195
function: String = #function,
192196
body: (IdentifiableContinuation<T, any Error>) -> Void,
193197
onCancel handler: @Sendable (IdentifiableContinuation<T, any Error>.ID) -> Void
194198
) async throws -> T {
195199
let id = IdentifiableContinuation<T, any Error>.ID()
196-
let state = AllocatedLock(initialState: (isStarted: false, isCancelled: false))
200+
let state = Mutex((isStarted: false, isCancelled: false))
197201
return try await withTaskCancellationHandler {
198202
try await withCheckedThrowingContinuation(function: function) {
199203
let continuation = IdentifiableContinuation(id: id, continuation: $0)
@@ -219,23 +223,30 @@ package func withIdentifiableThrowingContinuation<T>(
219223
}
220224
#endif
221225

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

229+
@usableFromInline
224230
package let id: ID
225231

232+
@usableFromInline
226233
package final class ID: Hashable, Sendable {
227234

235+
@usableFromInline
228236
init() { }
229237

238+
@usableFromInline
230239
package func hash(into hasher: inout Hasher) {
231240
ObjectIdentifier(self).hash(into: &hasher)
232241
}
233242

243+
@usableFromInline
234244
package static func == (lhs: IdentifiableContinuation<T, E>.ID, rhs: IdentifiableContinuation<T, E>.ID) -> Bool {
235245
lhs === rhs
236246
}
237247
}
238248

249+
@usableFromInline
239250
init(id: ID, continuation: CheckedContinuation<T, E>) {
240251
self.id = id
241252
self.continuation = continuation

0 commit comments

Comments
 (0)