15
15
import Logging
16
16
import NIOConcurrencyHelpers
17
17
import NIOCore
18
- import Synchronization
18
+
19
+ // To be re-enabled when we will be able to use Mutex on Linux
20
+ // import Synchronization
19
21
20
22
#if canImport(FoundationEssentials)
21
23
import FoundationEssentials
@@ -25,14 +27,16 @@ import Foundation
25
27
26
28
// This is our gardian to ensure only one LambdaRuntime is initialized
27
29
// 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 )
30
34
public enum LambdaRuntimeError : Error {
31
35
case moreThanOneLambdaRuntimeInstance
32
36
}
33
37
// We need `@unchecked` Sendable here, as `NIOLockedValueBox` does not understand `sending` today.
34
38
// 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) .
36
40
public final class LambdaRuntime < Handler> : @unchecked Sendable where Handler: StreamingLambdaHandler {
37
41
// TODO: We want to change this to Mutex as soon as this doesn't crash the Swift compiler on Linux anymore
38
42
let handlerMutex : NIOLockedValueBox < Handler ? >
@@ -46,7 +50,14 @@ public final class LambdaRuntime<Handler>: @unchecked Sendable where Handler: St
46
50
) throws ( LambdaRuntimeError) {
47
51
48
52
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 {
50
61
let alreadyCreated = $0
51
62
guard alreadyCreated == false else {
52
63
throw LambdaRuntimeError . moreThanOneLambdaRuntimeInstance
0 commit comments