ctkEADefaultThreadPool.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #include "ctkEADefaultThreadPool_p.h"
  16. #include "ctkEALinkedQueue_p.h"
  17. #include "ctkEAInterruptedException_p.h"
  18. #include <ctkEventAdminActivator_p.h>
  19. #include <tasks/ctkEASyncThread_p.h>
  20. struct _SyncThreadFactory : public ctkEAThreadFactory
  21. {
  22. ctkEAInterruptibleThread* newThread(ctkEARunnable* command)
  23. {
  24. ctkEAInterruptibleThread* thread = new ctkEASyncThread(command);
  25. //thread->setPriority(QThread::NormalPriority);
  26. //thread->setDaemon(true);
  27. return thread;
  28. }
  29. };
  30. struct _AsyncThreadFactory : public ctkEAThreadFactory
  31. {
  32. ctkEAInterruptibleThread* newThread(ctkEARunnable* command)
  33. {
  34. ctkEAInterruptibleThread* thread = new ctkEAInterruptibleThread(command);
  35. //thread->setPriority(QThread::NormalPriority);
  36. //thread->setDaemon(false);
  37. return thread;
  38. }
  39. };
  40. ctkEADefaultThreadPool::ctkEADefaultThreadPool(int poolSize, bool syncThreads)
  41. : ctkEAPooledExecutor(new ctkEALinkedQueue())
  42. {
  43. if (syncThreads)
  44. {
  45. delete this->setThreadFactory(new _SyncThreadFactory());
  46. }
  47. else
  48. {
  49. delete this->setThreadFactory(new _AsyncThreadFactory());
  50. }
  51. configure(poolSize);
  52. setKeepAliveTime(60000);
  53. runWhenBlocked();
  54. }
  55. void ctkEADefaultThreadPool::configure(int poolSize)
  56. {
  57. setMinimumPoolSize(poolSize);
  58. setMaximumPoolSize(poolSize + 10);
  59. }
  60. void ctkEADefaultThreadPool::close()
  61. {
  62. shutdownNow();
  63. try
  64. {
  65. awaitTerminationAfterShutdown();
  66. }
  67. catch (const ctkEAInterruptedException& )
  68. {
  69. // ignore this
  70. }
  71. }
  72. void ctkEADefaultThreadPool::executeTask(ctkEARunnable* task)
  73. {
  74. try
  75. {
  76. this->execute(task);
  77. }
  78. catch (const std::exception& e)
  79. {
  80. CTK_WARN_EXC(ctkEventAdminActivator::getLogService(), &e)
  81. << "Exception: " << e.what();
  82. // ignore this
  83. }
  84. }