Eliminate per-packet locking for processing

This commit is contained in:
Brad Davis 2015-07-01 10:06:04 -07:00
parent 448a0f01b6
commit d4d6f8f5d5

View file

@ -38,19 +38,28 @@ bool ReceivedPacketProcessor::process() {
_hasPackets.wait(&_waitingOnPacketsMutex, getMaxWait()); _hasPackets.wait(&_waitingOnPacketsMutex, getMaxWait());
_waitingOnPacketsMutex.unlock(); _waitingOnPacketsMutex.unlock();
} }
preProcess(); preProcess();
while (_packets.size() > 0) { QVector<NetworkPacket> currentPackets;
lock(); // lock to make sure nothing changes on us if (!_packets.size()) {
NetworkPacket& packet = _packets.front(); // get the oldest packet return isStillRunning();
NetworkPacket temporary = packet; // make a copy of the packet in case the vector is resized on us }
_packets.erase(_packets.begin()); // remove the oldest packet
if (!temporary.getNode().isNull()) { lock();
_nodePacketCounts[temporary.getNode()->getUUID()]--; std::swap(currentPackets, _packets);
} unlock();
unlock(); // let others add to the packets
processPacket(temporary.getNode(), temporary.getByteArray()); // process our temporary copy foreach(auto& packet, currentPackets) {
processPacket(packet.getNode(), packet.getByteArray());
midProcess(); midProcess();
} }
lock();
foreach(auto& packet, currentPackets) {
_nodePacketCounts[packet.getNode()->getUUID()]--;
}
unlock();
postProcess(); postProcess();
return isStillRunning(); // keep running till they terminate us return isStillRunning(); // keep running till they terminate us
} }