TimedNotificationQueue.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // TimedNotificationQueue.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/TimedNotificationQueue.h#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Notifications
  8. // Module: TimedNotificationQueue
  9. //
  10. // Definition of the TimedNotificationQueue class.
  11. //
  12. // Copyright (c) 2009, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_TimedNotificationQueue_INCLUDED
  18. #define Foundation_TimedNotificationQueue_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Notification.h"
  21. #include "Poco/Mutex.h"
  22. #include "Poco/Event.h"
  23. #include "Poco/Timestamp.h"
  24. #include "Poco/Clock.h"
  25. #include <map>
  26. namespace Poco {
  27. class Foundation_API TimedNotificationQueue
  28. /// A TimedNotificationQueue object provides a way to implement timed, asynchronous
  29. /// notifications. This is especially useful for sending notifications
  30. /// from one thread to another, for example from a background thread to
  31. /// the main (user interface) thread.
  32. ///
  33. /// The TimedNotificationQueue is quite similar to the NotificationQueue class.
  34. /// The only difference to NotificationQueue is that each Notification is tagged
  35. /// with a Timestamp. When inserting a Notification into the queue, the
  36. /// Notification is inserted according to the given Timestamp, with
  37. /// lower Timestamp values being inserted before higher ones.
  38. ///
  39. /// Notifications are dequeued in order of their timestamps.
  40. ///
  41. /// TimedNotificationQueue has some restrictions regarding multithreaded use.
  42. /// While multiple threads may enqueue notifications, only one thread at a
  43. /// time may dequeue notifications from the queue.
  44. ///
  45. /// If two threads try to dequeue a notification simultaneously, the results
  46. /// are undefined.
  47. {
  48. public:
  49. TimedNotificationQueue();
  50. /// Creates the TimedNotificationQueue.
  51. ~TimedNotificationQueue();
  52. /// Destroys the TimedNotificationQueue.
  53. void enqueueNotification(Notification::Ptr pNotification, Timestamp timestamp);
  54. /// Enqueues the given notification by adding it to
  55. /// the queue according to the given timestamp.
  56. /// Lower timestamp values are inserted before higher ones.
  57. /// The queue takes ownership of the notification, thus
  58. /// a call like
  59. /// notificationQueue.enqueueNotification(new MyNotification, someTime);
  60. /// does not result in a memory leak.
  61. ///
  62. /// The Timestamp is converted to an equivalent Clock value.
  63. void enqueueNotification(Notification::Ptr pNotification, Clock clock);
  64. /// Enqueues the given notification by adding it to
  65. /// the queue according to the given clock value.
  66. /// Lower clock values are inserted before higher ones.
  67. /// The queue takes ownership of the notification, thus
  68. /// a call like
  69. /// notificationQueue.enqueueNotification(new MyNotification, someTime);
  70. /// does not result in a memory leak.
  71. Notification* dequeueNotification();
  72. /// Dequeues the next pending notification with a timestamp
  73. /// less than or equal to the current time.
  74. /// Returns 0 (null) if no notification is available.
  75. /// The caller gains ownership of the notification and
  76. /// is expected to release it when done with it.
  77. ///
  78. /// It is highly recommended that the result is immediately
  79. /// assigned to a Notification::Ptr, to avoid potential
  80. /// memory management issues.
  81. Notification* waitDequeueNotification();
  82. /// Dequeues the next pending notification.
  83. /// If no notification is available, waits for a notification
  84. /// to be enqueued.
  85. /// The caller gains ownership of the notification and
  86. /// is expected to release it when done with it.
  87. ///
  88. /// It is highly recommended that the result is immediately
  89. /// assigned to a Notification::Ptr, to avoid potential
  90. /// memory management issues.
  91. Notification* waitDequeueNotification(long milliseconds);
  92. /// Dequeues the next pending notification.
  93. /// If no notification is available, waits for a notification
  94. /// to be enqueued up to the specified time.
  95. /// Returns 0 (null) if no notification is available.
  96. /// The caller gains ownership of the notification and
  97. /// is expected to release it when done with it.
  98. ///
  99. /// It is highly recommended that the result is immediately
  100. /// assigned to a Notification::Ptr, to avoid potential
  101. /// memory management issues.
  102. bool empty() const;
  103. /// Returns true iff the queue is empty.
  104. int size() const;
  105. /// Returns the number of notifications in the queue.
  106. void clear();
  107. /// Removes all notifications from the queue.
  108. ///
  109. /// Calling clear() while another thread executes one of
  110. /// the dequeue member functions will result in undefined
  111. /// behavior.
  112. protected:
  113. typedef std::multimap<Clock, Notification::Ptr> NfQueue;
  114. Notification::Ptr dequeueOne(NfQueue::iterator& it);
  115. bool wait(Clock::ClockDiff interval);
  116. private:
  117. NfQueue _nfQueue;
  118. Event _nfAvailable;
  119. mutable FastMutex _mutex;
  120. };
  121. } // namespace Poco
  122. #endif // Foundation_TimedNotificationQueue_INCLUDED