PacketTunnelProvider: fix locking logic
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
8aeca61322
commit
99b9a6b80a
|
@ -192,22 +192,19 @@ class WireGuardContext {
|
||||||
readPacketCondition.signal()
|
readPacketCondition.signal()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func packetsRead(packets: [NEPacket]) {
|
||||||
|
outboundPackets.append(contentsOf: packets)
|
||||||
|
readPacketCondition.signal()
|
||||||
|
}
|
||||||
|
|
||||||
func readPacket(isTunnelClosed: inout Bool) -> NEPacket? {
|
func readPacket(isTunnelClosed: inout Bool) -> NEPacket? {
|
||||||
if outboundPackets.isEmpty {
|
if outboundPackets.isEmpty {
|
||||||
let readPacketCondition = NSCondition()
|
|
||||||
readPacketCondition.lock()
|
readPacketCondition.lock()
|
||||||
var packetsObtained: [NEPacket]?
|
packetFlow.readPacketObjects(completionHandler: packetsRead)
|
||||||
packetFlow.readPacketObjects { (packets: [NEPacket]) in
|
|
||||||
packetsObtained = packets
|
|
||||||
readPacketCondition.signal()
|
|
||||||
}
|
|
||||||
// Wait till the completion handler of packetFlow.readPacketObjects() finishes
|
// Wait till the completion handler of packetFlow.readPacketObjects() finishes
|
||||||
while packetsObtained == nil && !self.isTunnelClosed {
|
while outboundPackets.isEmpty && !self.isTunnelClosed {
|
||||||
readPacketCondition.wait()
|
readPacketCondition.wait()
|
||||||
}
|
}
|
||||||
if let packetsObtained = packetsObtained {
|
|
||||||
outboundPackets = packetsObtained
|
|
||||||
}
|
|
||||||
readPacketCondition.unlock()
|
readPacketCondition.unlock()
|
||||||
}
|
}
|
||||||
isTunnelClosed = self.isTunnelClosed
|
isTunnelClosed = self.isTunnelClosed
|
||||||
|
|
Loading…
Reference in New Issue