Skip to content

Commit

Permalink
Fix data race thread sanitiser warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
abnegate committed May 16, 2024
1 parent aa80720 commit 4644ea2
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions templates/swift/Sources/WebSockets/WebSocketClient.swift.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import NIOFoundationCompat
import NIOSSL

public let WEBSOCKET_LOCKER_QUEUE = "SyncLocker"
public let WEBSOCKET_THREAD_QUEUE = "ThreadLocker"
public let WEBSOCKET_CHANNEL_QUEUE = "ChannelLocker"

/// Creates and manages connections to a WebSocket server.
///
Expand All @@ -20,16 +22,35 @@ public class WebSocketClient {
let query: String
let headers: HTTPHeaders
let frameKey: String

public private(set) var maxFrameSize: Int

var channel: Channel? = nil

var tlsEnabled: Bool = false
var closeSent: Bool = false

let locker = DispatchQueue(label: WEBSOCKET_LOCKER_QUEUE, qos: .background)
private let locker = DispatchQueue(label: WEBSOCKET_LOCKER_QUEUE, qos: .background)
private let channelQueue = DispatchQueue(label: WEBSOCKET_CHANNEL_QUEUE)
private let threadGroupQueue = DispatchQueue(label: WEBSOCKET_THREAD_QUEUE)

var threadGroup: MultiThreadedEventLoopGroup? = nil
var channel: Channel? {
get {
return channelQueue.sync { _channel }
}
set {
channelQueue.sync { _channel = newValue }
}
}
private var _channel: Channel? = nil

var threadGroup: MultiThreadedEventLoopGroup? {
get {
return threadGroupQueue.sync { _threadGroup }
}
set {
threadGroupQueue.sync { _threadGroup = newValue }
}
}
private var _threadGroup: MultiThreadedEventLoopGroup?

weak var delegate: WebSocketClientDelegate? = nil

Expand Down

0 comments on commit 4644ea2

Please sign in to comment.