PacketTunnelProvider: fix locking logic

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2018-09-22 04:10:31 +02:00
parent 8aeca61322
commit 99b9a6b80a
1 changed files with 7 additions and 10 deletions

View File

@ -192,22 +192,19 @@ class WireGuardContext {
readPacketCondition.signal()
}
func packetsRead(packets: [NEPacket]) {
outboundPackets.append(contentsOf: packets)
readPacketCondition.signal()
}
func readPacket(isTunnelClosed: inout Bool) -> NEPacket? {
if outboundPackets.isEmpty {
let readPacketCondition = NSCondition()
readPacketCondition.lock()
var packetsObtained: [NEPacket]?
packetFlow.readPacketObjects { (packets: [NEPacket]) in
packetsObtained = packets
readPacketCondition.signal()
}
packetFlow.readPacketObjects(completionHandler: packetsRead)
// Wait till the completion handler of packetFlow.readPacketObjects() finishes
while packetsObtained == nil && !self.isTunnelClosed {
while outboundPackets.isEmpty && !self.isTunnelClosed {
readPacketCondition.wait()
}
if let packetsObtained = packetsObtained {
outboundPackets = packetsObtained
}
readPacketCondition.unlock()
}
isTunnelClosed = self.isTunnelClosed