Akonadi

akthread.cpp
1 /*
2  SPDX-FileCopyrightText: 2015 Daniel Vr├ítil <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "akthread.h"
8 #include "akonadiserver_debug.h"
9 #include "storage/datastore.h"
10 
11 using namespace Akonadi::Server;
12 
13 AkThread::AkThread(const QString &objectName, StartMode startMode, QThread::Priority priority, QObject *parent)
14  : QObject(parent)
15  , m_startMode(startMode)
16 {
18  if (startMode != NoThread) {
19  auto thread = new QThread();
20  thread->setObjectName(objectName + QStringLiteral("-Thread"));
22  thread->start(priority);
23  }
24 
25  if (startMode == AutoStart) {
26  startThread();
27  }
28 }
29 
30 AkThread::AkThread(const QString &objectName, QThread::Priority priority, QObject *parent)
31  : AkThread(objectName, AutoStart, priority, parent)
32 {
33 }
34 
35 AkThread::~AkThread() = default;
36 
37 void AkThread::startThread()
38 {
39  Q_ASSERT(m_startMode != NoThread);
40  const bool init = QMetaObject::invokeMethod(this, &AkThread::init, Qt::QueuedConnection);
41  Q_ASSERT(init);
42  Q_UNUSED(init)
43 }
44 
45 void AkThread::quitThread()
46 {
47  if (m_startMode == NoThread) {
48  return;
49  }
50  qCDebug(AKONADISERVER_LOG) << "Shutting down" << objectName() << "...";
51  const bool invoke = QMetaObject::invokeMethod(this, &AkThread::quit, Qt::QueuedConnection);
52 
53  Q_ASSERT(invoke);
54  Q_UNUSED(invoke)
55  if (!thread()->wait(10 * 1000)) {
56  thread()->terminate();
57  thread()->wait();
58  }
59  delete thread();
60 }
61 
62 void AkThread::init()
63 {
64  Q_ASSERT(thread() == QThread::currentThread());
65 }
66 
67 void AkThread::quit()
68 {
69  Q_ASSERT(thread() == QThread::currentThread());
70 
71  if (DataStore::hasDataStore()) {
72  DataStore::self()->close();
73  }
74 
75  if (m_startMode != NoThread) {
76  thread()->quit();
77  }
78 }
QCA_EXPORT void init()
void moveToThread(QThread *targetThread)
QThread * thread() const const
void start(QThread::Priority priority)
QThread * currentThread()
QueuedConnection
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)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jul 2 2022 06:41:47 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.