TaskManager.h 3.4 KB

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