PriorityNotificationQueue.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // PriorityNotificationQueue.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/PriorityNotificationQueue.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Notifications
  8. // Module: PriorityNotificationQueue
  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/PriorityNotificationQueue.h"
  16. #include "Poco/NotificationCenter.h"
  17. #include "Poco/Notification.h"
  18. #include "Poco/SingletonHolder.h"
  19. namespace Poco {
  20. PriorityNotificationQueue::PriorityNotificationQueue()
  21. {
  22. }
  23. PriorityNotificationQueue::~PriorityNotificationQueue()
  24. {
  25. try
  26. {
  27. clear();
  28. }
  29. catch (...)
  30. {
  31. poco_unexpected();
  32. }
  33. }
  34. void PriorityNotificationQueue::enqueueNotification(Notification::Ptr pNotification, int priority)
  35. {
  36. poco_check_ptr (pNotification);
  37. FastMutex::ScopedLock lock(_mutex);
  38. if (_waitQueue.empty())
  39. {
  40. _nfQueue.insert(NfQueue::value_type(priority, pNotification));
  41. }
  42. else
  43. {
  44. poco_assert_dbg(_nfQueue.empty());
  45. WaitInfo* pWI = _waitQueue.front();
  46. _waitQueue.pop_front();
  47. pWI->pNf = pNotification;
  48. pWI->nfAvailable.set();
  49. }
  50. }
  51. Notification* PriorityNotificationQueue::dequeueNotification()
  52. {
  53. FastMutex::ScopedLock lock(_mutex);
  54. return dequeueOne().duplicate();
  55. }
  56. Notification* PriorityNotificationQueue::waitDequeueNotification()
  57. {
  58. Notification::Ptr pNf;
  59. WaitInfo* pWI = 0;
  60. {
  61. FastMutex::ScopedLock lock(_mutex);
  62. pNf = dequeueOne();
  63. if (pNf) return pNf.duplicate();
  64. pWI = new WaitInfo;
  65. _waitQueue.push_back(pWI);
  66. }
  67. pWI->nfAvailable.wait();
  68. pNf = pWI->pNf;
  69. delete pWI;
  70. return pNf.duplicate();
  71. }
  72. Notification* PriorityNotificationQueue::waitDequeueNotification(long milliseconds)
  73. {
  74. Notification::Ptr pNf;
  75. WaitInfo* pWI = 0;
  76. {
  77. FastMutex::ScopedLock lock(_mutex);
  78. pNf = dequeueOne();
  79. if (pNf) return pNf.duplicate();
  80. pWI = new WaitInfo;
  81. _waitQueue.push_back(pWI);
  82. }
  83. if (pWI->nfAvailable.tryWait(milliseconds))
  84. {
  85. pNf = pWI->pNf;
  86. }
  87. else
  88. {
  89. FastMutex::ScopedLock lock(_mutex);
  90. pNf = pWI->pNf;
  91. for (WaitQueue::iterator it = _waitQueue.begin(); it != _waitQueue.end(); ++it)
  92. {
  93. if (*it == pWI)
  94. {
  95. _waitQueue.erase(it);
  96. break;
  97. }
  98. }
  99. }
  100. delete pWI;
  101. return pNf.duplicate();
  102. }
  103. void PriorityNotificationQueue::dispatch(NotificationCenter& notificationCenter)
  104. {
  105. FastMutex::ScopedLock lock(_mutex);
  106. Notification::Ptr pNf = dequeueOne();
  107. while (pNf)
  108. {
  109. notificationCenter.postNotification(pNf);
  110. pNf = dequeueOne();
  111. }
  112. }
  113. void PriorityNotificationQueue::wakeUpAll()
  114. {
  115. FastMutex::ScopedLock lock(_mutex);
  116. for (WaitQueue::iterator it = _waitQueue.begin(); it != _waitQueue.end(); ++it)
  117. {
  118. (*it)->nfAvailable.set();
  119. }
  120. _waitQueue.clear();
  121. }
  122. bool PriorityNotificationQueue::empty() const
  123. {
  124. FastMutex::ScopedLock lock(_mutex);
  125. return _nfQueue.empty();
  126. }
  127. int PriorityNotificationQueue::size() const
  128. {
  129. FastMutex::ScopedLock lock(_mutex);
  130. return static_cast<int>(_nfQueue.size());
  131. }
  132. void PriorityNotificationQueue::clear()
  133. {
  134. FastMutex::ScopedLock lock(_mutex);
  135. _nfQueue.clear();
  136. }
  137. bool PriorityNotificationQueue::hasIdleThreads() const
  138. {
  139. FastMutex::ScopedLock lock(_mutex);
  140. return !_waitQueue.empty();
  141. }
  142. Notification::Ptr PriorityNotificationQueue::dequeueOne()
  143. {
  144. Notification::Ptr pNf;
  145. NfQueue::iterator it = _nfQueue.begin();
  146. if (it != _nfQueue.end())
  147. {
  148. pNf = it->second;
  149. _nfQueue.erase(it);
  150. }
  151. return pNf;
  152. }
  153. namespace
  154. {
  155. static SingletonHolder<PriorityNotificationQueue> sh;
  156. }
  157. PriorityNotificationQueue& PriorityNotificationQueue::defaultQueue()
  158. {
  159. return *sh.get();
  160. }
  161. } // namespace Poco