first cut at making PacketSender use QWaitCondition

This commit is contained in:
ZappoMan 2014-03-11 20:45:46 -07:00
parent f5cdb98efb
commit 8befefb054
2 changed files with 22 additions and 1 deletions

View file

@ -54,6 +54,10 @@ void PacketSender::queuePacketForSending(const SharedNodePointer& destinationNod
unlock();
_totalPacketsQueued++;
_totalBytesQueued += packet.size();
// Make sure to wake our actual processing thread because we now have packets for it to process.
qDebug() << "PacketSender::queuePacketForSending()... wake up, we need to send packets!.";
_hasPackets.wakeAll();
}
void PacketSender::setPacketsPerSecond(int packetsPerSecond) {
@ -68,6 +72,10 @@ bool PacketSender::process() {
return nonThreadedProcess();
}
void PacketSender::terminating() {
qDebug() << "PacketSender::terminating()... wake up, we need to die.";
_hasPackets.wakeAll();
}
bool PacketSender::threadedProcess() {
bool hasSlept = false;
@ -113,7 +121,14 @@ bool PacketSender::threadedProcess() {
// we don't want to sleep too long because how ever much we sleep will delay any future unsent
// packets that arrive while we're sleeping. So we sleep 1/2 of our target fps interval
if (!hasSlept) {
usleep(MINIMAL_SLEEP_INTERVAL);
//usleep(MINIMAL_SLEEP_INTERVAL);
// wait till we have packets
_waitingOnPacketsMutex.lock();
qDebug() << "PacketSender::threadedProcess()... waiting on packets to send...";
_hasPackets.wait(&_waitingOnPacketsMutex);
qDebug() << "PacketSender::threadedProcess()... YIPEEE we're awake...";
_waitingOnPacketsMutex.unlock();
}
return isStillRunning();

View file

@ -11,6 +11,8 @@
#ifndef __shared__PacketSender__
#define __shared__PacketSender__
#include <QWaitCondition>
#include "GenericThread.h"
#include "NetworkPacket.h"
#include "NodeList.h"
@ -44,6 +46,7 @@ public:
int getPacketsPerSecond() const { return _packetsPerSecond; }
virtual bool process();
virtual void terminating();
/// are there packets waiting in the send queue to be sent
bool hasPacketsToSend() const { return _packets.size() > 0; }
@ -113,6 +116,9 @@ private:
quint64 _totalPacketsQueued;
quint64 _totalBytesQueued;
QWaitCondition _hasPackets;
QMutex _waitingOnPacketsMutex;
};
#endif // __shared__PacketSender__