TimedNotificationQueue.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // TimedNotificationQueue.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/TimedNotificationQueue.cpp#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Notifications
  8. // Module: TimedNotificationQueue
  9. //
  10. // Copyright (c) 2009, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/TimedNotificationQueue.h"
  16. #include "Poco/Notification.h"
  17. #include <limits>
  18. namespace Poco {
  19. TimedNotificationQueue::TimedNotificationQueue()
  20. {
  21. }
  22. TimedNotificationQueue::~TimedNotificationQueue()
  23. {
  24. try
  25. {
  26. clear();
  27. }
  28. catch (...)
  29. {
  30. poco_unexpected();
  31. }
  32. }
  33. void TimedNotificationQueue::enqueueNotification(Notification::Ptr pNotification, Timestamp timestamp)
  34. {
  35. poco_check_ptr (pNotification);
  36. Timestamp tsNow;
  37. Clock clock;
  38. Timestamp::TimeDiff diff = timestamp - tsNow;
  39. clock += diff;
  40. FastMutex::ScopedLock lock(_mutex);
  41. _nfQueue.insert(NfQueue::value_type(clock, pNotification));
  42. _nfAvailable.set();
  43. }
  44. void TimedNotificationQueue::enqueueNotification(Notification::Ptr pNotification, Clock clock)
  45. {
  46. poco_check_ptr (pNotification);
  47. FastMutex::ScopedLock lock(_mutex);
  48. _nfQueue.insert(NfQueue::value_type(clock, pNotification));
  49. _nfAvailable.set();
  50. }
  51. Notification* TimedNotificationQueue::dequeueNotification()
  52. {
  53. FastMutex::ScopedLock lock(_mutex);
  54. NfQueue::iterator it = _nfQueue.begin();
  55. if (it != _nfQueue.end())
  56. {
  57. Clock::ClockDiff sleep = -it->first.elapsed();
  58. if (sleep <= 0)
  59. {
  60. Notification::Ptr pNf = it->second;
  61. _nfQueue.erase(it);
  62. return pNf.duplicate();
  63. }
  64. }
  65. return 0;
  66. }
  67. Notification* TimedNotificationQueue::waitDequeueNotification()
  68. {
  69. for (;;)
  70. {
  71. _mutex.lock();
  72. NfQueue::iterator it = _nfQueue.begin();
  73. if (it != _nfQueue.end())
  74. {
  75. _mutex.unlock();
  76. Clock::ClockDiff sleep = -it->first.elapsed();
  77. if (sleep <= 0)
  78. {
  79. return dequeueOne(it).duplicate();
  80. }
  81. else if (!wait(sleep))
  82. {
  83. return dequeueOne(it).duplicate();
  84. }
  85. else continue;
  86. }
  87. else
  88. {
  89. _mutex.unlock();
  90. }
  91. _nfAvailable.wait();
  92. }
  93. }
  94. Notification* TimedNotificationQueue::waitDequeueNotification(long milliseconds)
  95. {
  96. while (milliseconds >= 0)
  97. {
  98. _mutex.lock();
  99. NfQueue::iterator it = _nfQueue.begin();
  100. if (it != _nfQueue.end())
  101. {
  102. _mutex.unlock();
  103. Clock now;
  104. Clock::ClockDiff sleep = it->first - now;
  105. if (sleep <= 0)
  106. {
  107. return dequeueOne(it).duplicate();
  108. }
  109. else if (sleep <= 1000*Clock::ClockDiff(milliseconds))
  110. {
  111. if (!wait(sleep))
  112. {
  113. return dequeueOne(it).duplicate();
  114. }
  115. else
  116. {
  117. milliseconds -= static_cast<long>((now.elapsed() + 999)/1000);
  118. continue;
  119. }
  120. }
  121. }
  122. else
  123. {
  124. _mutex.unlock();
  125. }
  126. if (milliseconds > 0)
  127. {
  128. Clock now;
  129. _nfAvailable.tryWait(milliseconds);
  130. milliseconds -= static_cast<long>((now.elapsed() + 999)/1000);
  131. }
  132. else return 0;
  133. }
  134. return 0;
  135. }
  136. bool TimedNotificationQueue::wait(Clock::ClockDiff interval)
  137. {
  138. const Clock::ClockDiff MAX_SLEEP = 8*60*60*Clock::ClockDiff(1000000); // sleep at most 8 hours at a time
  139. while (interval > 0)
  140. {
  141. Clock now;
  142. Clock::ClockDiff sleep = interval <= MAX_SLEEP ? interval : MAX_SLEEP;
  143. if (_nfAvailable.tryWait(static_cast<long>((sleep + 999)/1000)))
  144. return true;
  145. interval -= now.elapsed();
  146. }
  147. return false;
  148. }
  149. bool TimedNotificationQueue::empty() const
  150. {
  151. FastMutex::ScopedLock lock(_mutex);
  152. return _nfQueue.empty();
  153. }
  154. int TimedNotificationQueue::size() const
  155. {
  156. FastMutex::ScopedLock lock(_mutex);
  157. return static_cast<int>(_nfQueue.size());
  158. }
  159. void TimedNotificationQueue::clear()
  160. {
  161. FastMutex::ScopedLock lock(_mutex);
  162. _nfQueue.clear();
  163. }
  164. Notification::Ptr TimedNotificationQueue::dequeueOne(NfQueue::iterator& it)
  165. {
  166. FastMutex::ScopedLock lock(_mutex);
  167. Notification::Ptr pNf = it->second;
  168. _nfQueue.erase(it);
  169. return pNf;
  170. }
  171. } // namespace Poco