1 | // |
---|
2 | // TaskManager.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/TaskManager.cpp#2 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Tasks |
---|
8 | // Module: Tasks |
---|
9 | // |
---|
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include "Poco/TaskManager.h" |
---|
38 | #include "Poco/TaskNotification.h" |
---|
39 | #include "Poco/ThreadPool.h" |
---|
40 | |
---|
41 | |
---|
42 | namespace Poco { |
---|
43 | |
---|
44 | |
---|
45 | const int TaskManager::MIN_PROGRESS_NOTIFICATION_INTERVAL = 100000; // 100 milliseconds |
---|
46 | |
---|
47 | |
---|
48 | TaskManager::TaskManager(): |
---|
49 | _threadPool(ThreadPool::defaultPool()) |
---|
50 | { |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | TaskManager::TaskManager(ThreadPool& pool): |
---|
55 | _threadPool(pool) |
---|
56 | { |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | TaskManager::~TaskManager() |
---|
61 | { |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | void TaskManager::start(Task* pTask) |
---|
66 | { |
---|
67 | TaskPtr pAutoTask(pTask); // take ownership immediately |
---|
68 | FastMutex::ScopedLock lock(_mutex); |
---|
69 | |
---|
70 | pAutoTask->setOwner(this); |
---|
71 | pAutoTask->setState(Task::TASK_STARTING); |
---|
72 | _taskList.push_back(pAutoTask); |
---|
73 | try |
---|
74 | { |
---|
75 | _threadPool.start(*pAutoTask, pAutoTask->name()); |
---|
76 | } |
---|
77 | catch (...) |
---|
78 | { |
---|
79 | // Make sure that we don't act like we own the task since |
---|
80 | // we never started it. If we leave the task on our task |
---|
81 | // list, the size of the list is incorrect. |
---|
82 | _taskList.pop_back(); |
---|
83 | throw; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | void TaskManager::cancelAll() |
---|
89 | { |
---|
90 | FastMutex::ScopedLock lock(_mutex); |
---|
91 | |
---|
92 | for (TaskList::iterator it = _taskList.begin(); it != _taskList.end(); ++it) |
---|
93 | { |
---|
94 | (*it)->cancel(); |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | void TaskManager::joinAll() |
---|
100 | { |
---|
101 | _threadPool.joinAll(); |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | TaskManager::TaskList TaskManager::taskList() const |
---|
106 | { |
---|
107 | FastMutex::ScopedLock lock(_mutex); |
---|
108 | |
---|
109 | return _taskList; |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | void TaskManager::addObserver(const AbstractObserver& observer) |
---|
114 | { |
---|
115 | _nc.addObserver(observer); |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | void TaskManager::removeObserver(const AbstractObserver& observer) |
---|
120 | { |
---|
121 | _nc.removeObserver(observer); |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | void TaskManager::postNotification(Notification* pNf) |
---|
126 | { |
---|
127 | _nc.postNotification(pNf); |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | void TaskManager::taskStarted(Task* pTask) |
---|
132 | { |
---|
133 | _nc.postNotification(new TaskStartedNotification(pTask)); |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | void TaskManager::taskProgress(Task* pTask, float progress) |
---|
138 | { |
---|
139 | FastMutex::ScopedLock lock(_mutex); |
---|
140 | |
---|
141 | if (_lastProgressNotification.isElapsed(MIN_PROGRESS_NOTIFICATION_INTERVAL)) |
---|
142 | { |
---|
143 | _lastProgressNotification.update(); |
---|
144 | _nc.postNotification(new TaskProgressNotification(pTask, progress)); |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | void TaskManager::taskCancelled(Task* pTask) |
---|
150 | { |
---|
151 | _nc.postNotification(new TaskCancelledNotification(pTask)); |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | void TaskManager::taskFinished(Task* pTask) |
---|
156 | { |
---|
157 | _nc.postNotification(new TaskFinishedNotification(pTask)); |
---|
158 | |
---|
159 | FastMutex::ScopedLock lock(_mutex); |
---|
160 | for (TaskList::iterator it = _taskList.begin(); it != _taskList.end(); ++it) |
---|
161 | { |
---|
162 | if (*it == pTask) |
---|
163 | { |
---|
164 | _taskList.erase(it); |
---|
165 | break; |
---|
166 | } |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | void TaskManager::taskFailed(Task* pTask, const Exception& exc) |
---|
172 | { |
---|
173 | _nc.postNotification(new TaskFailedNotification(pTask, exc)); |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | } // namespace Poco |
---|