1 | // |
2 | // TaskManager.h |
3 | // |
4 | // Library: Foundation |
5 | // Package: Tasks |
6 | // Module: Tasks |
7 | // |
8 | // Definition of the TaskManager class. |
9 | // |
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
11 | // and Contributors. |
12 | // |
13 | // SPDX-License-Identifier: BSL-1.0 |
14 | // |
15 | |
16 | |
17 | #ifndef Foundation_TaskManager_INCLUDED |
18 | #define Foundation_TaskManager_INCLUDED |
19 | |
20 | |
21 | #include "Poco/Foundation.h" |
22 | #include "Poco/Mutex.h" |
23 | #include "Poco/Task.h" |
24 | #include "Poco/AutoPtr.h" |
25 | #include "Poco/NotificationCenter.h" |
26 | #include "Poco/Timestamp.h" |
27 | #include "Poco/ThreadPool.h" |
28 | #include <list> |
29 | |
30 | |
31 | namespace Poco { |
32 | |
33 | |
34 | class Notification; |
35 | class Exception; |
36 | |
37 | |
38 | class Foundation_API TaskManager |
39 | /// The TaskManager manages a collection of tasks |
40 | /// and monitors their lifetime. |
41 | /// |
42 | /// A TaskManager has a built-in NotificationCenter that |
43 | /// is used to send out notifications on task progress |
44 | /// and task states. See the TaskNotification class and its |
45 | /// subclasses for the various events that result in a notification. |
46 | /// To keep the number of notifications small, a TaskProgressNotification |
47 | /// will only be sent out once in 100 milliseconds. |
48 | { |
49 | public: |
50 | typedef AutoPtr<Task> TaskPtr; |
51 | typedef std::list<TaskPtr> TaskList; |
52 | |
53 | TaskManager(ThreadPool::ThreadAffinityPolicy affinityPolicy = ThreadPool::TAP_DEFAULT); |
54 | /// Creates the TaskManager, using the |
55 | /// default ThreadPool. |
56 | |
57 | TaskManager(ThreadPool& pool); |
58 | /// Creates the TaskManager, using the |
59 | /// given ThreadPool. |
60 | |
61 | ~TaskManager(); |
62 | /// Destroys the TaskManager. |
63 | |
64 | void start(Task* pTask, int cpu = -1); |
65 | /// Starts the given task in a thread obtained |
66 | /// from the thread pool, |
67 | /// on specified cpu. |
68 | /// The TaskManager takes ownership of the Task object |
69 | /// and deletes it when it it finished. |
70 | |
71 | void cancelAll(); |
72 | /// Requests cancellation of all tasks. |
73 | |
74 | void joinAll(); |
75 | /// Waits for the completion of all the threads |
76 | /// in the TaskManager's thread pool. |
77 | /// |
78 | /// Note: joinAll() will wait for ALL tasks in the |
79 | /// TaskManager's ThreadPool to complete. If the |
80 | /// ThreadPool has threads created by other |
81 | /// facilities, these threads must also complete |
82 | /// before joinAll() can return. |
83 | |
84 | TaskList taskList() const; |
85 | /// Returns a copy of the internal task list. |
86 | |
87 | std::size_t count() const; |
88 | /// Returns the number of tasks in the internal task list. |
89 | |
90 | void addObserver(const AbstractObserver& observer); |
91 | /// Registers an observer with the NotificationCenter. |
92 | /// Usage: |
93 | /// Observer<MyClass, MyNotification> obs(*this, &MyClass::handleNotification); |
94 | /// notificationCenter.addObserver(obs); |
95 | |
96 | void removeObserver(const AbstractObserver& observer); |
97 | /// Unregisters an observer with the NotificationCenter. |
98 | |
99 | static const int MIN_PROGRESS_NOTIFICATION_INTERVAL; |
100 | |
101 | protected: |
102 | void postNotification(const Notification::Ptr& pNf); |
103 | /// Posts a notification to the task manager's |
104 | /// notification center. |
105 | |
106 | void taskStarted(Task* pTask); |
107 | void taskProgress(Task* pTask, float progress); |
108 | void taskCancelled(Task* pTask); |
109 | void taskFinished(Task* pTask); |
110 | void taskFailed(Task* pTask, const Exception& exc); |
111 | |
112 | private: |
113 | ThreadPool& _threadPool; |
114 | TaskList _taskList; |
115 | Timestamp _lastProgressNotification; |
116 | NotificationCenter _nc; |
117 | mutable FastMutex _mutex; |
118 | |
119 | friend class Task; |
120 | }; |
121 | |
122 | |
123 | // |
124 | // inlines |
125 | // |
126 | inline std::size_t TaskManager::count() const |
127 | { |
128 | FastMutex::ScopedLock lock(_mutex); |
129 | |
130 | return _taskList.size(); |
131 | } |
132 | |
133 | |
134 | } // namespace Poco |
135 | |
136 | |
137 | #endif // Foundation_TaskManager_INCLUDED |
138 | |