TaskManager.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #ifndef Foundation_TaskManager_INCLUDED
  16. #define Foundation_TaskManager_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/Mutex.h"
  19. #include "Poco/Task.h"
  20. #include "Poco/AutoPtr.h"
  21. #include "Poco/NotificationCenter.h"
  22. #include "Poco/Timestamp.h"
  23. #include <list>
  24. namespace Poco {
  25. class Notification;
  26. class ThreadPool;
  27. class Exception;
  28. class Foundation_API TaskManager
  29. /// The TaskManager manages a collection of tasks
  30. /// and monitors their lifetime.
  31. ///
  32. /// A TaskManager has a built-in NotificationCenter that
  33. /// is used to send out notifications on task progress
  34. /// and task states. See the TaskNotification class and its
  35. /// subclasses for the various events that result in a notification.
  36. /// To keep the number of notifications small, a TaskProgressNotification
  37. /// will only be sent out once in 100 milliseconds.
  38. {
  39. public:
  40. typedef AutoPtr<Task> TaskPtr;
  41. typedef std::list<TaskPtr> TaskList;
  42. TaskManager();
  43. /// Creates the TaskManager, using the
  44. /// default ThreadPool.
  45. TaskManager(ThreadPool& pool);
  46. /// Creates the TaskManager, using the
  47. /// given ThreadPool.
  48. ~TaskManager();
  49. /// Destroys the TaskManager.
  50. void start(Task* pTask);
  51. /// Starts the given task in a thread obtained
  52. /// from the thread pool.
  53. ///
  54. /// The TaskManager takes ownership of the Task object
  55. /// and deletes it when it it finished.
  56. void cancelAll();
  57. /// Requests cancellation of all tasks.
  58. void joinAll();
  59. /// Waits for the completion of all the threads
  60. /// in the TaskManager's thread pool.
  61. ///
  62. /// Note: joinAll() will wait for ALL tasks in the
  63. /// TaskManager's ThreadPool to complete. If the
  64. /// ThreadPool has threads created by other
  65. /// facilities, these threads must also complete
  66. /// before joinAll() can return.
  67. TaskList taskList() const;
  68. /// Returns a copy of the internal task list.
  69. int count() const;
  70. /// Returns the number of tasks in the internal task list.
  71. void addObserver(const AbstractObserver& observer);
  72. /// Registers an observer with the NotificationCenter.
  73. /// Usage:
  74. /// Observer<MyClass, MyNotification> obs(*this, &MyClass::handleNotification);
  75. /// notificationCenter.addObserver(obs);
  76. void removeObserver(const AbstractObserver& observer);
  77. /// Unregisters an observer with the NotificationCenter.
  78. static const int MIN_PROGRESS_NOTIFICATION_INTERVAL;
  79. protected:
  80. void postNotification(const Notification::Ptr& pNf);
  81. /// Posts a notification to the task manager's
  82. /// notification center.
  83. void taskStarted(Task* pTask);
  84. void taskProgress(Task* pTask, float progress);
  85. void taskCancelled(Task* pTask);
  86. void taskFinished(Task* pTask);
  87. void taskFailed(Task* pTask, const Exception& exc);
  88. private:
  89. ThreadPool& _threadPool;
  90. TaskList _taskList;
  91. Timestamp _lastProgressNotification;
  92. NotificationCenter _nc;
  93. mutable FastMutex _mutex;
  94. friend class Task;
  95. };
  96. //
  97. // inlines
  98. //
  99. inline int TaskManager::count() const
  100. {
  101. FastMutex::ScopedLock lock(_mutex);
  102. return (int) _taskList.size();
  103. }
  104. } // namespace Poco
  105. #endif // Foundation_TaskManager_INCLUDED