1 | // |
---|
2 | // PriorityNotificationQueue.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/PriorityNotificationQueue.cpp#2 $ |
---|
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 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include <Poco/PriorityNotificationQueue.h> |
---|
38 | #include <Poco/NotificationCenter.h> |
---|
39 | #include <Poco/Notification.h> |
---|
40 | #include <Poco/SingletonHolder.h> |
---|
41 | |
---|
42 | |
---|
43 | namespace Poco { |
---|
44 | |
---|
45 | |
---|
46 | PriorityNotificationQueue::PriorityNotificationQueue() |
---|
47 | { |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | PriorityNotificationQueue::~PriorityNotificationQueue() |
---|
52 | { |
---|
53 | clear(); |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | void PriorityNotificationQueue::enqueueNotification(Notification::Ptr pNotification, int priority) |
---|
58 | { |
---|
59 | poco_check_ptr (pNotification); |
---|
60 | FastMutex::ScopedLock lock(_mutex); |
---|
61 | if (_waitQueue.empty()) |
---|
62 | { |
---|
63 | _nfQueue.insert(NfQueue::value_type(priority, pNotification)); |
---|
64 | } |
---|
65 | else |
---|
66 | { |
---|
67 | poco_assert_dbg(_nfQueue.empty()); |
---|
68 | WaitInfo* pWI = _waitQueue.front(); |
---|
69 | _waitQueue.pop_front(); |
---|
70 | pWI->pNf = pNotification; |
---|
71 | pWI->nfAvailable.set(); |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | Notification* PriorityNotificationQueue::dequeueNotification() |
---|
77 | { |
---|
78 | FastMutex::ScopedLock lock(_mutex); |
---|
79 | return dequeueOne().duplicate(); |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | Notification* PriorityNotificationQueue::waitDequeueNotification() |
---|
84 | { |
---|
85 | Notification::Ptr pNf; |
---|
86 | WaitInfo* pWI = 0; |
---|
87 | { |
---|
88 | FastMutex::ScopedLock lock(_mutex); |
---|
89 | pNf = dequeueOne(); |
---|
90 | if (pNf) return pNf.duplicate(); |
---|
91 | pWI = new WaitInfo; |
---|
92 | _waitQueue.push_back(pWI); |
---|
93 | } |
---|
94 | pWI->nfAvailable.wait(); |
---|
95 | pNf = pWI->pNf; |
---|
96 | delete pWI; |
---|
97 | return pNf.duplicate(); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | Notification* PriorityNotificationQueue::waitDequeueNotification(long milliseconds) |
---|
102 | { |
---|
103 | Notification::Ptr pNf; |
---|
104 | WaitInfo* pWI = 0; |
---|
105 | { |
---|
106 | FastMutex::ScopedLock lock(_mutex); |
---|
107 | pNf = dequeueOne(); |
---|
108 | if (pNf) return pNf.duplicate(); |
---|
109 | pWI = new WaitInfo; |
---|
110 | _waitQueue.push_back(pWI); |
---|
111 | } |
---|
112 | if (pWI->nfAvailable.tryWait(milliseconds)) |
---|
113 | { |
---|
114 | pNf = pWI->pNf; |
---|
115 | } |
---|
116 | else |
---|
117 | { |
---|
118 | FastMutex::ScopedLock lock(_mutex); |
---|
119 | pNf = pWI->pNf; |
---|
120 | for (WaitQueue::iterator it = _waitQueue.begin(); it != _waitQueue.end(); ++it) |
---|
121 | { |
---|
122 | if (*it == pWI) |
---|
123 | { |
---|
124 | _waitQueue.erase(it); |
---|
125 | break; |
---|
126 | } |
---|
127 | } |
---|
128 | } |
---|
129 | delete pWI; |
---|
130 | return pNf.duplicate(); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | void PriorityNotificationQueue::dispatch(NotificationCenter& notificationCenter) |
---|
135 | { |
---|
136 | FastMutex::ScopedLock lock(_mutex); |
---|
137 | Notification::Ptr pNf = dequeueOne(); |
---|
138 | while (pNf) |
---|
139 | { |
---|
140 | notificationCenter.postNotification(pNf); |
---|
141 | pNf = dequeueOne(); |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | void PriorityNotificationQueue::wakeUpAll() |
---|
147 | { |
---|
148 | FastMutex::ScopedLock lock(_mutex); |
---|
149 | for (WaitQueue::iterator it = _waitQueue.begin(); it != _waitQueue.end(); ++it) |
---|
150 | { |
---|
151 | (*it)->nfAvailable.set(); |
---|
152 | } |
---|
153 | _waitQueue.clear(); |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | bool PriorityNotificationQueue::empty() const |
---|
158 | { |
---|
159 | FastMutex::ScopedLock lock(_mutex); |
---|
160 | return _nfQueue.empty(); |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | int PriorityNotificationQueue::size() const |
---|
165 | { |
---|
166 | FastMutex::ScopedLock lock(_mutex); |
---|
167 | return static_cast<int>(_nfQueue.size()); |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | void PriorityNotificationQueue::clear() |
---|
172 | { |
---|
173 | FastMutex::ScopedLock lock(_mutex); |
---|
174 | _nfQueue.clear(); |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | bool PriorityNotificationQueue::hasIdleThreads() const |
---|
179 | { |
---|
180 | FastMutex::ScopedLock lock(_mutex); |
---|
181 | return !_waitQueue.empty(); |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | Notification::Ptr PriorityNotificationQueue::dequeueOne() |
---|
186 | { |
---|
187 | Notification::Ptr pNf; |
---|
188 | NfQueue::iterator it = _nfQueue.begin(); |
---|
189 | if (it != _nfQueue.end()) |
---|
190 | { |
---|
191 | pNf = it->second; |
---|
192 | _nfQueue.erase(it); |
---|
193 | } |
---|
194 | return pNf; |
---|
195 | } |
---|
196 | |
---|
197 | |
---|
198 | PriorityNotificationQueue& PriorityNotificationQueue::defaultQueue() |
---|
199 | { |
---|
200 | static SingletonHolder<PriorityNotificationQueue> sh; |
---|
201 | return *sh.get(); |
---|
202 | } |
---|
203 | |
---|
204 | |
---|
205 | } // namespace Poco |
---|