blob: 42b0f26cafe5ec7f5f073cf0c641b6e107757a7c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
/* $Id$ */
/** @file
* VBox Qt GUI - UIThreadPool and UITask classes declaration.
*/
/*
* Copyright (C) 2013-2017 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#ifndef ___UIThreadPool_h___
#define ___UIThreadPool_h___
/* Qt includes: */
#include <QObject>
#include <QMutex>
#include <QQueue>
#include <QSet>
#include <QVariant>
#include <QVector>
#include <QWaitCondition>
/* Forward declarations: */
class UIThreadWorker;
class UITask;
/** QObject extension used as worker-thread pool.
* Schedules COM-related GUI tasks to multiple worker-threads. */
class UIThreadPool : public QObject
{
Q_OBJECT;
signals:
/** Notifies listeners about @a pTask complete. */
void sigTaskComplete(UITask *pTask);
public:
/** Constructs worker-thread pool.
* @param cMaxWorkers defines the maximum amount of worker-threads.
* @param cMsWorkerIdleTimeout defines the maximum amount of time (in ms) which
* pool will wait for the worker-thread on cleanup. */
UIThreadPool(ulong cMaxWorkers = 3, ulong cMsWorkerIdleTimeout = 5000);
/** Destructs worker-thread pool. */
~UIThreadPool();
/** Returns whether the 'termination sequence' is started. */
bool isTerminating() const;
/** Defines that the 'termination sequence' is started. */
void setTerminating();
/** Enqueues @a pTask into the task-queue. */
void enqueueTask(UITask *pTask);
/** Returns dequeued top-most task from the task-queue.
* @remarks Called by the @a pWorker passed as a hint. */
UITask* dequeueTask(UIThreadWorker *pWorker);
private slots:
/** Handles @a pTask complete signal. */
void sltHandleTaskComplete(UITask *pTask);
/** Handles @a pWorker finished signal. */
void sltHandleWorkerFinished(UIThreadWorker *pWorker);
private:
/** @name Worker-thread stuff.
* @{ */
/** Holds the maximum amount of time (in ms) which
* pool will wait for the worker-thread on cleanup. */
const ulong m_cMsIdleTimeout;
/** Holds the vector of worker-threads. */
QVector<UIThreadWorker*> m_workers;
/** Holds the number of worker-threads.
* @remarks We cannot use the vector size since it may contain 0 pointers. */
int m_cWorkers;
/** Holds the number of idle worker-threads. */
int m_cIdleWorkers;
/** Holds whether the 'termination sequence' is started
* and all worker-threads should terminate ASAP. */
bool m_fTerminating;
/** @} */
/** @name Task stuff
* @{ */
/** Holds the queue of pending tasks. */
QQueue<UITask*> m_pendingTasks;
/** Holds the set of executing tasks. */
QSet<UITask*> m_executingTasks;
/** Holds the condition variable that gets signalled when
* queuing a new task and there are idle worker threads around.
* @remarks Idle threads sits in dequeueTask waiting for this.
* Thus on thermination, setTerminating() will send a
* broadcast signal to wake up all workers (after
* setting m_fTerminating of course). */
QWaitCondition m_taskCondition;
/** @} */
/** Holds the guard mutex object protecting
* all the inter-thread variables. */
mutable QMutex m_everythingLocker;
};
/** QObject extension used as worker-thread task interface.
* Describes task to be executed by the UIThreadWorker object. */
class UITask : public QObject
{
Q_OBJECT;
signals:
/** Notifies listeners about @a pTask complete. */
void sigComplete(UITask *pTask);
public:
/** Task types. */
enum Type
{
Type_MediumEnumeration = 1,
Type_DetailsPopulation = 2,
};
/** Constructs the task of passed @a type. */
UITask(UITask::Type type) : m_type(type) {}
/** Returns the type of the task. */
UITask::Type type() const { return m_type; }
/** Starts the task. */
void start();
protected:
/** Contains the abstract task body.
* @remarks To be reimplemented in sub-class. */
virtual void run() = 0;
private:
/** Holds the type of the task. */
const UITask::Type m_type;
};
#endif /* !___UIThreadPool_h___ */
|