15
15
import Logging
16
16
import NIOConcurrencyHelpers
17
17
import NIOCore
18
-
19
- // To be re-enabled when we will be able to use Mutex on Linux
20
- // import Synchronization
18
+ import Synchronization
21
19
22
20
#if canImport(FoundationEssentials)
23
21
import FoundationEssentials
@@ -28,18 +26,12 @@ import Foundation
28
26
// This is our gardian to ensure only one LambdaRuntime is initialized
29
27
// We use a Mutex here to ensure thread safety
30
28
// 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 )
29
+ private let _singleton = Mutex < Bool > ( false )
34
30
public enum LambdaRuntimeError : Error {
35
31
case moreThanOneLambdaRuntimeInstance
36
32
}
37
- // We need `@unchecked` Sendable here, as `NIOLockedValueBox` does not understand `sending` today.
38
- // We don't want to use `NIOLockedValueBox` here anyway. We would love to use Mutex here, but this
39
- // sadly crashes the compiler today (on Linux).
40
- public final class LambdaRuntime < Handler> : @unchecked Sendable where Handler: StreamingLambdaHandler {
41
- // TODO: We want to change this to Mutex as soon as this doesn't crash the Swift compiler on Linux anymore
42
- let handlerMutex : NIOLockedValueBox < Handler ? >
33
+ public final class LambdaRuntime < Handler> : Sendable where Handler: StreamingLambdaHandler {
34
+ let handlerMutex : Mutex < Handler ? >
43
35
let logger : Logger
44
36
let eventLoop : EventLoop
45
37
@@ -50,14 +42,7 @@ public final class LambdaRuntime<Handler>: @unchecked Sendable where Handler: St
50
42
) throws ( LambdaRuntimeError) {
51
43
52
44
do {
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 {
45
+ try _singleton. withLock {
61
46
let alreadyCreated = $0
62
47
guard alreadyCreated == false else {
63
48
throw LambdaRuntimeError . moreThanOneLambdaRuntimeInstance
@@ -70,7 +55,7 @@ public final class LambdaRuntime<Handler>: @unchecked Sendable where Handler: St
70
55
fatalError ( " An unknown error occurred: \( error) " )
71
56
}
72
57
73
- self . handlerMutex = NIOLockedValueBox ( handler)
58
+ self . handlerMutex = Mutex ( handler)
74
59
self . eventLoop = eventLoop
75
60
76
61
// by setting the log level here, we understand it can not be changed dynamically at runtime
@@ -83,7 +68,7 @@ public final class LambdaRuntime<Handler>: @unchecked Sendable where Handler: St
83
68
}
84
69
85
70
public func run( ) async throws {
86
- let handler = self . handlerMutex. withLockedValue { handler in
71
+ let handler = self . handlerMutex. withLock { handler in
87
72
let result = handler
88
73
handler = nil
89
74
return result
0 commit comments