ctkEAInterruptibleThread.cpp 2.7 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 "ctkEAInterruptibleThread_p.h"
  16. #include "ctkEAInterruptedException_p.h"
  17. #include <QRunnable>
  18. #include <QDebug>
  19. ctkEAInterruptibleThread::ctkEAInterruptibleThread(ctkEARunnable* command, QObject* parent)
  20. : QThread(parent), command(command)
  21. {
  22. this->setObjectName(QString("ctkEAInterruptibleThread") + QString::number(reinterpret_cast<qint64>(command)));
  23. }
  24. void ctkEAInterruptibleThread::run()
  25. {
  26. if (command)
  27. {
  28. const bool autoDelete = command->autoDelete();
  29. command->run();
  30. if (autoDelete && !--command->ref) delete command;
  31. }
  32. else
  33. {
  34. QThread::run();
  35. }
  36. }
  37. ctkEAInterruptibleThread* ctkEAInterruptibleThread::currentThread()
  38. {
  39. return qobject_cast<ctkEAInterruptibleThread*>(QThread::currentThread());
  40. }
  41. void ctkEAInterruptibleThread::wait(QMutex* mutex, QWaitCondition* waitCond, unsigned long time)
  42. {
  43. currWaitCond_.testAndSetOrdered(0, waitCond);
  44. isWaiting_.testAndSetOrdered(0,1);
  45. waitCond->wait(mutex, time == 0 ? ULONG_MAX : time);
  46. isWaiting_.testAndSetOrdered(1,0);
  47. currWaitCond_.fetchAndStoreOrdered(0);
  48. if (interrupted_.fetchAndAddOrdered(0))
  49. {
  50. interrupted_.testAndSetOrdered(1, 0);
  51. throw ctkEAInterruptedException();
  52. }
  53. }
  54. void ctkEAInterruptibleThread::join()
  55. {
  56. this->QThread::wait();
  57. }
  58. void ctkEAInterruptibleThread::interrupt()
  59. {
  60. if (isWaiting_.fetchAndAddOrdered(0) && this->isRunning())
  61. {
  62. interrupted_.testAndSetOrdered(0, 1);
  63. if (QWaitCondition* waitCond = currWaitCond_.fetchAndStoreOrdered(0))
  64. waitCond->wakeAll();
  65. }
  66. else
  67. {
  68. if (this->isRunning())
  69. {
  70. interrupted_.testAndSetOrdered(0, 1);
  71. }
  72. }
  73. }
  74. bool ctkEAInterruptibleThread::interrupted()
  75. {
  76. if (ctkEAInterruptibleThread* t = qobject_cast<ctkEAInterruptibleThread*>(QThread::currentThread()))
  77. {
  78. return t->interrupted_.fetchAndStoreOrdered(0);
  79. }
  80. return false;
  81. }
  82. bool ctkEAInterruptibleThread::isInterrupted() const
  83. {
  84. return interrupted_.fetchAndAddOrdered(0);
  85. }