ctkEACyclicBarrier.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 "ctkEACyclicBarrier_p.h"
  16. // for ctk::msecsTo() - remove after switching to Qt 4.7
  17. #include <ctkUtils.h>
  18. #include <QDateTime>
  19. #include <QRunnable>
  20. #include <QDebug>
  21. #include <dispatch/ctkEAInterruptibleThread_p.h>
  22. #include <dispatch/ctkEAInterruptedException_p.h>
  23. #include "ctkEATimeoutException_p.h"
  24. #include "ctkEABrokenBarrierException_p.h"
  25. ctkEACyclicBarrier::ctkEACyclicBarrier(int parties, ctkEARunnable* command)
  26. : parties_(parties), broken_(false), barrierCommand_(command),
  27. count_(parties), resets_(0)
  28. {
  29. if (parties <= 0) throw ctkInvalidArgumentException("parties cannot be negative");
  30. if (barrierCommand_) ++barrierCommand_->ref;
  31. }
  32. ctkEARunnable* ctkEACyclicBarrier::setBarrierCommand(ctkEARunnable* command)
  33. {
  34. QMutexLocker lock(&mutex);
  35. ctkEARunnable* old = barrierCommand_;
  36. --old->ref;
  37. barrierCommand_ = command;
  38. ++barrierCommand_->ref;
  39. return old;
  40. }
  41. bool ctkEACyclicBarrier::broken() const
  42. {
  43. QMutexLocker lock(&mutex);
  44. return broken_;
  45. }
  46. void ctkEACyclicBarrier::restart()
  47. {
  48. QMutexLocker lock(&mutex);
  49. broken_ = false;
  50. ++resets_;
  51. count_ = parties_;
  52. waitCond.wakeAll();
  53. }
  54. int ctkEACyclicBarrier::parties() const
  55. {
  56. return parties_;
  57. }
  58. int ctkEACyclicBarrier::barrier()
  59. {
  60. return doBarrier(false, 0);
  61. }
  62. int ctkEACyclicBarrier::attemptBarrier(long msecs)
  63. {
  64. return doBarrier(true, msecs);
  65. }
  66. int ctkEACyclicBarrier::doBarrier(bool timed, long msecs)
  67. {
  68. QMutexLocker lock(&mutex);
  69. int index = --count_;
  70. ctkEAInterruptibleThread* currThread = ctkEAInterruptibleThread::currentThread();
  71. Q_ASSERT(currThread != 0); // ctkEACyclicBarrier can only be used with ctkEAInterruptibleThread
  72. if (broken_)
  73. {
  74. throw ctkEABrokenBarrierException(index);
  75. }
  76. else if (ctkEAInterruptibleThread::interrupted())
  77. {
  78. broken_ = true;
  79. waitCond.wakeAll();
  80. throw ctkEAInterruptedException();
  81. }
  82. else if (index == 0)
  83. { // tripped
  84. count_ = parties_;
  85. ++resets_;
  86. waitCond.wakeAll();
  87. try
  88. {
  89. if (barrierCommand_)
  90. {
  91. const bool autoDelete = barrierCommand_->autoDelete();
  92. barrierCommand_->run();
  93. if (autoDelete && !--barrierCommand_->ref) delete barrierCommand_;
  94. }
  95. return 0;
  96. }
  97. catch (...)
  98. {
  99. broken_ = true;
  100. return 0;
  101. }
  102. }
  103. else if (timed && msecs <= 0)
  104. {
  105. broken_ = true;
  106. waitCond.wakeAll();
  107. throw ctkEATimeoutException(msecs);
  108. }
  109. else
  110. { // wait until next reset
  111. int r = resets_;
  112. QDateTime startTime = QDateTime::currentDateTime();
  113. qint64 waitTime = static_cast<qint64>(msecs);
  114. forever
  115. {
  116. try
  117. {
  118. currThread->wait(&mutex, &waitCond, waitTime);
  119. }
  120. catch (const ctkEAInterruptedException& ex)
  121. {
  122. mutex.lock();
  123. // Only claim that broken if interrupted before reset
  124. if (resets_ == r)
  125. {
  126. broken_ = true;
  127. waitCond.wakeAll();
  128. throw ex;
  129. }
  130. else
  131. {
  132. currThread->interrupt(); // propagate
  133. }
  134. }
  135. if (broken_)
  136. {
  137. throw ctkEABrokenBarrierException(index);
  138. }
  139. else if (r != resets_)
  140. {
  141. return index;
  142. }
  143. else if (timed)
  144. {
  145. //TODO use Qt 4.7 API
  146. //waitTime = msecs - QDateTime::toMSecs(startTime);
  147. waitTime = static_cast<qint64>(msecs) - ctk::msecsTo(startTime, QDateTime::currentDateTime());
  148. if (waitTime <= 0)
  149. {
  150. broken_ = true;
  151. waitCond.wakeAll();
  152. throw ctkEATimeoutException(msecs);
  153. }
  154. }
  155. }
  156. }
  157. return 0; // will never be reached
  158. }