Akonadi

akthread.cpp
1/*
2 SPDX-FileCopyrightText: 2015 Daniel Vrátil <dvratil@kde.org>
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#include <QAbstractEventDispatcher>
12
13#include <chrono>
14#include <thread>
15
16using namespace std::chrono_literals;
17using namespace Akonadi::Server;
18
19AkThread::AkThread(const QString &objectName, StartMode startMode, QThread::Priority priority, QObject *parent)
20 : QObject(parent)
21 , m_startMode(startMode)
22{
23 setObjectName(objectName);
24 if (startMode != NoThread) {
25 auto thread = new QThread();
26 thread->setObjectName(objectName + QStringLiteral("-Thread"));
27 moveToThread(thread);
28 thread->start(priority);
29 }
30}
31
32AkThread::AkThread(const QString &objectName, QThread::Priority priority, QObject *parent)
33 : AkThread(objectName, AutoStart, priority, parent)
34{
35}
36
37AkThread::~AkThread() = default;
38
39void AkThread::waitForInitialized()
40{
41 while (!m_initialized) {
42 if (auto *dispatcher = QThread::currentThread()->eventDispatcher(); dispatcher != nullptr) {
43 dispatcher->processEvents(QEventLoop::AllEvents);
44 }
45 std::this_thread::sleep_for(50ms);
46 }
47}
48
49void AkThread::startThread()
50{
51 Q_ASSERT(m_startMode != NoThread);
53 this,
54 [this]() {
55 init();
56 m_initialized = true;
57 Q_EMIT initialized();
58 },
60}
61
62void AkThread::quitThread()
63{
64 if (m_quitCalled) {
65 return;
66 }
67 m_quitCalled = true;
68
69 if (m_startMode == NoThread) {
70 quit();
71 return;
72 }
73 qCDebug(AKONADISERVER_LOG) << "Shutting down" << objectName() << "...";
74 const bool invoke = QMetaObject::invokeMethod(this, &AkThread::quit, Qt::QueuedConnection);
75
76 Q_ASSERT(invoke);
77 Q_UNUSED(invoke)
78 if (!thread()->wait(10 * 1000)) {
79 thread()->terminate();
80 thread()->wait();
81 }
82 delete thread();
83}
84
85void AkThread::init()
86{
88}
89
90void AkThread::quit()
91{
93
96 }
97
98 if (m_startMode != NoThread) {
99 thread()->quit();
100 }
101}
102
103#include "moc_akthread.cpp"
void close()
Closes the database connection.
static DataStore * self()
Per thread singleton.
static bool hasDataStore()
Returns whether per thread DataStore has been created.
bool invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret)
Q_EMITQ_EMIT
T qobject_cast(QObject *object)
QThread * thread() const const
QueuedConnection
QThread * currentThread()
void quit()
void terminate()
bool wait(QDeadlineTimer deadline)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:38 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.