ctkEAAsyncDeliverTasks.tpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. template<class SyncDeliverTasks, class HandlerTask>
  16. class ctkEAAsyncDeliverTasks<SyncDeliverTasks, HandlerTask>::TaskExecuter
  17. : public ctkEARunnable
  18. {
  19. private:
  20. typedef ctkEAAsyncDeliverTasks<SyncDeliverTasks, HandlerTask> TopClass;
  21. TopClass* tc;
  22. QList<HandlerTask> tasks;
  23. QMutex tasksMutex;
  24. QThread* key;
  25. public:
  26. TaskExecuter(TopClass* tc, const QList<HandlerTask>& tasks, QThread* key)
  27. : tc(tc), tasks(tasks), key(key)
  28. {
  29. }
  30. void run()
  31. {
  32. bool running = true;
  33. do
  34. {
  35. QList<HandlerTask> currTasks;
  36. {
  37. QMutexLocker l(&tasksMutex);
  38. currTasks.push_back(tasks.takeFirst());
  39. }
  40. tc->deliver_task->execute(currTasks);
  41. {
  42. QMutexLocker l(&tc->running_threads_mutex);
  43. running = tasks.size() > 0;
  44. if (!running)
  45. {
  46. ctkEARunnable* runnable = tc->running_threads.take(key);
  47. if (runnable->autoDelete() && !--runnable->ref) delete runnable;
  48. }
  49. }
  50. } while (running);
  51. }
  52. void add(const QList<HandlerTask>& newTasks)
  53. {
  54. QMutexLocker l(&tasksMutex);
  55. tasks.append(newTasks);
  56. }
  57. };
  58. template<class SyncDeliverTasks, class HandlerTask>
  59. ctkEAAsyncDeliverTasks<SyncDeliverTasks, HandlerTask>::ctkEAAsyncDeliverTasks(ctkEADefaultThreadPool* pool, DeliverTask* deliverTask)
  60. : pool(pool), deliver_task(deliverTask)
  61. {
  62. }
  63. template<class SyncDeliverTasks, class HandlerTask>
  64. void ctkEAAsyncDeliverTasks<SyncDeliverTasks, HandlerTask>::execute(const QList<HandlerTask>& tasks)
  65. {
  66. QThread* currentThread = QThread::currentThread();
  67. TaskExecuter* executer = 0;
  68. {
  69. QMutexLocker l(&running_threads_mutex);
  70. TaskExecuter* runningExecutor = dynamic_cast<TaskExecuter*>(running_threads.value(currentThread));
  71. if (runningExecutor)
  72. {
  73. runningExecutor->add(tasks);
  74. }
  75. else
  76. {
  77. executer = new TaskExecuter(this, tasks, currentThread);
  78. ++executer->ref;
  79. running_threads.insert(currentThread, executer);
  80. }
  81. }
  82. if (executer)
  83. {
  84. pool->executeTask(executer);
  85. }
  86. }