1 | // |
---|
2 | // PriorityNotificationQueue.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/PriorityNotificationQueue.h#3 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Notifications |
---|
8 | // Module: PriorityNotificationQueue |
---|
9 | // |
---|
10 | // Definition of the PriorityNotificationQueue 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_PriorityNotificationQueue_INCLUDED |
---|
40 | #define Foundation_PriorityNotificationQueue_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 <map> |
---|
48 | #include <deque> |
---|
49 | |
---|
50 | |
---|
51 | namespace Poco { |
---|
52 | |
---|
53 | |
---|
54 | class NotificationCenter; |
---|
55 | |
---|
56 | |
---|
57 | class Foundation_API PriorityNotificationQueue |
---|
58 | /// A PriorityNotificationQueue object provides a way to implement asynchronous |
---|
59 | /// notifications. This is especially useful for sending notifications |
---|
60 | /// from one thread to another, for example from a background thread to |
---|
61 | /// the main (user interface) thread. |
---|
62 | /// |
---|
63 | /// The PriorityNotificationQueue is quite similar to the NotificationQueue class. |
---|
64 | /// The only difference to NotificationQueue is that each Notification is tagged |
---|
65 | /// with a priority value. When inserting a Notification into the queue, the |
---|
66 | /// Notification is inserted according to the given priority value, with |
---|
67 | /// lower priority values being inserted before higher priority |
---|
68 | /// values. Therefore, the lower the numerical priority value, the higher |
---|
69 | /// the actual notification priority. |
---|
70 | /// |
---|
71 | /// Notifications are dequeued in order of their priority. |
---|
72 | /// |
---|
73 | /// The PriorityNotificationQueue can also be used to distribute work from |
---|
74 | /// a controlling thread to one or more worker threads. Each worker thread |
---|
75 | /// repeatedly calls waitDequeueNotification() and processes the |
---|
76 | /// returned notification. Special care must be taken when shutting |
---|
77 | /// down a queue with worker threads waiting for notifications. |
---|
78 | /// The recommended sequence to shut down and destroy the queue is to |
---|
79 | /// 1. set a termination flag for every worker thread |
---|
80 | /// 2. call the wakeUpAll() method |
---|
81 | /// 3. join each worker thread |
---|
82 | /// 4. destroy the notification queue. |
---|
83 | { |
---|
84 | public: |
---|
85 | PriorityNotificationQueue(); |
---|
86 | /// Creates the PriorityNotificationQueue. |
---|
87 | |
---|
88 | ~PriorityNotificationQueue(); |
---|
89 | /// Destroys the PriorityNotificationQueue. |
---|
90 | |
---|
91 | void enqueueNotification(Notification::Ptr pNotification, int priority); |
---|
92 | /// Enqueues the given notification by adding it to |
---|
93 | /// the queue according to the given priority. |
---|
94 | /// Lower priority values are inserted before higher priority values. |
---|
95 | /// The queue takes ownership of the notification, thus |
---|
96 | /// a call like |
---|
97 | /// notificationQueue.enqueueNotification(new MyNotification, 1); |
---|
98 | /// does not result in a memory leak. |
---|
99 | |
---|
100 | Notification* dequeueNotification(); |
---|
101 | /// Dequeues the next pending notification. |
---|
102 | /// Returns 0 (null) if no notification is available. |
---|
103 | /// The caller gains ownership of the notification and |
---|
104 | /// is expected to release it when done with it. |
---|
105 | /// |
---|
106 | /// It is highly recommended that the result is immediately |
---|
107 | /// assigned to a Notification::Ptr, to avoid potential |
---|
108 | /// memory management issues. |
---|
109 | |
---|
110 | Notification* waitDequeueNotification(); |
---|
111 | /// Dequeues the next pending notification. |
---|
112 | /// If no notification is available, waits for a notification |
---|
113 | /// to be enqueued. |
---|
114 | /// The caller gains ownership of the notification and |
---|
115 | /// is expected to release it when done with it. |
---|
116 | /// This method returns 0 (null) if wakeUpAll() |
---|
117 | /// has been called by another thread. |
---|
118 | /// |
---|
119 | /// It is highly recommended that the result is immediately |
---|
120 | /// assigned to a Notification::Ptr, to avoid potential |
---|
121 | /// memory management issues. |
---|
122 | |
---|
123 | Notification* waitDequeueNotification(long milliseconds); |
---|
124 | /// Dequeues the next pending notification. |
---|
125 | /// If no notification is available, waits for a notification |
---|
126 | /// to be enqueued up to the specified time. |
---|
127 | /// Returns 0 (null) if no notification is available. |
---|
128 | /// The caller gains ownership of the notification and |
---|
129 | /// is expected to release it when done with it. |
---|
130 | /// |
---|
131 | /// It is highly recommended that the result is immediately |
---|
132 | /// assigned to a Notification::Ptr, to avoid potential |
---|
133 | /// memory management issues. |
---|
134 | |
---|
135 | void dispatch(NotificationCenter& notificationCenter); |
---|
136 | /// Dispatches all queued notifications to the given |
---|
137 | /// notification center. |
---|
138 | |
---|
139 | void wakeUpAll(); |
---|
140 | /// Wakes up all threads that wait for a notification. |
---|
141 | |
---|
142 | bool empty() const; |
---|
143 | /// Returns true iff the queue is empty. |
---|
144 | |
---|
145 | int size() const; |
---|
146 | /// Returns the number of notifications in the queue. |
---|
147 | |
---|
148 | void clear(); |
---|
149 | /// Removes all notifications from the queue. |
---|
150 | |
---|
151 | bool hasIdleThreads() const; |
---|
152 | /// Returns true if the queue has at least one thread waiting |
---|
153 | /// for a notification. |
---|
154 | |
---|
155 | static PriorityNotificationQueue& defaultQueue(); |
---|
156 | /// Returns a reference to the default |
---|
157 | /// PriorityNotificationQueue. |
---|
158 | |
---|
159 | protected: |
---|
160 | Notification::Ptr dequeueOne(); |
---|
161 | |
---|
162 | private: |
---|
163 | typedef std::multimap<int, Notification::Ptr> NfQueue; |
---|
164 | struct WaitInfo |
---|
165 | { |
---|
166 | Notification::Ptr pNf; |
---|
167 | Event nfAvailable; |
---|
168 | }; |
---|
169 | typedef std::deque<WaitInfo*> WaitQueue; |
---|
170 | |
---|
171 | NfQueue _nfQueue; |
---|
172 | WaitQueue _waitQueue; |
---|
173 | mutable FastMutex _mutex; |
---|
174 | }; |
---|
175 | |
---|
176 | |
---|
177 | } // namespace Poco |
---|
178 | |
---|
179 | |
---|
180 | #endif // Foundation_PriorityNotificationQueue_INCLUDED |
---|