source: XMLIO_V2/external/src/POCO/Foundation.save/Timer.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.8 KB
Line 
1//
2// Timer.cpp
3//
4// $Id: //poco/1.3/Foundation/src/Timer.cpp#7 $
5//
6// Library: Foundation
7// Package: Threading
8// Module:  Timer
9//
10// Copyright (c) 2004-2006, 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/Timer.h"
38#include "Poco/ThreadPool.h"
39#include "Poco/Exception.h"
40#include "Poco/ErrorHandler.h"
41
42
43namespace Poco {
44
45
46Timer::Timer(long startInterval, long periodicInterval): 
47        _startInterval(startInterval), 
48        _periodicInterval(periodicInterval),
49        _pCallback(0)
50{
51        poco_assert (startInterval >= 0 && periodicInterval >= 0);
52}
53
54
55Timer::~Timer()
56{
57        stop();
58}
59
60
61void Timer::start(const AbstractTimerCallback& method)
62{
63        start(method, Thread::PRIO_NORMAL, ThreadPool::defaultPool());
64}
65
66
67void Timer::start(const AbstractTimerCallback& method, Thread::Priority priority)
68{
69        start(method, priority, ThreadPool::defaultPool());
70}
71
72
73void Timer::start(const AbstractTimerCallback& method, ThreadPool& threadPool)
74{
75        start(method, Thread::PRIO_NORMAL, threadPool);
76}
77
78
79void Timer::start(const AbstractTimerCallback& method, Thread::Priority priority, ThreadPool& threadPool)
80{
81        Timestamp nextInvocation;
82        nextInvocation += _startInterval*1000;
83
84        poco_assert (!_pCallback);
85
86        FastMutex::ScopedLock lock(_mutex);     
87        _nextInvocation = nextInvocation;
88        _pCallback = method.clone();
89        _wakeUp.reset();
90        threadPool.startWithPriority(priority, *this);
91}
92
93
94void Timer::stop()
95{
96        FastMutex::ScopedLock lock(_mutex);
97        if (_pCallback)
98        {
99                _periodicInterval = 0;
100                _mutex.unlock();
101                _wakeUp.set();
102                _done.wait(); // warning: deadlock if called from timer callback
103                _mutex.lock();
104                delete _pCallback;
105                _pCallback = 0;
106        }
107}
108
109
110void Timer::restart()
111{
112        FastMutex::ScopedLock lock(_mutex);
113        if (_pCallback)
114        {
115                _wakeUp.set();
116        }
117}
118
119
120void Timer::restart(long milliseconds)
121{
122        poco_assert (milliseconds >= 0);
123        FastMutex::ScopedLock lock(_mutex);
124        if (_pCallback)
125        {
126                _periodicInterval = milliseconds;
127                _wakeUp.set();
128        }
129}
130
131
132long Timer::getStartInterval() const
133{
134        FastMutex::ScopedLock lock(_mutex);
135        return _startInterval;
136}
137
138
139void Timer::setStartInterval(long milliseconds)
140{
141        poco_assert (milliseconds >= 0);
142        FastMutex::ScopedLock lock(_mutex);
143        _startInterval = milliseconds;
144}
145
146
147long Timer::getPeriodicInterval() const
148{
149        FastMutex::ScopedLock lock(_mutex);
150        return _periodicInterval;
151}
152
153
154void Timer::setPeriodicInterval(long milliseconds)
155{
156        poco_assert (milliseconds >= 0);
157        FastMutex::ScopedLock lock(_mutex);
158        _periodicInterval = milliseconds;
159}
160
161
162void Timer::run()
163{
164        Timestamp now;
165        long interval(0);
166        do
167        {
168                now.update();
169                long sleep = static_cast<long>((_nextInvocation - now)/1000);
170                if (sleep < 0) sleep = 0;
171                if (_wakeUp.tryWait(sleep))
172                {
173                        FastMutex::ScopedLock lock(_mutex);
174                        _nextInvocation.update();
175                        interval = _periodicInterval;
176                }
177                else
178                {
179                        try
180                        {
181                                _pCallback->invoke(*this);
182                        }
183                        catch (Exception& exc)
184                        {
185                                ErrorHandler::handle(exc);
186                        }
187                        catch (std::exception& exc)
188                        {
189                                ErrorHandler::handle(exc);
190                        }
191                        catch (...)
192                        {
193                                ErrorHandler::handle();
194                        }
195                        {
196                                FastMutex::ScopedLock lock(_mutex);
197                                interval = _periodicInterval;
198                        }
199                }
200                _nextInvocation += static_cast<Timestamp::TimeDiff>(interval)*1000;
201        }
202        while (interval > 0);
203        _done.set();
204}
205
206
207AbstractTimerCallback::AbstractTimerCallback()
208{
209}
210
211
212AbstractTimerCallback::AbstractTimerCallback(const AbstractTimerCallback& callback)
213{
214}
215
216
217AbstractTimerCallback::~AbstractTimerCallback()
218{
219}
220
221
222AbstractTimerCallback& AbstractTimerCallback::operator = (const AbstractTimerCallback& callback)
223{
224        return *this;
225}
226
227
228} // namespace Poco
Note: See TracBrowser for help on using the repository browser.