TaskManager.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. using TaskPtr = AutoPtr<Task>;
  41. using TaskList = std::list<TaskPtr>;
  42. TaskManager(const std::string& name = "",
  43. int minCapacity = 2,
  44. int maxCapacity = 16,
  45. int idleTime = 60,
  46. int stackSize = POCO_THREAD_STACK_SIZE);
  47. /// Creates the TaskManager.
  48. TaskManager(ThreadPool& pool);
  49. /// Creates the TaskManager, using the
  50. /// given ThreadPool (should be used
  51. /// by this TaskManager exclusively).
  52. ~TaskManager();
  53. /// Destroys the TaskManager.
  54. bool start(Task* pTask);
  55. /// Starts the given task in a thread obtained
  56. /// from the thread pool; returns true if successful.
  57. ///
  58. /// If this method returns false, the task was cancelled
  59. /// before it could be started, or it was already running;
  60. /// in any case, a false return means refusal of ownership
  61. /// and indicates that the task pointer may not be valid
  62. /// anymore (it will only be valid if it was duplicated
  63. /// prior to this call).
  64. ///
  65. /// The TaskManager takes ownership of the Task object
  66. /// and deletes it when it is finished.
  67. void cancelAll();
  68. /// Requests cancellation of all tasks.
  69. void joinAll();
  70. /// Waits for the completion of all the threads
  71. /// in the TaskManager's thread pool.
  72. ///
  73. /// Note: joinAll() will wait for ALL tasks in the
  74. /// TaskManager's ThreadPool to complete. If the
  75. /// ThreadPool has threads created by other
  76. /// facilities, these threads must also complete
  77. /// before joinAll() can return.
  78. TaskList taskList() const;
  79. /// Returns a copy of the internal task list.
  80. int count() const;
  81. /// Returns the number of tasks in the internal task list.
  82. void addObserver(const AbstractObserver& observer);
  83. /// Registers an observer with the NotificationCenter.
  84. /// Usage:
  85. /// Observer<MyClass, MyNotification> obs(*this, &MyClass::handleNotification);
  86. /// notificationCenter.addObserver(obs);
  87. void removeObserver(const AbstractObserver& observer);
  88. /// Unregisters an observer with the NotificationCenter.
  89. static const int MIN_PROGRESS_NOTIFICATION_INTERVAL;
  90. protected:
  91. void postNotification(const Notification::Ptr& pNf);
  92. /// Posts a notification to the task manager's
  93. /// notification center.
  94. void taskStarted(Task* pTask);
  95. void taskProgress(Task* pTask, float progress);
  96. void taskCancelled(Task* pTask);
  97. void taskFinished(Task* pTask);
  98. void taskFailed(Task* pTask, const Exception& exc);
  99. private:
  100. using MutexT = FastMutex;
  101. using ScopedLockT = MutexT::ScopedLock;
  102. ThreadPool& _threadPool;
  103. bool _ownPool;
  104. TaskList _taskList;
  105. Timestamp _lastProgressNotification;
  106. NotificationCenter _nc;
  107. mutable MutexT _mutex;
  108. friend class Task;
  109. };
  110. //
  111. // inlines
  112. //
  113. inline int TaskManager::count() const
  114. {
  115. ScopedLockT lock(_mutex);
  116. return (int) _taskList.size();
  117. }
  118. } // namespace Poco
  119. #endif // Foundation_TaskManager_INCLUDED