Debian Codegraph @

src/serviceframework/qserviceoperations.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSystems module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qserviceoperations_p.h"
41 #include "qservicerequest_p.h"
42 #include "qservicereply.h"
43 #include "qservicedebuglog_p.h"
44 #include "qservicereply_p.h"
45
46 Q_GLOBAL_STATIC(QServiceOperations, q_service_operations_object)
47
48 //// Implementation notes: The background thread is implemented as a facade
49 //// behind which the actual worker object is hidden. The facade - an instance
50 //// of QServiceOperations - lives in (has QObject affinity to) the *foreground*
51 //// calling thread.
52 ////
53 //// The worker object, an instance of QServiceOperationProcessor, is created
54 //// on the stack inside the run() method of the QThread, thus ensuring it does
55 //// not need to be cleaned up by some deleter. Since it is created in the
56 //// run() it lives in the *background* thread, and connections to it from the
57 //// facade (QServiceOperations) are thus queued connections.
58 ////
59 //// Signal-slot connections are thus two-level indirection, but what this means
60 //// is that no locking is needed in QServiceOperationProcessor since it can
61 //// only be accessed within its own thread, by calls that are serialised into
62 //// its slots by the signal-slot event queue.
63
64 QServiceOperations::QServiceOperations(QObject *parent)
65 : QThread(parent)
66 , m_engageCount(0)
67 {
68 // This call must be executed before the first signal-slot connection
69 // which uses a QServiceRequest. Since this constructor will need to fire
70 // before we have an instance of this class to connect anything to
71 // we should be OK here.
72 qRegisterMetaType<QServiceRequest>("QServiceRequest");
73 qRegisterMetaType<QServiceManager::Error>("QServiceManager::Error");
74
75 // created in the constructor otherwise we can signals/slots
76 QServiceOperationProcessor *processor = new QServiceOperationProcessor();
77 processor->moveToThread(this);
78
79 // This call creates a queued connection
80 connect(this, SIGNAL(newRequest(QServiceRequest)),
81 processor, SLOT(handleRequest(QServiceRequest)), Qt::QueuedConnection);
82 connect(this, SIGNAL(destroyed()),
83 processor, SLOT(deleteLater()));
84
85 qServiceLog() << "event" << "new"
86 << "class" << "QServiceOperations";
87 }
88
89 QServiceOperations::~QServiceOperations()
90 {
91 qServiceLog() << "event" << "delete"
92 << "class" << "QServiceOperations";
93 }
94
95 /*
96 Return the per-process instance of the thread facade object
97 */
98 QServiceOperations *QServiceOperations::instance()
99 {
100 qServiceLog() << "event" << "instance access"
101 << "class" << "QServiceOperations"
102 << "clients" << q_service_operations_object()->clientCount();
103
104 return q_service_operations_object();
105 }
106
107 /*
108 Calling code engages the thread facade, marking themselves as users of it.
109 The first caller will cause the thread to start. Client code should call
110 engage early during initialisation as the once-off thread startup cost could
111 cause latency for time critical operations if its done on-demand.
112 */
113 void QServiceOperations::engage()
114 {
115 if (m_engageCount.testAndSetAcquire(0, 1)) {
116 start();
117 } else {
118 m_engageCount.ref();
119 }
120 qServiceLog() << "event" << "engage"
121 << "class" << "QServiceOperations"
122 << "clients" << q_service_operations_object()->clientCount();
123 }
124
125 /*
126 Client code disengages from the thread, releasing themselves from use of it.
127 The last client to do so will cause the thread to quit, exiting the event
128 loop, and running the thread shutdown process. Normally this should be a
129 100ms or so, but if for some reason there are problems it could take longer.
130 This might be a bug, or exceptional condition, or some long running request
131 that just happens to be in flight at the time. The current implementation
132 will try 3 times to quit the thread (with a 500ms window each time) for a
133 total worst-case of 1500ms; before calling terminate on the thread. After
134 that the termination will still take a short period of time before its
135 safe to clean up the thread object.
136 */
137 void QServiceOperations::disengage()
138 {
139 qServiceLog() << "event" << "disengage"
140 << "class" << "QServiceOperations"
141 << "clients" << q_service_operations_object()->clientCount();
142
143 if (!m_engageCount.deref()) {
144 qServiceLog() << "event" << "shutdown"
145 << "class" << "QServiceOperations";
146 quit();
147 int triesLeft = 3;
148 bool exitedCleanly = false;
149 while (triesLeft) {
150 exitedCleanly = wait(500);
151 if (exitedCleanly)
152 break;
153 qWarning() << "Waiting for QServiceOperations background thread to exit...";
154 triesLeft--;
155 }
156 if (!exitedCleanly) {
157 qWarning() << "...forcing termination of QServiceOperations thread!";
158 terminate();
159 wait();
160 }
161 }
162 }
163
164 /*
165 Make a new request. This slot will be called synchronously from the client
166 code, since the client and the facade are in the same thread. However this
167 slot then forwards the request on via a queued connection to the internal
168 operations object. The queue automatically gives thread safety to the
169 processor meaning no locking is required.
170 */
171 void QServiceOperations::initiateRequest(const QServiceRequest &req)
172 {
173 qServiceLog() << "event" << "initiate"
174 << "class" << "QServiceOperations"
175 << "iface" << req.descriptor().interfaceName();
176
177 emit newRequest(req);
178 }
179
180 /*
181 This function will be called when start() is called in the engage() code above.
182 All the code in run() will execute in the background thread.
183 */
184 void QServiceOperations::run()
185 {
186 qServiceLog() << "event" << "run"
187 << "class" << "QServiceOperations"
188 << "priority" << (qint32)priority();
189
190 // spin the per-thread event loop
191 exec();
192 }
193
194
195 ////////////////////////////////////////////////////
196 ////
197 //// QServiceOperationProcessor implementation
198
199 /*
200 Constructor - stateless object at present.
201 */
202 QServiceOperationProcessor::QServiceOperationProcessor(QObject *parent)
203 : QObject(parent), inRequest(false)
204 {
205 qServiceLog() << "event" << "new"
206 << "class" << "QServiceOperationProc";
207 }
208
209 /*
210 Destructor - no resources. Should destruct when the main event
211 loop of the facade exits.
212 */
213 QServiceOperationProcessor::~QServiceOperationProcessor()
214 {
215 qServiceLog() << "event" << "delete"
216 << "class" << "QServiceOperationProc";
217 }
218
219 /*
220 Do the actual work. Note that all calls to this function are serialised by
221 the queued signal-slot connection from the facade. But since SFW could
222 spin the even loop we need to lock loadInterface so it's only called once
223 */
224 void QServiceOperationProcessor::handleRequest(const QServiceRequest &inrequest)
225 {
226 pendingList.append(inrequest);
227
228 if (inRequest) {
229 qServiceLog() << "event" << "already handle req"
230 << "class" << "QServiceOperationProc"
231 << "iface" << inrequest.descriptor().interfaceName();
232 return;
233 }
234
235 inRequest = true;
236
237 QServiceRequest req;
238
239 while (!pendingList.isEmpty()) {
240 req = pendingList.takeFirst();
241
242 qServiceLog() << "event" << "handle req start"
243 << "class" << "QServiceOperationProc"
244 << "iface" << inrequest.descriptor().interfaceName();
245
246 QServiceInterfaceDescriptor descriptor = req.descriptor();
247 QServiceReply *reply = req.reply();
248
249 QServiceManager mgr(req.scope());
250
251 QMetaObject::invokeMethod(reply, "start", Qt::QueuedConnection);
252
253 if (req.requestType() == QServiceRequest::DefaultInterfaceRequest) {
254 // OK, this was a request based on the interface name rather than a
255 // fully specified descriptor
256 qDebug() << "Asking for the default interface for" << mgr.interfaceDefault(req.interfaceName());
257 descriptor = mgr.interfaceDefault(req.interfaceName());
258 } else {
259 qDebug() << "NOT asking for default";
260 }
261
262 if (!descriptor.isValid()) {
263 qDebug() << "Failed to fetch default";
264 QMetaObject::invokeMethod(reply, "setError", Qt::QueuedConnection, Q_ARG(QServiceManager::Error, QServiceManager::InvalidServiceInterfaceDescriptor));
265 QMetaObject::invokeMethod(reply, "finish", Qt::QueuedConnection);
266 continue;
267 }
268
269 QObject *obj = 0;
270 int serviceType = descriptor.attribute(QServiceInterfaceDescriptor::ServiceType).toInt();
271 const QString location = descriptor.attribute(QServiceInterfaceDescriptor::Location).toString();
272
273 if (serviceType == QService::InterProcess) {
274 obj = mgr.loadInterProcessService(descriptor, location);
275 } else {
276
277 const QString serviceFilePath = mgr.resolveLibraryPath(location);
278 if (serviceFilePath.isEmpty()) {
279 QMetaObject::invokeMethod(reply, "setError", Qt::QueuedConnection, Q_ARG(QServiceManager::Error, QServiceManager::InvalidServiceLocation));
280 QMetaObject::invokeMethod(reply, "finish", Qt::QueuedConnection);
281 continue;
282 }
283
284 obj = mgr.loadInProcessService(descriptor, serviceFilePath);
285 if (!obj) {
286 QMetaObject::invokeMethod(reply, "setError", Qt::QueuedConnection, Q_ARG(QServiceManager::Error, QServiceManager::PluginLoadingFailed));
287 QMetaObject::invokeMethod(reply, "finish", Qt::QueuedConnection);
288 continue;
289 }
290 }
291
292 // loadInterface() can return null
293 if (obj) {
294 obj->moveToThread(reply->thread());
295 }
296
297 QMetaObject::invokeMethod(reply, "setProxyObject", Qt::QueuedConnection, Q_ARG(QObject*, obj));
298 QMetaObject::invokeMethod(reply, "finish", Qt::QueuedConnection);
299
300 qServiceLog() << "event" << "handle req done"
301 << "class" << "QServiceOperationProc"
302 << "iface" << inrequest.descriptor().interfaceName();
303 }
304
305 inRequest = false;
306 }