1 | /* $Id: UIThreadPool.h 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIThreadPool class declaration.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2023 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 | #ifndef FEQT_INCLUDED_SRC_globals_UIThreadPool_h
|
---|
29 | #define FEQT_INCLUDED_SRC_globals_UIThreadPool_h
|
---|
30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
31 | # pragma once
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | /* Qt includes: */
|
---|
35 | #include <QMutex>
|
---|
36 | #include <QObject>
|
---|
37 | #include <QQueue>
|
---|
38 | #include <QSet>
|
---|
39 | #include <QVector>
|
---|
40 | #include <QWaitCondition>
|
---|
41 |
|
---|
42 | /* GUI includes: */
|
---|
43 | #include "UILibraryDefs.h"
|
---|
44 |
|
---|
45 | /* Forward declarations: */
|
---|
46 | class UITask;
|
---|
47 | class UIThreadWorker;
|
---|
48 |
|
---|
49 | /** QObject extension used as worker-thread pool.
|
---|
50 | * Schedules COM-related GUI tasks to multiple worker-threads. */
|
---|
51 | class SHARED_LIBRARY_STUFF UIThreadPool : public QObject
|
---|
52 | {
|
---|
53 | Q_OBJECT;
|
---|
54 |
|
---|
55 | signals:
|
---|
56 |
|
---|
57 | /** Notifies listeners about @a pTask complete. */
|
---|
58 | void sigTaskComplete(UITask *pTask);
|
---|
59 |
|
---|
60 | public:
|
---|
61 |
|
---|
62 | /** Constructs worker-thread pool.
|
---|
63 | * @param cMaxWorkers Brings the maximum amount of worker-threads.
|
---|
64 | * @param cMsWorkerIdleTimeout Brings the maximum amount of time (in ms) which
|
---|
65 | * pool will wait for the worker-thread on cleanup. */
|
---|
66 | UIThreadPool(ulong cMaxWorkers = 3, ulong cMsWorkerIdleTimeout = 5000);
|
---|
67 | /** Destructs worker-thread pool. */
|
---|
68 | virtual ~UIThreadPool() RT_OVERRIDE;
|
---|
69 |
|
---|
70 | /** Returns whether the 'termination sequence' is started. */
|
---|
71 | bool isTerminating() const;
|
---|
72 | /** Defines that the 'termination sequence' is started. */
|
---|
73 | void setTerminating();
|
---|
74 |
|
---|
75 | /** Enqueues @a pTask into the task-queue. */
|
---|
76 | void enqueueTask(UITask *pTask);
|
---|
77 | /** Returns dequeued top-most task from the task-queue. */
|
---|
78 | UITask *dequeueTask(UIThreadWorker *pWorker);
|
---|
79 |
|
---|
80 | private slots:
|
---|
81 |
|
---|
82 | /** Handles @a pTask complete signal. */
|
---|
83 | void sltHandleTaskComplete(UITask *pTask);
|
---|
84 |
|
---|
85 | /** Handles @a pWorker finished signal. */
|
---|
86 | void sltHandleWorkerFinished(UIThreadWorker *pWorker);
|
---|
87 |
|
---|
88 | private:
|
---|
89 |
|
---|
90 | /** @name Worker-thread stuff.
|
---|
91 | * @{ */
|
---|
92 | /** Holds the maximum amount of time (in ms) which
|
---|
93 | * pool will wait for the worker-thread on cleanup. */
|
---|
94 | const ulong m_cMsIdleTimeout;
|
---|
95 | /** Holds the vector of worker-threads. */
|
---|
96 | QVector<UIThreadWorker*> m_workers;
|
---|
97 | /** Holds the number of worker-threads.
|
---|
98 | * @remarks We cannot use the vector size since it may contain 0 pointers. */
|
---|
99 | int m_cWorkers;
|
---|
100 | /** Holds the number of idle worker-threads. */
|
---|
101 | int m_cIdleWorkers;
|
---|
102 | /** Holds whether the 'termination sequence' is started
|
---|
103 | * and all worker-threads should terminate ASAP. */
|
---|
104 | bool m_fTerminating;
|
---|
105 | /** @} */
|
---|
106 |
|
---|
107 | /** @name Task stuff
|
---|
108 | * @{ */
|
---|
109 | /** Holds the queue of pending tasks. */
|
---|
110 | QQueue<UITask*> m_pendingTasks;
|
---|
111 | /** Holds the set of executing tasks. */
|
---|
112 | QSet<UITask*> m_executingTasks;
|
---|
113 | /** Holds the condition variable that gets signalled when
|
---|
114 | * queuing a new task and there are idle worker threads around.
|
---|
115 | * @remarks Idle threads sits in dequeueTask waiting for this.
|
---|
116 | * Thus on thermination, setTerminating() will send a
|
---|
117 | * broadcast signal to wake up all workers (after
|
---|
118 | * setting m_fTerminating of course). */
|
---|
119 | QWaitCondition m_taskCondition;
|
---|
120 | /** @} */
|
---|
121 |
|
---|
122 | /** Holds the guard mutex object protecting
|
---|
123 | * all the inter-thread variables. */
|
---|
124 | mutable QMutex m_everythingLocker;
|
---|
125 | };
|
---|
126 |
|
---|
127 | #endif /* !FEQT_INCLUDED_SRC_globals_UIThreadPool_h */
|
---|