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 startSync(Task* pTask); |
72 | /// Starts the given task in the current thread. |
73 | /// The TaskManager takes ownership of the Task object |
74 | /// and deletes it when it it finished. |
75 | |
76 | void cancelAll(); |
77 | /// Requests cancellation of all tasks. |
78 | |
79 | void joinAll(); |
80 | /// Waits for the completion of all the threads |
81 | /// in the TaskManager's thread pool. |
82 | /// |
83 | /// Note: joinAll() will wait for ALL tasks in the |
84 | /// TaskManager's ThreadPool to complete. If the |
85 | /// ThreadPool has threads created by other |
86 | /// facilities, these threads must also complete |
87 | /// before joinAll() can return. |
88 | |
89 | TaskList taskList() const; |
90 | /// Returns a copy of the internal task list. |
91 | |
92 | std::size_t count() const; |
93 | /// Returns the number of tasks in the internal task list. |
94 | |
95 | void addObserver(const AbstractObserver& observer); |
96 | /// Registers an observer with the NotificationCenter. |
97 | /// Usage: |
98 | /// Observer<MyClass, MyNotification> obs(*this, &MyClass::handleNotification); |
99 | /// notificationCenter.addObserver(obs); |
100 | |
101 | void removeObserver(const AbstractObserver& observer); |
102 | /// Unregisters an observer with the NotificationCenter. |
103 | |
104 | static const int MIN_PROGRESS_NOTIFICATION_INTERVAL; |
105 | |
106 | protected: |
107 | void postNotification(const Notification::Ptr& pNf); |
108 | /// Posts a notification to the task manager's |
109 | /// notification center. |
110 | |
111 | void taskStarted(Task* pTask); |
112 | void taskProgress(Task* pTask, float progress); |
113 | void taskCancelled(Task* pTask); |
114 | void taskFinished(Task* pTask); |
115 | void taskFailed(Task* pTask, const Exception& exc); |
116 | |
117 | private: |
118 | ThreadPool& _threadPool; |
119 | TaskList _taskList; |
120 | Timestamp _lastProgressNotification; |
121 | NotificationCenter _nc; |
122 | mutable FastMutex _mutex; |
123 | |
124 | friend class Task; |
125 | }; |
126 | |
127 | |
128 | // |
129 | // inlines |
130 | // |
131 | inline std::size_t TaskManager::count() const |
132 | { |
133 | FastMutex::ScopedLock lock(_mutex); |
134 | |
135 | return _taskList.size(); |
136 | } |
137 | |
138 | |
139 | } // namespace Poco |
140 | |
141 | |
142 | #endif // Foundation_TaskManager_INCLUDED |
143 | |