Skip to content

Commit 40b6127

Browse files
committed
convert Mutex to NIOLockedValueBox
1 parent a63a639 commit 40b6127

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

Sources/AWSLambdaRuntime/LambdaRuntime.swift

+16-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
import Logging
1616
import NIOConcurrencyHelpers
1717
import NIOCore
18-
import Synchronization
18+
19+
// To be re-enabled when we will be able to use Mutex on Linux
20+
// import Synchronization
1921

2022
#if canImport(FoundationEssentials)
2123
import FoundationEssentials
@@ -25,14 +27,16 @@ import Foundation
2527

2628
// This is our gardian to ensure only one LambdaRuntime is initialized
2729
// We use a Mutex here to ensure thread safety
28-
// We don't use LambdaRuntime<> as the type here, as we don't know the concrete type that will be used
29-
private let _singleton = Mutex<Bool>(false)
30+
// We use Bool instead of LambdaRuntime<Handler> as the type here, as we don't know the concrete type that will be used
31+
// We would love to use Mutex here, but this sadly crashes the compiler today (on Linux).
32+
// private let _singleton = Mutex<Bool>(false)
33+
private let _singleton = NIOLockedValueBox<Bool>(false)
3034
public enum LambdaRuntimeError: Error {
3135
case moreThanOneLambdaRuntimeInstance
3236
}
3337
// We need `@unchecked` Sendable here, as `NIOLockedValueBox` does not understand `sending` today.
3438
// We don't want to use `NIOLockedValueBox` here anyway. We would love to use Mutex here, but this
35-
// sadly crashes the compiler today.
39+
// sadly crashes the compiler today (on Linux).
3640
public final class LambdaRuntime<Handler>: @unchecked Sendable where Handler: StreamingLambdaHandler {
3741
// TODO: We want to change this to Mutex as soon as this doesn't crash the Swift compiler on Linux anymore
3842
let handlerMutex: NIOLockedValueBox<Handler?>
@@ -46,7 +50,14 @@ public final class LambdaRuntime<Handler>: @unchecked Sendable where Handler: St
4650
) throws(LambdaRuntimeError) {
4751

4852
do {
49-
try _singleton.withLock {
53+
// try _singleton.withLock {
54+
// let alreadyCreated = $0
55+
// guard alreadyCreated == false else {
56+
// throw LambdaRuntimeError.moreThanOneLambdaRuntimeInstance
57+
// }
58+
// $0 = true
59+
// }
60+
try _singleton.withLockedValue {
5061
let alreadyCreated = $0
5162
guard alreadyCreated == false else {
5263
throw LambdaRuntimeError.moreThanOneLambdaRuntimeInstance

0 commit comments

Comments
 (0)