Akonadi

akthread.cpp
1 /*
2  Copyright (c) 2015 Daniel Vrátil <[email protected]>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "akthread.h"
21 #include "storage/datastore.h"
22 #include "akonadiserver_debug.h"
23 
24 #include <QThread>
25 
26 using namespace Akonadi::Server;
27 
28 AkThread::AkThread(const QString &objectName, StartMode startMode, QThread::Priority priority, QObject *parent)
29  : QObject(parent), m_startMode(startMode)
30 {
31  setObjectName(objectName);
32  if (startMode != NoThread) {
33  QThread *thread = new QThread();
34  thread->setObjectName(objectName + QStringLiteral("-Thread"));
35  moveToThread(thread);
36  thread->start(priority);
37  }
38 
39  if (startMode == AutoStart) {
40  startThread();
41  }
42 }
43 
44 AkThread::AkThread(const QString &objectName, QThread::Priority priority, QObject *parent)
45  : AkThread(objectName, AutoStart, priority, parent)
46 {
47 }
48 
49 AkThread::~AkThread() = default;
50 
51 void AkThread::startThread()
52 {
53  Q_ASSERT(m_startMode != NoThread);
54  const bool init = QMetaObject::invokeMethod(this, &AkThread::init, Qt::QueuedConnection);
55  Q_ASSERT(init);
56  Q_UNUSED(init);
57 }
58 
59 void AkThread::quitThread()
60 {
61  if (m_startMode == NoThread) {
62  return;
63  }
64  qCDebug(AKONADISERVER_LOG) << "Shutting down" << objectName() << "...";
65  const bool invoke = QMetaObject::invokeMethod(this, &AkThread::quit, Qt::QueuedConnection);
66 
67  Q_ASSERT(invoke);
68  Q_UNUSED(invoke);
69  if (!thread()->wait(10 * 1000)) {
70  thread()->terminate();
71  thread()->wait();
72  }
73  delete thread();
74 }
75 
76 void AkThread::init()
77 {
78  Q_ASSERT(thread() == QThread::currentThread());
79 }
80 
81 void AkThread::quit()
82 {
83  Q_ASSERT(thread() == QThread::currentThread());
84 
85  if (DataStore::hasDataStore()) {
86  DataStore::self()->close();
87  }
88 
89  if (m_startMode != NoThread) {
90  thread()->quit();
91  }
92 }
void moveToThread(QThread *targetThread)
QThread * thread() const const
void start(QThread::Priority priority)
void terminate()
void setObjectName(const QString &name)
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
bool wait(unsigned long time)
QThread * currentThread()
void quit()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue May 26 2020 22:46:17 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.