1 | // |
---|
2 | // TimedNotificationQueue.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/TimedNotificationQueue.h#4 $ |
---|
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 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_TimedNotificationQueue_INCLUDED |
---|
40 | #define Foundation_TimedNotificationQueue_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/Notification.h" |
---|
45 | #include "Poco/Mutex.h" |
---|
46 | #include "Poco/Event.h" |
---|
47 | #include "Poco/Timestamp.h" |
---|
48 | #include <map> |
---|
49 | |
---|
50 | |
---|
51 | namespace Poco { |
---|
52 | |
---|
53 | |
---|
54 | class Foundation_API TimedNotificationQueue |
---|
55 | /// A TimedNotificationQueue object provides a way to implement timed, asynchronous |
---|
56 | /// notifications. This is especially useful for sending notifications |
---|
57 | /// from one thread to another, for example from a background thread to |
---|
58 | /// the main (user interface) thread. |
---|
59 | /// |
---|
60 | /// The TimedNotificationQueue is quite similar to the NotificationQueue class. |
---|
61 | /// The only difference to NotificationQueue is that each Notification is tagged |
---|
62 | /// with a Timestamp. When inserting a Notification into the queue, the |
---|
63 | /// Notification is inserted according to the given Timestamp, with |
---|
64 | /// lower Timestamp values being inserted before higher ones. |
---|
65 | /// |
---|
66 | /// Notifications are dequeued in order of their timestamps. |
---|
67 | /// |
---|
68 | /// TimedNotificationQueue has some restrictions regarding multithreaded use. |
---|
69 | /// While multiple threads may enqueue notifications, only one thread at a |
---|
70 | /// time may dequeue notifications from the queue. |
---|
71 | /// |
---|
72 | /// If two threads try to dequeue a notification simultaneously, the results |
---|
73 | /// are undefined. |
---|
74 | { |
---|
75 | public: |
---|
76 | TimedNotificationQueue(); |
---|
77 | /// Creates the TimedNotificationQueue. |
---|
78 | |
---|
79 | ~TimedNotificationQueue(); |
---|
80 | /// Destroys the TimedNotificationQueue. |
---|
81 | |
---|
82 | void enqueueNotification(Notification::Ptr pNotification, Timestamp timestamp); |
---|
83 | /// Enqueues the given notification by adding it to |
---|
84 | /// the queue according to the given timestamp. |
---|
85 | /// Lower timestamp values are inserted before higher ones. |
---|
86 | /// The queue takes ownership of the notification, thus |
---|
87 | /// a call like |
---|
88 | /// notificationQueue.enqueueNotification(new MyNotification, someTime); |
---|
89 | /// does not result in a memory leak. |
---|
90 | |
---|
91 | Notification* dequeueNotification(); |
---|
92 | /// Dequeues the next pending notification with a timestamp |
---|
93 | /// less than or equal to the current time. |
---|
94 | /// Returns 0 (null) if no notification is available. |
---|
95 | /// The caller gains ownership of the notification and |
---|
96 | /// is expected to release it when done with it. |
---|
97 | /// |
---|
98 | /// It is highly recommended that the result is immediately |
---|
99 | /// assigned to a Notification::Ptr, to avoid potential |
---|
100 | /// memory management issues. |
---|
101 | |
---|
102 | Notification* waitDequeueNotification(); |
---|
103 | /// Dequeues the next pending notification. |
---|
104 | /// If no notification is available, waits for a notification |
---|
105 | /// to be enqueued. |
---|
106 | /// The caller gains ownership of the notification and |
---|
107 | /// is expected to release it when done with it. |
---|
108 | /// |
---|
109 | /// It is highly recommended that the result is immediately |
---|
110 | /// assigned to a Notification::Ptr, to avoid potential |
---|
111 | /// memory management issues. |
---|
112 | |
---|
113 | Notification* waitDequeueNotification(long milliseconds); |
---|
114 | /// Dequeues the next pending notification. |
---|
115 | /// If no notification is available, waits for a notification |
---|
116 | /// to be enqueued up to the specified time. |
---|
117 | /// Returns 0 (null) if no notification is available. |
---|
118 | /// The caller gains ownership of the notification and |
---|
119 | /// is expected to release it when done with it. |
---|
120 | /// |
---|
121 | /// It is highly recommended that the result is immediately |
---|
122 | /// assigned to a Notification::Ptr, to avoid potential |
---|
123 | /// memory management issues. |
---|
124 | |
---|
125 | bool empty() const; |
---|
126 | /// Returns true iff the queue is empty. |
---|
127 | |
---|
128 | int size() const; |
---|
129 | /// Returns the number of notifications in the queue. |
---|
130 | |
---|
131 | void clear(); |
---|
132 | /// Removes all notifications from the queue. |
---|
133 | /// |
---|
134 | /// Calling clear() while another thread executes one of |
---|
135 | /// the dequeue member functions will result in undefined |
---|
136 | /// behavior. |
---|
137 | |
---|
138 | protected: |
---|
139 | typedef std::multimap<Timestamp, Notification::Ptr> NfQueue; |
---|
140 | Notification::Ptr dequeueOne(NfQueue::iterator& it); |
---|
141 | bool wait(Timestamp::TimeDiff interval); |
---|
142 | |
---|
143 | private: |
---|
144 | NfQueue _nfQueue; |
---|
145 | Event _nfAvailable; |
---|
146 | mutable FastMutex _mutex; |
---|
147 | }; |
---|
148 | |
---|
149 | |
---|
150 | } // namespace Poco |
---|
151 | |
---|
152 | |
---|
153 | #endif // Foundation_TimedNotificationQueue_INCLUDED |
---|