|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0"" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 #include "cptaskexecutor.h" |
|
18 #include <QMutexLocker> |
|
19 #include <QDebug> |
|
20 #include <QtAlgorithms> |
|
21 #include <QPointer> |
|
22 |
|
23 static QPointer<CpTaskExecutor> g_instance; |
|
24 |
|
25 CpTaskExecutor *CpTaskExecutor::globalInstance() |
|
26 { |
|
27 if (!g_instance) { |
|
28 g_instance = new CpTaskExecutor(); |
|
29 } |
|
30 return g_instance.data(); |
|
31 } |
|
32 |
|
33 void CpTaskExecutor::destroyGlobalInstance() |
|
34 { |
|
35 delete g_instance.data(); |
|
36 } |
|
37 |
|
38 CpTaskExecutor::CpTaskExecutor(QObject *parent /*=0*/) |
|
39 : QThread(parent), mStopped(false) |
|
40 { |
|
41 } |
|
42 |
|
43 CpTaskExecutor::~CpTaskExecutor() |
|
44 { |
|
45 stop(); |
|
46 } |
|
47 |
|
48 bool CpTaskExecutor::runTask(CpTask *task,bool append /*= false*/) |
|
49 { |
|
50 if (mStopped) { |
|
51 if (isRunning()) { |
|
52 qDebug() << "thread in stopping process...can not run task.\r\n"; |
|
53 return false; |
|
54 } |
|
55 mStopped = false; |
|
56 } |
|
57 |
|
58 if (task) { |
|
59 { |
|
60 QMutexLocker locker(&mMutex); |
|
61 if (append) { |
|
62 mTasks.append(task); |
|
63 } |
|
64 else { |
|
65 mTasks.insert(0,task); |
|
66 } |
|
67 } |
|
68 |
|
69 if (!isRunning()) { |
|
70 qDebug() << "isRunning() == false, call start()\r\n"; |
|
71 start(); |
|
72 } |
|
73 } |
|
74 |
|
75 return true; |
|
76 } |
|
77 |
|
78 void CpTaskExecutor::stop() |
|
79 { |
|
80 if (!mStopped && isRunning() ) { |
|
81 mStopped = true; |
|
82 removeTasks(); |
|
83 wait(); //wait for finished |
|
84 } |
|
85 } |
|
86 |
|
87 void CpTaskExecutor::removeTasks() |
|
88 { |
|
89 QMutexLocker locker(&mMutex); |
|
90 |
|
91 QList<CpTask*>::const_iterator begin(mTasks.begin()); |
|
92 QList<CpTask*>::const_iterator end(mTasks.end()); |
|
93 for (; begin != end; ++begin) { |
|
94 if ((*begin)->autoDelete()) { |
|
95 delete (*begin); |
|
96 } |
|
97 } |
|
98 |
|
99 mTasks.clear(); |
|
100 } |
|
101 |
|
102 void CpTaskExecutor::run() |
|
103 { |
|
104 while (!mStopped) { |
|
105 CpTask *task = 0; |
|
106 { |
|
107 QMutexLocker locker(&mMutex); |
|
108 if (mTasks.isEmpty()) { |
|
109 break; |
|
110 } |
|
111 task = mTasks.takeFirst(); |
|
112 } |
|
113 try { |
|
114 task->execute(&mStopped); |
|
115 task->complete(&mStopped); |
|
116 }catch(...) { |
|
117 qDebug() << "excetion occur when running task\r\n"; |
|
118 } |
|
119 |
|
120 if (task->autoDelete()) { |
|
121 delete task; |
|
122 } |
|
123 } |
|
124 } |
|
125 |
|
126 bool CpTaskExecutor::toFront(CpTask *task) |
|
127 { |
|
128 QMutexLocker locker(&mMutex); |
|
129 QList<CpTask*>::iterator foundIterator = ::qFind(mTasks.begin(),mTasks.end(),task); |
|
130 if (foundIterator == mTasks.end()) { |
|
131 return false; |
|
132 } |
|
133 |
|
134 mTasks.erase(foundIterator); |
|
135 mTasks.insert(0,task); |
|
136 |
|
137 return true; |
|
138 } |
|
139 |
|
140 //End of File |