Added the thread safe TSQueue and using it in mainwindow to replace the adhoc queue implementation.

This commit is contained in:
Eelke Klein 2017-01-03 07:22:36 +01:00
parent c551d982c6
commit 83064ab86b
10 changed files with 253 additions and 27 deletions

View file

@ -40,7 +40,6 @@ MainWindow::MainWindow(QWidget *parent) :
ui->queryEdit->setFont(font);
highlighter.reset(new SqlHighlighter(ui->queryEdit->document()));
ui->queryEdit->setPlainText(test_query);
ui->connectionStringEdit->setText("user=postgres dbname=foutrapport password=admin");
QAction *action;
@ -60,10 +59,9 @@ MainWindow::MainWindow(QWidget *parent) :
MainWindow::~MainWindow()
{}
void MainWindow::QueueTask(callable c)
void MainWindow::QueueTask(TSQueue::t_Callable c)
{
std::lock_guard<std::mutex> lg(m_mutexCallableQueue);
m_callableQueue.emplace_back(std::move(c));
m_taskQueue.add(c);
// Theoretically this needs to be only called if the queue was empty because otherwise it already would
// be busy emptying the queue. For now however I think it is safer to call it just to make sure.
QMetaObject::invokeMethod(this, "processCallableQueue", Qt::QueuedConnection); // queues on main thread
@ -71,20 +69,12 @@ void MainWindow::QueueTask(callable c)
void MainWindow::processCallableQueue()
{
bool empty;
callable c;
{ // narrow scope for lock guard
std::lock_guard<std::mutex> lg(m_mutexCallableQueue);
c = m_callableQueue.back();
m_callableQueue.pop_back();
empty = m_callableQueue.empty();
}
c();
if (!empty) {
// This gives other events a chance to be processed to keep the UI snappy.
QTimer::singleShot(0, this, SLOT(processCallableQueue()));
if (!m_taskQueue.empty()) {
auto c = m_taskQueue.pop();
c();
if (!m_taskQueue.empty()) {
QTimer::singleShot(0, this, SLOT(processCallableQueue()));
}
}
}
@ -148,6 +138,7 @@ void MainWindow::performQuery()
queryCancel = std::move(connection->getCancel());
QString command = ui->queryEdit->toPlainText();
std::thread([this,command]()
{
auto res = std::make_shared<Pgsql::Result>(connection->query(command));