ctkEALinkedQueue.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 "ctkEALinkedQueue_p.h"
  16. #include "ctkEAInterruptibleThread_p.h"
  17. #include "ctkEAInterruptedException_p.h"
  18. #include <ctkHighPrecisionTimer.h>
  19. ctkEALinkedQueue::ctkEALinkedQueue()
  20. : head_(new ctkEALinkedNode()), last_(head_), waitingForTake_(0)
  21. {
  22. }
  23. ctkEALinkedQueue::~ctkEALinkedQueue()
  24. {
  25. delete head_;
  26. }
  27. void ctkEALinkedQueue::put(ctkEARunnable* x)
  28. {
  29. if (x == 0) throw ctkInvalidArgumentException("QRunnable cannot be null");
  30. if (ctkEAInterruptibleThread::interrupted()) throw ctkEAInterruptedException();
  31. insert(x);
  32. }
  33. bool ctkEALinkedQueue::offer(ctkEARunnable* x, long msecs)
  34. {
  35. Q_UNUSED(msecs)
  36. if (x == 0) throw ctkInvalidArgumentException("QRunnable cannot be null");
  37. if (ctkEAInterruptibleThread::interrupted()) throw ctkEAInterruptedException();
  38. insert(x);
  39. return true;
  40. }
  41. ctkEARunnable* ctkEALinkedQueue::take()
  42. {
  43. if (ctkEAInterruptibleThread::interrupted()) throw ctkEAInterruptedException();
  44. // try to extract. If fail, then enter wait-based retry loop
  45. ctkEARunnable* x = extract();
  46. if (x != 0)
  47. return x;
  48. else
  49. {
  50. {
  51. QMutexLocker l(&putLock_);
  52. try
  53. {
  54. ++waitingForTake_;
  55. forever
  56. {
  57. x = extract();
  58. if (x != 0)
  59. {
  60. --waitingForTake_;
  61. return x;
  62. }
  63. else
  64. {
  65. ctkEAInterruptibleThread::currentThread()->wait(&putLock_, &putLockWait_);
  66. }
  67. }
  68. }
  69. catch(const ctkEAInterruptedException& ex)
  70. {
  71. --waitingForTake_;
  72. if (x && x->autoDelete() && !--x->ref) delete x;
  73. putLockWait_.wakeOne();
  74. throw ex;
  75. }
  76. }
  77. }
  78. }
  79. ctkEARunnable* ctkEALinkedQueue::peek() const
  80. {
  81. QMutexLocker l(&headLock_);
  82. ctkEALinkedNode* first = head_->next;
  83. if (first)
  84. return first->value;
  85. else
  86. return 0;
  87. }
  88. bool ctkEALinkedQueue::isEmpty() const
  89. {
  90. QMutexLocker l(&headLock_);
  91. return !(head_->next);
  92. }
  93. ctkEARunnable* ctkEALinkedQueue::poll(long msecs)
  94. {
  95. if (ctkEAInterruptibleThread::interrupted()) throw ctkEAInterruptedException();
  96. ctkEARunnable* x = extract();
  97. if (x != 0)
  98. return x;
  99. else
  100. {
  101. QMutexLocker l(&putLock_);
  102. try {
  103. qint64 waitTime = static_cast<qint64>(msecs);
  104. ctkHighPrecisionTimer t;
  105. t.start();
  106. ++waitingForTake_;
  107. forever
  108. {
  109. x = extract();
  110. if (x != 0 || waitTime <= 0)
  111. {
  112. --waitingForTake_;
  113. return x;
  114. }
  115. else
  116. {
  117. ctkEAInterruptibleThread::currentThread()->wait(&putLock_, &putLockWait_, waitTime);
  118. waitTime = static_cast<qint64>(msecs) - t.elapsedMilli();
  119. }
  120. }
  121. }
  122. catch(const ctkEAInterruptedException& ex)
  123. {
  124. --waitingForTake_;
  125. if (x && x->autoDelete() && !--x->ref) delete x;
  126. putLockWait_.wakeOne();
  127. throw ex;
  128. }
  129. }
  130. }
  131. void ctkEALinkedQueue::insert(ctkEARunnable* x)
  132. {
  133. QMutexLocker l(&putLock_);
  134. ctkEALinkedNode* p = new ctkEALinkedNode(x);
  135. {
  136. QMutexLocker l2(&lastLock_);
  137. last_->next = p;
  138. last_ = p;
  139. }
  140. if (waitingForTake_ > 0)
  141. {
  142. putLockWait_.wakeOne();
  143. }
  144. }
  145. ctkEARunnable* ctkEALinkedQueue::extract()
  146. {
  147. QMutexLocker lock(&mutex_);
  148. {
  149. QMutexLocker l(&headLock_);
  150. ctkEARunnable* x = 0;
  151. ctkEALinkedNode* first(head_->next);
  152. if (first)
  153. {
  154. x = first->value;
  155. first->value = 0;
  156. delete head_;
  157. head_ = first;
  158. }
  159. return x;
  160. }
  161. }