Akonadi

sessionthread.cpp
1 /*
2  Copyright (c) 2016 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 "sessionthread_p.h"
21 #include "session_p.h"
22 #include "akonadicore_debug.h"
23 
24 #include <QCoreApplication>
25 #include <QThread>
26 
27 Q_DECLARE_METATYPE(Akonadi::Connection::ConnectionType)
28 Q_DECLARE_METATYPE(Akonadi::Connection *)
29 Q_DECLARE_METATYPE(Akonadi::CommandBuffer *)
30 
31 using namespace Akonadi;
32 
33 SessionThread::SessionThread(QObject *parent)
34  : QObject(parent)
35 {
36  qRegisterMetaType<Connection::ConnectionType>();
37  qRegisterMetaType<Connection *>();
38  qRegisterMetaType<CommandBuffer *>();
39 
40  QThread *thread = new QThread();
41  thread->setObjectName(QStringLiteral("SessionThread"));
42  moveToThread(thread);
43  thread->start();
44 }
45 
46 SessionThread::~SessionThread()
47 {
48  QMetaObject::invokeMethod(this, &SessionThread::doThreadQuit, Qt::QueuedConnection);
49  if (!thread()->wait(10 * 1000)) {
50  thread()->terminate();
51  // Make sure to wait until it's done, otherwise it can crash when the pthread callback is called
52  thread()->wait();
53  }
54  delete thread();
55 }
56 
57 void SessionThread::addConnection(Connection *connection)
58 {
59  connection->moveToThread(thread());
60  const bool invoke = QMetaObject::invokeMethod(this, "doAddConnection",
61  Qt::BlockingQueuedConnection,
62  Q_ARG(Akonadi::Connection*, connection));
63  Q_ASSERT(invoke); Q_UNUSED(invoke);
64 }
65 
66 void SessionThread::doAddConnection(Connection *connection)
67 {
68  Q_ASSERT(thread() == QThread::currentThread());
69  Q_ASSERT(!mConnections.contains(connection));
70 
71  connect(connection, &QObject::destroyed,
72  this, [this](QObject * obj) {
73  mConnections.removeOne(static_cast<Connection *>(obj));
74  });
75  mConnections.push_back(connection);
76 }
77 
78 void SessionThread::destroyConnection(Connection *connection)
79 {
81  return;
82  const bool invoke = QMetaObject::invokeMethod(this, "doDestroyConnection",
83  Qt::BlockingQueuedConnection,
84  Q_ARG(Akonadi::Connection*, connection));
85  Q_ASSERT(invoke); Q_UNUSED(invoke);
86 }
87 
88 void SessionThread::doDestroyConnection(Connection *connection)
89 {
90  Q_ASSERT(thread() == QThread::currentThread());
91  Q_ASSERT(mConnections.contains(connection));
92 
93  connection->disconnect(this);
94  connection->doCloseConnection();
95  mConnections.removeAll(connection);
96  delete connection;
97 }
98 
99 void SessionThread::doThreadQuit()
100 {
101  Q_ASSERT(thread() == QThread::currentThread());
102 
103  for (Connection *conn : qAsConst(mConnections)) {
104  conn->disconnect(this);
105  conn->doCloseConnection(); // we can call directly because we are in the correct thread
106  delete conn;
107  }
108 
109  thread()->quit();
110 }
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)
QThread * currentThread()
Helper integration between Akonadi and Qt.
void destroyed(QObject *obj)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:08:56 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.