13
13
//===----------------------------------------------------------------------===//
14
14
15
15
import Logging
16
- import NIOConcurrencyHelpers
17
16
import NIOCore
17
+ import Synchronization
18
18
19
19
#if canImport(FoundationEssentials)
20
20
import FoundationEssentials
21
21
#else
22
22
import Foundation
23
23
#endif
24
24
25
- // We need `@unchecked` Sendable here, as `NIOLockedValueBox` does not understand `sending` today.
26
- // We don't want to use `NIOLockedValueBox` here anyway. We would love to use Mutex here, but this
27
- // sadly crashes the compiler today.
25
+ // We need `@unchecked` Sendable here until we can make `Handler` `Sendable`.
28
26
public final class LambdaRuntime < Handler> : @unchecked Sendable where Handler: StreamingLambdaHandler {
29
- // TODO: We want to change this to Mutex as soon as this doesn't crash the Swift compiler on Linux anymore
30
- let handlerMutex : NIOLockedValueBox < Handler ? >
27
+ let handlerMutex : Mutex < Handler ? > = Mutex ( nil )
31
28
32
29
let logger : Logger
33
30
let eventLoop : EventLoop
@@ -37,7 +34,8 @@ public final class LambdaRuntime<Handler>: @unchecked Sendable where Handler: St
37
34
eventLoop: EventLoop = Lambda . defaultEventLoop,
38
35
logger: Logger = Logger ( label: " LambdaRuntime " )
39
36
) {
40
- self . handlerMutex = NIOLockedValueBox ( handler)
37
+
38
+ handlerMutex. withLock { $0 = handler }
41
39
self . eventLoop = eventLoop
42
40
43
41
// by setting the log level here, we understand it can not be changed dynamically at runtime
@@ -50,11 +48,7 @@ public final class LambdaRuntime<Handler>: @unchecked Sendable where Handler: St
50
48
}
51
49
52
50
public func run( ) async throws {
53
- let handler = self . handlerMutex. withLockedValue { handler in
54
- let result = handler
55
- handler = nil
56
- return result
57
- }
51
+ let handler = self . handlerMutex. withLock { $0 }
58
52
59
53
guard let handler else {
60
54
throw LambdaRuntimeError ( code: . runtimeCanOnlyBeStartedOnce)
0 commit comments