|
|
@@ -33,12 +33,6 @@ public class Server {
|
|
|
/// Completion queue used for server operations
|
|
|
let completionQueue: CompletionQueue
|
|
|
|
|
|
- /// Active handlers
|
|
|
- private var handlers = Set<Handler>()
|
|
|
-
|
|
|
- /// Mutex for synchronizing access to handlers
|
|
|
- private let handlersMutex: Mutex = Mutex()
|
|
|
-
|
|
|
/// Optional callback when server stops serving
|
|
|
public var onCompletion: (() -> Void)?
|
|
|
|
|
|
@@ -86,20 +80,21 @@ public class Server {
|
|
|
// run the handler and remove it when it finishes
|
|
|
if event.success != 0 {
|
|
|
// hold onto the handler while it runs
|
|
|
- self.handlersMutex.synchronize {
|
|
|
- self.handlers.insert(handler)
|
|
|
- }
|
|
|
+ var strongHandlerReference: Handler?
|
|
|
+ strongHandlerReference = handler
|
|
|
+ // To prevent the "Variable 'strongHandlerReference' was written to, but never read" warning.
|
|
|
+ _ = strongHandlerReference
|
|
|
// this will start the completion queue on a new thread
|
|
|
handler.completionQueue.runToCompletion {
|
|
|
dispatchQueue.async {
|
|
|
- self.handlersMutex.synchronize {
|
|
|
- // release the handler when it finishes
|
|
|
- self.handlers.remove(handler)
|
|
|
- }
|
|
|
+ // release the handler when it finishes
|
|
|
+ strongHandlerReference = nil
|
|
|
}
|
|
|
}
|
|
|
- // call the handler function on the server thread
|
|
|
- handlerFunction(handler)
|
|
|
+ dispatchQueue.async {
|
|
|
+ // dispatch the handler function on a separate thread
|
|
|
+ handlerFunction(handler)
|
|
|
+ }
|
|
|
}
|
|
|
} else if event.tag == Server.stopTag || event.tag == Server.destroyTag {
|
|
|
running = false // exit the loop
|