ctkEALinkedQueue.cpp 4.4 KB

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