TaskManager.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // TaskManager.h
  3. //
  4. // $Id: //poco/svn/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. // Permission is hereby granted, free of charge, to any person or organization
  16. // obtaining a copy of the software and accompanying documentation covered by
  17. // this license (the "Software") to use, reproduce, display, distribute,
  18. // execute, and transmit the Software, and to prepare derivative works of the
  19. // Software, and to permit third-parties to whom the Software is furnished to
  20. // do so, all subject to the following:
  21. //
  22. // The copyright notices in the Software and this entire statement, including
  23. // the above license grant, this restriction and the following disclaimer,
  24. // must be included in all copies of the Software, in whole or in part, and
  25. // all derivative works of the Software, unless such copies or derivative
  26. // works are solely in the form of machine-executable object code generated by
  27. // a source language processor.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  32. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  33. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  34. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  35. // DEALINGS IN THE SOFTWARE.
  36. //
  37. #ifndef Foundation_TaskManager_INCLUDED
  38. #define Foundation_TaskManager_INCLUDED
  39. #include "Poco/Foundation.h"
  40. #include "Poco/Mutex.h"
  41. #include "Poco/Task.h"
  42. #include "Poco/AutoPtr.h"
  43. #include "Poco/NotificationCenter.h"
  44. #include "Poco/Timestamp.h"
  45. #include <list>
  46. namespace Poco {
  47. class Notification;
  48. class ThreadPool;
  49. class Exception;
  50. class Foundation_API TaskManager
  51. /// The TaskManager manages a collection of tasks
  52. /// and monitors their lifetime.
  53. ///
  54. /// A TaskManager has a built-in NotificationCenter that
  55. /// is used to send out notifications on task progress
  56. /// and task states. See the TaskNotification class and its
  57. /// subclasses for the various events that result in a notification.
  58. /// To keep the number of notifications small, a TaskProgressNotification
  59. /// will only be sent out once in 100 milliseconds.
  60. {
  61. public:
  62. typedef AutoPtr<Task> TaskPtr;
  63. typedef std::list<TaskPtr> TaskList;
  64. TaskManager();
  65. /// Creates the TaskManager, using the
  66. /// default ThreadPool.
  67. TaskManager(ThreadPool& pool);
  68. /// Creates the TaskManager, using the
  69. /// given ThreadPool.
  70. ~TaskManager();
  71. /// Destroys the TaskManager.
  72. void start(Task* pTask);
  73. /// Starts the given task in a thread obtained
  74. /// from the thread pool.
  75. ///
  76. /// The TaskManager takes ownership of the Task object
  77. /// and deletes it when it it finished.
  78. void cancelAll();
  79. /// Requests cancellation of all tasks.
  80. void joinAll();
  81. /// Waits for the completion of all the threads
  82. /// in the TaskManager's thread pool.
  83. TaskList taskList() const;
  84. /// Returns a copy of the internal task list.
  85. int count() const;
  86. /// Returns the number of tasks in the internal task list.
  87. void addObserver(const AbstractObserver& observer);
  88. /// Registers an observer with the NotificationCenter.
  89. /// Usage:
  90. /// Observer<MyClass, MyNotification> obs(*this, &MyClass::handleNotification);
  91. /// notificationCenter.addObserver(obs);
  92. void removeObserver(const AbstractObserver& observer);
  93. /// Unregisters an observer with the NotificationCenter.
  94. static const int MIN_PROGRESS_NOTIFICATION_INTERVAL;
  95. protected:
  96. void postNotification(Notification* pNf);
  97. /// Posts a notification to the task manager's
  98. /// notification center.
  99. void taskStarted(Task* pTask);
  100. void taskProgress(Task* pTask, float progress);
  101. void taskCancelled(Task* pTask);
  102. void taskFinished(Task* pTask);
  103. void taskFailed(Task* pTask, const Exception& exc);
  104. private:
  105. ThreadPool& _threadPool;
  106. TaskList _taskList;
  107. Timestamp _lastProgressNotification;
  108. NotificationCenter _nc;
  109. mutable FastMutex _mutex;
  110. friend class Task;
  111. };
  112. //
  113. // inlines
  114. //
  115. inline int TaskManager::count() const
  116. {
  117. FastMutex::ScopedLock lock(_mutex);
  118. return (int) _taskList.size();
  119. }
  120. } // namespace Poco
  121. #endif // Foundation_TaskManager_INCLUDED