TaskManager.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // TaskManager.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/TaskManager.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Tasks
  8. // Module: Tasks
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/TaskManager.h"
  16. #include "Poco/TaskNotification.h"
  17. #include "Poco/ThreadPool.h"
  18. namespace Poco {
  19. const int TaskManager::MIN_PROGRESS_NOTIFICATION_INTERVAL = 100000; // 100 milliseconds
  20. TaskManager::TaskManager():
  21. _threadPool(ThreadPool::defaultPool())
  22. {
  23. }
  24. TaskManager::TaskManager(ThreadPool& pool):
  25. _threadPool(pool)
  26. {
  27. }
  28. TaskManager::~TaskManager()
  29. {
  30. }
  31. void TaskManager::start(Task* pTask)
  32. {
  33. TaskPtr pAutoTask(pTask); // take ownership immediately
  34. FastMutex::ScopedLock lock(_mutex);
  35. pAutoTask->setOwner(this);
  36. pAutoTask->setState(Task::TASK_STARTING);
  37. _taskList.push_back(pAutoTask);
  38. try
  39. {
  40. _threadPool.start(*pAutoTask, pAutoTask->name());
  41. }
  42. catch (...)
  43. {
  44. // Make sure that we don't act like we own the task since
  45. // we never started it. If we leave the task on our task
  46. // list, the size of the list is incorrect.
  47. _taskList.pop_back();
  48. throw;
  49. }
  50. }
  51. void TaskManager::cancelAll()
  52. {
  53. FastMutex::ScopedLock lock(_mutex);
  54. for (TaskList::iterator it = _taskList.begin(); it != _taskList.end(); ++it)
  55. {
  56. (*it)->cancel();
  57. }
  58. }
  59. void TaskManager::joinAll()
  60. {
  61. _threadPool.joinAll();
  62. }
  63. TaskManager::TaskList TaskManager::taskList() const
  64. {
  65. FastMutex::ScopedLock lock(_mutex);
  66. return _taskList;
  67. }
  68. void TaskManager::addObserver(const AbstractObserver& observer)
  69. {
  70. _nc.addObserver(observer);
  71. }
  72. void TaskManager::removeObserver(const AbstractObserver& observer)
  73. {
  74. _nc.removeObserver(observer);
  75. }
  76. void TaskManager::postNotification(const Notification::Ptr& pNf)
  77. {
  78. _nc.postNotification(pNf);
  79. }
  80. void TaskManager::taskStarted(Task* pTask)
  81. {
  82. _nc.postNotification(new TaskStartedNotification(pTask));
  83. }
  84. void TaskManager::taskProgress(Task* pTask, float progress)
  85. {
  86. FastMutex::ScopedLock lock(_mutex);
  87. if (_lastProgressNotification.isElapsed(MIN_PROGRESS_NOTIFICATION_INTERVAL))
  88. {
  89. _lastProgressNotification.update();
  90. _nc.postNotification(new TaskProgressNotification(pTask, progress));
  91. }
  92. }
  93. void TaskManager::taskCancelled(Task* pTask)
  94. {
  95. _nc.postNotification(new TaskCancelledNotification(pTask));
  96. }
  97. void TaskManager::taskFinished(Task* pTask)
  98. {
  99. _nc.postNotification(new TaskFinishedNotification(pTask));
  100. FastMutex::ScopedLock lock(_mutex);
  101. for (TaskList::iterator it = _taskList.begin(); it != _taskList.end(); ++it)
  102. {
  103. if (*it == pTask)
  104. {
  105. _taskList.erase(it);
  106. break;
  107. }
  108. }
  109. }
  110. void TaskManager::taskFailed(Task* pTask, const Exception& exc)
  111. {
  112. _nc.postNotification(new TaskFailedNotification(pTask, exc));
  113. }
  114. } // namespace Poco