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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295 | //! \file CubitQtConcurrentApi.cpp
#include "CubitQtConcurrentApi.h"
#include <QtConcurrentRun>
#include <QtConcurrentMap>
#include <QFutureSynchronizer>
#include <QFutureWatcher>
#include <QEventLoop>
#include <QThreadStorage>
#include <QCoreApplication>
namespace {
class QtMutex : public QMutex, public CubitConcurrent::Mutex
{
public:
//QtMutex() : QMutex(QMutex::Recursive) {}
QtMutex() : QMutex() {}
~QtMutex() {}
void lock()<--- Function in derived class
{
QMutex::lock();
}
void unlock()<--- Function in derived class
{
QMutex::unlock();
}
};
struct TLSWrapper
{
TLSWrapper(void* p, void (*cleanup)(void*)) : mP(p) {}<--- Member variable 'TLSWrapper::mCleanup' is not initialized in the constructor.
~TLSWrapper()
{
(*mCleanup)(mP);
}
void* mP;
void (*mCleanup)(void*);
};
struct QtTLS : public QThreadStorage<TLSWrapper*>, public CubitConcurrent::ThreadLocalStorageInterface
{
QtTLS(void (*cleanup)(void*)) : mCleanup(cleanup)<--- Struct 'QtTLS' has a constructor with 1 argument that is not explicit. [+]Struct 'QtTLS' has a constructor with 1 argument that is not explicit. Such constructors should in general be explicit for type safety reasons. Using the explicit keyword in the constructor means some mistakes when using the class can be avoided.
{
}
void* local_data()<--- Function in derived class<--- The function 'local_data' is never used.
{
return localData()->mP;
}
void set_local_data(void* p)<--- Function in derived class<--- The function 'set_local_data' is never used.
{
TLSWrapper* w = new TLSWrapper(p, mCleanup);
setLocalData(w);
}
void (*mCleanup)(void*);
};
}
CubitQtConcurrent::CubitQtConcurrent()
{
_name = "CubitQtConcurrent";<--- Variable '_name' is assigned in constructor body. Consider performing initialization in initialization list. [+]When an object of a class is created, the constructors of all member variables are called consecutively in the order the variables are declared, even if you don't explicitly write them to the initialization list. You could avoid assigning '_name' a value by passing the value to the constructor in the initialization list.
// If there is no global instance, set this object as the instance.
if(!CubitConcurrent::mInstance)
CubitConcurrent::mInstance = this;
}
CubitQtConcurrent::~CubitQtConcurrent()
{
// If this is the global instance, clear the pointer.
if(this == CubitConcurrent::mInstance)
CubitConcurrent::mInstance = 0;
}
const std::string& CubitQtConcurrent::get_name() const<--- The function 'get_name' is never used.
{
return _name;
}
const char* CubitQtConcurrent::get_type() const
{
return _name.c_str();
}
/*
CubitConcurrent::Mutex* CubitQtConcurrent::create_mutex()
{
return new QtMutex;
}
void CubitQtConcurrent::destroy_mutex(CubitConcurrent::Mutex* m)
{
delete static_cast<QtMutex*>(m);
}
*/
CubitConcurrent::ThreadLocalStorageInterface* CubitQtConcurrent::create_local_storage(void (*cleanup_function)(void*))<--- The function 'create_local_storage' is never used.
{
return new QtTLS(cleanup_function);
}
void CubitQtConcurrent::destroy_local_storage(ThreadLocalStorageInterface* i)<--- The function 'destroy_local_storage' is never used.
{
delete static_cast<QtTLS*>(i);
}
void CubitQtConcurrent::schedule(CubitConcurrent::Task* task)
{
QFuture<void> f = ::QtConcurrent::run(task, &Task::execute);
m.lock();
taskmap[task] = f;
m.unlock();
}
void CubitQtConcurrent::wait(CubitConcurrent::Task* task)
{
m.lock();
QMap<CubitConcurrent::Task*, QFuture<void> >::iterator iter = taskmap.find(task);
m.unlock();
iter->waitForFinished();
m.lock();
taskmap.erase(iter);
m.unlock();
}
void CubitQtConcurrent::idle_wait(CubitConcurrent::Task* task)<--- The function 'idle_wait' is never used.
{
m.lock();
QMap<CubitConcurrent::Task*, QFuture<void> >::iterator iter = taskmap.find(task);
m.unlock();
if(!iter->isFinished())
{
QEventLoop loop;
QFutureWatcher<void> watcher;
watcher.setFuture(*iter);
QObject::connect(&watcher, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
}
m.lock();
taskmap.erase(iter);
m.unlock();
}
void CubitQtConcurrent::wait_for_any(const std::vector<CubitConcurrent::Task*>& tasks,std::vector<CubitConcurrent::Task*>& finished_tasks)<--- The function 'wait_for_any' is never used.
{
m.lock();
for(size_t i=0; i<tasks.size(); i++)
{
QMap<CubitConcurrent::Task*, QFuture<void> >::iterator iter = taskmap.find(tasks[i]);
if(iter->isFinished())
{
finished_tasks.push_back(tasks[i]);
taskmap.erase(iter);
}
}
m.unlock();
if(!finished_tasks.empty())
return;
if(!QCoreApplication::instance())
{
int arg=0;
new QCoreApplication(arg,NULL);
}
QEventLoop evLoop;
m.lock();
for(size_t i=0; i<tasks.size(); i++)
{
QFutureWatcher<void> *f= new QFutureWatcher<void>(&evLoop);
QMap<CubitConcurrent::Task*, QFuture<void> >::iterator iter = taskmap.find(tasks[i]);
f->setFuture(*iter);
QObject::connect(f,SIGNAL(finished()),&evLoop,SLOT(quit()));
}
m.unlock();
evLoop.exec();
m.lock();
for(size_t i=0; i<tasks.size(); i++)
{
QMap<CubitConcurrent::Task*, QFuture<void> >::iterator iter = taskmap.find(tasks[i]);
if(iter->isFinished())
{
finished_tasks.push_back(tasks[i]);
taskmap.erase(iter);
}
}
m.unlock();
}
void CubitQtConcurrent::wait(const std::vector<CubitConcurrent::Task*>& task)
{
m.lock();
std::vector<QMap<CubitConcurrent::Task*, QFuture<void> >::iterator> iters;
QFutureSynchronizer<void> f;
for(size_t i=0; i<task.size(); i++)
{
QMap<CubitConcurrent::Task*, QFuture<void> >::iterator iter = taskmap.find(task[i]);
iters.push_back(iter);
f.addFuture(*iter);
}
m.unlock();
f.waitForFinished();
m.lock();
for(size_t i=0; i<iters.size(); i++)
{
taskmap.erase(iters[i]);
}
m.unlock();
}
bool CubitQtConcurrent::is_completed(CubitConcurrent::Task* task)<--- The function 'is_completed' is never used.
{
m.lock();
QMap<CubitConcurrent::Task*, QFuture<void> >::iterator iter = taskmap.find(task);
m.unlock();
return iter->isFinished();
}
bool CubitQtConcurrent::is_running(CubitConcurrent::Task* task)<--- The function 'is_running' is never used.
{
m.lock();
QMap<CubitConcurrent::Task*, QFuture<void> >::iterator iter = taskmap.find(task);
m.unlock();
return iter->isRunning();
}
// internal helper to start tasks within a task group
void CubitQtConcurrent::execute(CubitConcurrent::Task* t)
{
t->execute();
}
void CubitQtConcurrent::schedule(CubitConcurrent::TaskGroup* tg)
{
QFuture<void> f = ::QtConcurrent::map(tg->tasks, CubitQtConcurrent::execute);
m2.lock();
taskgroupmap[tg] = f;
m2.unlock();
}
void CubitQtConcurrent::wait(CubitConcurrent::TaskGroup* tg)
{
m2.lock();
QMap<CubitConcurrent::TaskGroup*, QFuture<void> >::iterator iter = taskgroupmap.find(tg);
m2.unlock();
iter->waitForFinished();
m2.lock();
taskgroupmap.erase(iter);
m2.unlock();
}
bool CubitQtConcurrent::is_completed(CubitConcurrent::TaskGroup* tg)
{
m2.lock();
QMap<CubitConcurrent::TaskGroup*, QFuture<void> >::iterator iter = taskgroupmap.find(tg);
m2.unlock();
return iter->isFinished();
}
bool CubitQtConcurrent::is_running(CubitConcurrent::TaskGroup* tg)
{
m2.lock();
QMap<CubitConcurrent::TaskGroup*, QFuture<void> >::iterator iter = taskgroupmap.find(tg);
m2.unlock();
return iter->isRunning();
}
void CubitQtConcurrent::cancel(CubitConcurrent::TaskGroup* tg)
{
m2.lock();
QMap<CubitConcurrent::TaskGroup*, QFuture<void> >::iterator iter = taskgroupmap.find(tg);
m2.unlock();
iter->cancel();
}
|