1515import Logging
1616import NIOConcurrencyHelpers
1717import NIOCore
18-
19- // To be re-enabled when we will be able to use Mutex on Linux
20- // import Synchronization
18+ import Synchronization
2119
2220#if canImport(FoundationEssentials)
2321import FoundationEssentials
@@ -28,18 +26,12 @@ import Foundation
2826// This is our gardian to ensure only one LambdaRuntime is initialized
2927// We use a Mutex here to ensure thread safety
3028// 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 )
3430public enum LambdaRuntimeError : Error {
3531 case moreThanOneLambdaRuntimeInstance
3632}
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 ? >
4335 let logger : Logger
4436 let eventLoop : EventLoop
4537
@@ -50,14 +42,7 @@ public final class LambdaRuntime<Handler>: @unchecked Sendable where Handler: St
5042 ) throws ( LambdaRuntimeError) {
5143
5244 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 {
6146 let alreadyCreated = $0
6247 guard alreadyCreated == false else {
6348 throw LambdaRuntimeError . moreThanOneLambdaRuntimeInstance
@@ -70,7 +55,7 @@ public final class LambdaRuntime<Handler>: @unchecked Sendable where Handler: St
7055 fatalError ( " An unknown error occurred: \( error) " )
7156 }
7257
73- self . handlerMutex = NIOLockedValueBox ( handler)
58+ self . handlerMutex = Mutex ( handler)
7459 self . eventLoop = eventLoop
7560
7661 // 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
8368 }
8469
8570 public func run( ) async throws {
86- let handler = self . handlerMutex. withLockedValue { handler in
71+ let handler = self . handlerMutex. withLock { handler in
8772 let result = handler
8873 handler = nil
8974 return result
0 commit comments