1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-13 20:01:46 -05:00
sdrangel/sdrbase/util/messagequeue.cpp

86 lines
2.4 KiB
C++
Raw Normal View History

2015-08-13 23:00:28 -04:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2015 F4EXB //
// written by Edouard Griffiths //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation as version 3 of the License, or //
// (at your option) any later version. //
2015-08-13 23:00:28 -04:00
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License V3 for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <QDebug>
#include <QMutexLocker>
#include "util/messagequeue.h"
#include "util/message.h"
MessageQueue::MessageQueue(QObject* parent) :
QObject(parent),
m_lock(QMutex::Recursive),
m_queue()
{
}
MessageQueue::~MessageQueue()
{
2015-08-13 23:00:28 -04:00
Message* message;
while ((message = pop()) != 0)
{
qDebug() << "MessageQueue::~MessageQueue: message: " << message->getIdentifier() << " was still in queue";
2015-08-13 23:00:28 -04:00
delete message;
}
}
2015-08-13 23:00:28 -04:00
void MessageQueue::push(Message* message, bool emitSignal)
{
2015-08-13 23:00:28 -04:00
if (message)
{
m_lock.lock();
m_queue.append(message);
m_lock.unlock();
}
if (emitSignal)
{
emit messageEnqueued();
}
}
2015-08-13 23:00:28 -04:00
Message* MessageQueue::pop()
{
QMutexLocker locker(&m_lock);
2015-08-13 23:00:28 -04:00
if (m_queue.isEmpty())
{
return 0;
}
else
{
return m_queue.takeFirst();
}
}
2015-08-13 23:00:28 -04:00
int MessageQueue::size()
{
QMutexLocker locker(&m_lock);
return m_queue.size();
}
void MessageQueue::clear()
{
QMutexLocker locker(&m_lock);
while (!m_queue.isEmpty()) {
delete m_queue.takeFirst();
}
}