VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/globals/UIThreadPool.cpp@ 96407

Last change on this file since 96407 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.9 KB
Line 
1/* $Id: UIThreadPool.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIThreadPool class implementation.
4 */
5
6/*
7 * Copyright (C) 2013-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28/* Qt includes: */
29#include <QThread>
30
31/* GUI includes: */
32#include "COMDefs.h"
33#include "UIDefs.h"
34#include "UITask.h"
35#include "UIThreadPool.h"
36
37/* Other VBox includes: */
38#include <iprt/assert.h>
39
40
41/** QThread extension used as worker-thread.
42 * Capable of executing COM-related tasks. */
43class UIThreadWorker : public QThread
44{
45 Q_OBJECT;
46
47signals:
48
49 /** Notifies listeners about @a pWorker finished. */
50 void sigFinished(UIThreadWorker *pWorker);
51
52public:
53
54 /** Constructs worker-thread for parent worker-thread @a pPool.
55 * @param iIndex Brings worker-thread index within the worker-thread pool registry. */
56 UIThreadWorker(UIThreadPool *pPool, int iIndex);
57
58 /** Returns worker-thread index within the worker-thread pool registry. */
59 int index() const { return m_iIndex; }
60
61 /** Disables sigFinished signal, for optimizing worker-thread pool termination. */
62 void setNoFinishedSignal() { m_fNoFinishedSignal = true; }
63
64private:
65
66 /** Contains the worker-thread body. */
67 void run();
68
69 /** Holds the worker-thread pool reference. */
70 UIThreadPool *m_pPool;
71
72 /** Holds the worker-thread index within the worker-thread pool registry. */
73 int m_iIndex;
74
75 /** Holds whether sigFinished signal should be emitted or not. */
76 bool m_fNoFinishedSignal;
77};
78
79
80/*********************************************************************************************************************************
81* Class UIThreadPool implementation. *
82*********************************************************************************************************************************/
83
84UIThreadPool::UIThreadPool(ulong cMaxWorkers /* = 3 */, ulong cMsWorkerIdleTimeout /* = 5000 */)
85 : m_cMsIdleTimeout(cMsWorkerIdleTimeout)
86 , m_workers(cMaxWorkers)
87 , m_cWorkers(0)
88 , m_cIdleWorkers(0)
89 , m_fTerminating(false)
90{
91}
92
93UIThreadPool::~UIThreadPool()
94{
95 /* Set termination status: */
96 setTerminating();
97
98 /* Lock initially: */
99 m_everythingLocker.lock();
100
101 /* Cleanup all the workers: */
102 for (int idxWorker = 0; idxWorker < m_workers.size(); ++idxWorker)
103 {
104 /* Acquire the worker: */
105 UIThreadWorker *pWorker = m_workers.at(idxWorker);
106 /* Remove it from the registry: */
107 m_workers[idxWorker] = 0;
108
109 /* Clean up the worker, if there was one: */
110 if (pWorker)
111 {
112 /* Decrease the number of workers: */
113 --m_cWorkers;
114 /* Unlock temporary to let the worker finish: */
115 m_everythingLocker.unlock();
116 /* Wait for the worker to finish: */
117 pWorker->wait();
118 /* Lock again: */
119 m_everythingLocker.lock();
120 /* Delete the worker finally: */
121 delete pWorker;
122 }
123 }
124
125 /* Cleanup all the tasks: */
126 qDeleteAll(m_pendingTasks);
127 qDeleteAll(m_executingTasks);
128 m_pendingTasks.clear();
129 m_executingTasks.clear();
130
131 /* Unlock finally: */
132 m_everythingLocker.unlock();
133}
134
135bool UIThreadPool::isTerminating() const
136{
137 /* Lock initially: */
138 m_everythingLocker.lock();
139
140 /* Acquire termination-flag: */
141 bool fTerminating = m_fTerminating;
142
143 /* Unlock finally: */
144 m_everythingLocker.unlock();
145
146 /* Return termination-flag: */
147 return fTerminating;
148}
149
150void UIThreadPool::setTerminating()
151{
152 /* Lock initially: */
153 m_everythingLocker.lock();
154
155 /* Assign termination-flag: */
156 m_fTerminating = true;
157
158 /* Tell all threads to NOT queue any termination signals: */
159 for (int idxWorker = 0; idxWorker < m_workers.size(); ++idxWorker)
160 {
161 UIThreadWorker *pWorker = m_workers.at(idxWorker);
162 if (pWorker)
163 pWorker->setNoFinishedSignal();
164 }
165
166 /* Wake up all idle worker threads: */
167 m_taskCondition.wakeAll();
168
169 /* Unlock finally: */
170 m_everythingLocker.unlock();
171}
172
173void UIThreadPool::enqueueTask(UITask *pTask)
174{
175 /* Do nothing if terminating: */
176 AssertReturnVoid(!isTerminating());
177
178 /* Prepare task: */
179 connect(pTask, &UITask::sigComplete,
180 this, &UIThreadPool::sltHandleTaskComplete, Qt::QueuedConnection);
181
182 /* Lock initially: */
183 m_everythingLocker.lock();
184
185 /* Put the task into the queue: */
186 m_pendingTasks.enqueue(pTask);
187
188 /* Wake up an idle worker if we got one: */
189 if (m_cIdleWorkers > 0)
190 {
191 m_taskCondition.wakeOne();
192 }
193 /* No idle worker threads, should we create a new one? */
194 else if (m_cWorkers < m_workers.size())
195 {
196 /* Find free slot: */
197 int idxFirstUnused = m_workers.size();
198 while (idxFirstUnused-- > 0)
199 if (m_workers.at(idxFirstUnused) == 0)
200 {
201 /* Prepare the new worker: */
202 UIThreadWorker *pWorker = new UIThreadWorker(this, idxFirstUnused);
203 connect(pWorker, &UIThreadWorker::sigFinished,
204 this, &UIThreadPool::sltHandleWorkerFinished, Qt::QueuedConnection);
205 m_workers[idxFirstUnused] = pWorker;
206 ++m_cWorkers;
207
208 /* And start it: */
209 pWorker->start();
210 break;
211 }
212 }
213 /* else: wait for some worker to complete
214 * whatever it's busy with and jump to it. */
215
216 /* Unlock finally: */
217 m_everythingLocker.unlock();
218}
219
220UITask *UIThreadPool::dequeueTask(UIThreadWorker *pWorker)
221{
222 /* Lock initially: */
223 m_everythingLocker.lock();
224
225 /* Dequeue a task, watching out for terminations.
226 * For optimal efficiency in enqueueTask() we keep count of idle threads.
227 * If the wait times out, we'll return 0 and terminate the thread. */
228 bool fIdleTimedOut = false;
229 while (!m_fTerminating)
230 {
231 /* Make sure that worker has proper index: */
232 Assert(m_workers.at(pWorker->index()) == pWorker);
233
234 /* Dequeue task if there is one: */
235 if (!m_pendingTasks.isEmpty())
236 {
237 UITask *pTask = m_pendingTasks.dequeue();
238 if (pTask)
239 {
240 /* Put into the set of executing tasks: */
241 m_executingTasks << pTask;
242
243 /* Unlock finally: */
244 m_everythingLocker.unlock();
245
246 /* Return dequeued task: */
247 return pTask;
248 }
249 }
250
251 /* If we timed out already, then quit the worker thread. To prevent a
252 * race between enqueueTask and the queue removal of the thread from
253 * the workers vector, we remove it here already. (This does not apply
254 * to the termination scenario.) */
255 if (fIdleTimedOut)
256 {
257 m_workers[pWorker->index()] = 0;
258 --m_cWorkers;
259 break;
260 }
261
262 /* Wait for a task or timeout: */
263 ++m_cIdleWorkers;
264 fIdleTimedOut = !m_taskCondition.wait(&m_everythingLocker, m_cMsIdleTimeout);
265 --m_cIdleWorkers;
266 }
267
268 /* Unlock finally: */
269 m_everythingLocker.unlock();
270
271 /* Return 0 by default: */
272 return 0;
273}
274
275void UIThreadPool::sltHandleTaskComplete(UITask *pTask)
276{
277 /* Skip on termination: */
278 if (isTerminating())
279 return;
280
281 /* Notify listeners: */
282 emit sigTaskComplete(pTask);
283
284 /* Lock initially: */
285 m_everythingLocker.lock();
286
287 /* Delete task finally: */
288 if ( !m_executingTasks.contains(pTask)
289 || !m_executingTasks.remove(pTask))
290 AssertMsgFailed(("Unable to find or remove complete task!"));
291 delete pTask;
292
293 /* Unlock finally: */
294 m_everythingLocker.unlock();
295}
296
297void UIThreadPool::sltHandleWorkerFinished(UIThreadWorker *pWorker)
298{
299 /* Wait for the thread to finish completely, then delete the thread
300 * object. We have already removed the thread from the workers vector.
301 * Note! We don't want to use 'this' here, in case it's invalid. */
302 pWorker->wait();
303 delete pWorker;
304}
305
306
307/*********************************************************************************************************************************
308* Class UIThreadWorker implementation. *
309*********************************************************************************************************************************/
310
311UIThreadWorker::UIThreadWorker(UIThreadPool *pPool, int iIndex)
312 : m_pPool(pPool)
313 , m_iIndex(iIndex)
314 , m_fNoFinishedSignal(false)
315{
316}
317
318void UIThreadWorker::run()
319{
320 /* Initialize COM: */
321 COMBase::InitializeCOM(false);
322
323 /* Try get a task from the pool queue: */
324 while (UITask *pTask = m_pPool->dequeueTask(this))
325 {
326 /* Process the task if we are not terminating.
327 * Please take into account tasks are cleared by the UIThreadPool
328 * after all listeners notified about task is complete and handled it. */
329 if (!m_pPool->isTerminating())
330 pTask->start();
331 }
332
333 /* Cleanup COM: */
334 COMBase::CleanupCOM();
335
336 /* Queue a signal for the pool to do thread cleanup, unless the pool is
337 already terminating and doesn't need the signal. */
338 if (!m_fNoFinishedSignal)
339 emit sigFinished(this);
340}
341
342
343#include "UIThreadPool.moc"
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette