source: XMLIO_V2/external/src/POCO/Foundation/TimedNotificationQueue.cpp @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
1//
2// TimedNotificationQueue.cpp
3//
4// $Id: //poco/1.3/Foundation/src/TimedNotificationQueue.cpp#5 $
5//
6// Library: Foundation
7// Package: Notifications
8// Module:  TimedNotificationQueue
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/TimedNotificationQueue.h>
38#include <Poco/Notification.h>
39#include <limits>
40
41
42namespace Poco {
43
44
45TimedNotificationQueue::TimedNotificationQueue()
46{
47}
48
49
50TimedNotificationQueue::~TimedNotificationQueue()
51{
52        clear();
53}
54
55
56void TimedNotificationQueue::enqueueNotification(Notification::Ptr pNotification, Timestamp timestamp)
57{
58        poco_check_ptr (pNotification);
59
60        FastMutex::ScopedLock lock(_mutex);
61        _nfQueue.insert(NfQueue::value_type(timestamp, pNotification));
62        _nfAvailable.set();
63}
64
65
66Notification* TimedNotificationQueue::dequeueNotification()
67{
68        FastMutex::ScopedLock lock(_mutex);
69
70        NfQueue::iterator it = _nfQueue.begin();
71        if (it != _nfQueue.end())
72        {
73                Timestamp::TimeDiff sleep = -it->first.elapsed();
74                if (sleep <= 0)
75                {
76                        Notification::Ptr pNf = it->second;
77                        _nfQueue.erase(it);
78                        return pNf.duplicate();
79                }
80        }
81        return 0;
82}
83
84
85Notification* TimedNotificationQueue::waitDequeueNotification()
86{
87        for (;;)
88        {
89                _mutex.lock();
90                NfQueue::iterator it = _nfQueue.begin();
91                if (it != _nfQueue.end())
92                {
93                        _mutex.unlock();
94                        Timestamp::TimeDiff sleep = -it->first.elapsed();
95                        if (sleep <= 0)
96                        {
97                                return dequeueOne(it).duplicate();
98                        }
99                        else if (!wait(sleep))
100                        {
101                                return dequeueOne(it).duplicate();
102                        }
103                        else continue;
104                }
105                else
106                {
107                        _mutex.unlock();
108                }
109                _nfAvailable.wait();
110        }
111}
112
113
114Notification* TimedNotificationQueue::waitDequeueNotification(long milliseconds)
115{
116        while (milliseconds >= 0)
117        {
118                _mutex.lock();
119                NfQueue::iterator it = _nfQueue.begin();
120                if (it != _nfQueue.end())
121                {
122                        _mutex.unlock();
123                        Poco::Timestamp now;
124                        Timestamp::TimeDiff sleep = it->first - now;
125                        if (sleep <= 0)
126                        {
127                                return dequeueOne(it).duplicate();
128                        }
129                        else if (sleep <= 1000*Timestamp::TimeDiff(milliseconds))
130                        {
131                                if (!wait(sleep))
132                                {
133                                        return dequeueOne(it).duplicate();
134                                }
135                                else 
136                                {
137                                        milliseconds -= static_cast<long>((now.elapsed() + 999)/1000);
138                                        continue;
139                                }
140                        }
141                }
142                else
143                {
144                        _mutex.unlock();
145                }
146                if (milliseconds > 0)
147                {
148                        Poco::Timestamp now;
149                        _nfAvailable.tryWait(milliseconds);
150                        milliseconds -= static_cast<long>((now.elapsed() + 999)/1000);
151                }
152                else return 0;
153        }
154        return 0;
155}
156
157
158bool TimedNotificationQueue::wait(Timestamp::TimeDiff interval)
159{
160        const Timestamp::TimeDiff MAX_SLEEP = 8*60*60*Timestamp::TimeDiff(1000000); // sleep at most 8 hours at a time
161        while (interval > 0)
162        {
163                Timestamp now;
164                Timestamp::TimeDiff sleep = interval <= MAX_SLEEP ? interval : MAX_SLEEP;
165                if (_nfAvailable.tryWait(static_cast<long>((sleep + 999)/1000)))
166                        return true;
167                interval -= now.elapsed();
168        }
169        return false;
170}
171
172
173bool TimedNotificationQueue::empty() const
174{
175        FastMutex::ScopedLock lock(_mutex);
176        return _nfQueue.empty();
177}
178
179       
180int TimedNotificationQueue::size() const
181{
182        FastMutex::ScopedLock lock(_mutex);
183        return static_cast<int>(_nfQueue.size());
184}
185
186
187void TimedNotificationQueue::clear()
188{
189        FastMutex::ScopedLock lock(_mutex);
190        _nfQueue.clear();       
191}
192
193
194Notification::Ptr TimedNotificationQueue::dequeueOne(NfQueue::iterator& it)
195{
196        FastMutex::ScopedLock lock(_mutex);
197        Notification::Ptr pNf = it->second;
198        _nfQueue.erase(it);
199        return pNf;
200}
201
202
203} // namespace Poco
Note: See TracBrowser for help on using the repository browser.