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 #ifndef CPTASKEXECUTOR_H |
|
18 #define CPTASKEXECUTOR_H |
|
19 |
|
20 #include <cpcategoryglobal.h> |
|
21 #include <QThread> |
|
22 #include <QList> |
|
23 #include <QMutex> |
|
24 |
|
25 class CpTask |
|
26 { |
|
27 public: |
|
28 explicit CpTask(bool autoDelete = true) |
|
29 : mAutoDelete(autoDelete) |
|
30 { |
|
31 } |
|
32 |
|
33 virtual ~CpTask() |
|
34 { |
|
35 } |
|
36 |
|
37 bool autoDelete() const |
|
38 { |
|
39 return mAutoDelete; |
|
40 } |
|
41 |
|
42 virtual void execute(volatile bool *stopped) |
|
43 { |
|
44 Q_UNUSED(stopped); |
|
45 } |
|
46 |
|
47 virtual void complete(volatile bool *stopped) |
|
48 { |
|
49 Q_UNUSED(stopped); |
|
50 } |
|
51 |
|
52 private: |
|
53 bool mAutoDelete; |
|
54 }; |
|
55 |
|
56 class CP_CATEGORY_EXPORT CpTaskExecutor : public QThread |
|
57 { |
|
58 Q_OBJECT |
|
59 public: |
|
60 explicit CpTaskExecutor(QObject *parent = 0); |
|
61 |
|
62 virtual ~CpTaskExecutor(); |
|
63 |
|
64 /* |
|
65 return the gloabl instance |
|
66 */ |
|
67 static CpTaskExecutor *globalInstance(); |
|
68 |
|
69 /* |
|
70 destroy the global instance |
|
71 */ |
|
72 static void destroyGlobalInstance(); |
|
73 |
|
74 /* |
|
75 add the task to running queue |
|
76 @task the task |
|
77 @append ture -- append to tail false -- preappend to head |
|
78 */ |
|
79 |
|
80 bool runTask(CpTask *task,bool append = false); |
|
81 |
|
82 /* |
|
83 stop the thread and remove all not running tasks |
|
84 */ |
|
85 void stop(); |
|
86 |
|
87 /* |
|
88 move a task to front of the queue |
|
89 */ |
|
90 bool toFront(CpTask *task); |
|
91 |
|
92 /* |
|
93 move a task to front of the queue |
|
94 */ |
|
95 template <typename Predicate> bool toFront(Predicate pred); |
|
96 |
|
97 protected: |
|
98 /* |
|
99 From QThread, run tasks |
|
100 */ |
|
101 virtual void run(); |
|
102 private: |
|
103 /* |
|
104 remove not running tasks |
|
105 */ |
|
106 void removeTasks(); |
|
107 private: |
|
108 QList<CpTask*> mTasks; |
|
109 QMutex mMutex; |
|
110 volatile bool mStopped; |
|
111 }; |
|
112 |
|
113 |
|
114 template<typename Predicate> |
|
115 bool CpTaskExecutor::toFront(Predicate pred) |
|
116 { |
|
117 QMutexLocker locker(&mMutex); |
|
118 |
|
119 QList<CpTask*>::iterator workIterator = mTasks.begin(); |
|
120 QList<CpTask*>::iterator endIterator(mTasks.end()); |
|
121 |
|
122 for(;workIterator != endIterator;++workIterator) |
|
123 { |
|
124 if (pred(*workIterator)) { |
|
125 break; |
|
126 } |
|
127 } |
|
128 |
|
129 if (workIterator == endIterator) { |
|
130 return false; |
|
131 } |
|
132 |
|
133 CpTask *task = *workIterator; |
|
134 mTasks.erase(workIterator); |
|
135 mTasks.insert(0,task); |
|
136 |
|
137 return true; |
|
138 } |
|
139 |
|
140 #endif //CPTASKECECUTOR_H |
|
141 |
|
142 //End of File |
|