bump product version to 4.2.0.1
[LibreOffice.git] / salhelper / source / timer.cxx
blob6b8a5ad13fd1e119c7c4f1bd1b94100e513ca5de
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #include <salhelper/timer.hxx>
21 #include <osl/diagnose.h>
22 #include <salhelper/simplereferenceobject.hxx>
23 #include <osl/thread.hxx>
24 #include <osl/conditn.hxx>
25 #include <osl/mutex.hxx>
26 #include <rtl/instance.hxx>
28 using namespace salhelper;
30 class salhelper::TimerManager : public osl::Thread
33 public:
35 ///
36 TimerManager();
38 ///
39 ~TimerManager();
41 /// register timer
42 sal_Bool SAL_CALL registerTimer(salhelper::Timer* pTimer);
44 /// unregister timer
45 sal_Bool SAL_CALL unregisterTimer(salhelper::Timer* pTimer);
47 /// lookup timer
48 sal_Bool SAL_CALL lookupTimer(const salhelper::Timer* pTimer);
50 /// retrieves the "Singleton" TimerManager Instance
51 static TimerManager* SAL_CALL getTimerManager();
54 protected:
56 /// worker-function of thread
57 virtual void SAL_CALL run();
59 // Checking and triggering of a timer event
60 void SAL_CALL checkForTimeout();
62 // cleanup Method
63 virtual void SAL_CALL onTerminated();
65 // sorted-queue data
66 salhelper::Timer* m_pHead;
67 // List Protection
68 osl::Mutex m_Lock;
69 // Signal the insertion of a timer
70 osl::Condition m_notEmpty;
72 // "Singleton Pattern"
73 static salhelper::TimerManager* m_pManager;
77 /////////////////////////////////////////////////////////////////////////////
79 // Timer class
82 Timer::Timer()
83 : m_aTimeOut( 0 ),
84 m_aExpired( 0 ),
85 m_aRepeatDelta( 0 ),
86 m_pNext( NULL )
90 Timer::Timer( const TTimeValue& Time )
91 : m_aTimeOut( Time ),
92 m_aExpired( 0 ),
93 m_aRepeatDelta( 0 ),
94 m_pNext( NULL )
98 Timer::Timer( const TTimeValue& Time, const TTimeValue& Repeat )
99 : m_aTimeOut( Time ),
100 m_aExpired( 0 ),
101 m_aRepeatDelta( Repeat ),
102 m_pNext( NULL )
106 Timer::~Timer()
108 stop();
111 void Timer::start()
113 if (! isTicking())
115 if (! m_aTimeOut.isEmpty())
116 setRemainingTime(m_aTimeOut);
118 TimerManager *pManager = TimerManager::getTimerManager();
120 OSL_ASSERT(pManager);
122 if ( pManager != 0 )
124 pManager->registerTimer(this);
129 void Timer::stop()
131 TimerManager *pManager = TimerManager::getTimerManager();
133 OSL_ASSERT(pManager);
135 if ( pManager != 0 )
137 pManager->unregisterTimer(this);
141 sal_Bool Timer::isTicking() const
143 TimerManager *pManager = TimerManager::getTimerManager();
145 OSL_ASSERT(pManager);
147 if (pManager)
148 return pManager->lookupTimer(this);
149 else
150 return sal_False;
154 sal_Bool Timer::isExpired() const
156 TTimeValue Now;
158 osl_getSystemTime(&Now);
160 return !(Now < m_aExpired);
163 sal_Bool Timer::expiresBefore(const Timer* pTimer) const
165 OSL_ASSERT(pTimer);
167 if ( pTimer != 0 )
169 return m_aExpired < pTimer->m_aExpired;
171 else
173 return sal_False;
177 void Timer::setAbsoluteTime(const TTimeValue& Time)
179 m_aTimeOut = 0;
180 m_aExpired = Time;
181 m_aRepeatDelta = 0;
183 m_aExpired.normalize();
186 void Timer::setRemainingTime(const TTimeValue& Remaining)
188 osl_getSystemTime(&m_aExpired);
190 m_aExpired.addTime(Remaining);
193 void Timer::setRemainingTime(const TTimeValue& Remaining, const TTimeValue& Repeat)
195 osl_getSystemTime(&m_aExpired);
197 m_aExpired.addTime(Remaining);
199 m_aRepeatDelta = Repeat;
202 void Timer::addTime(const TTimeValue& Delta)
204 m_aExpired.addTime(Delta);
207 TTimeValue Timer::getRemainingTime() const
209 TTimeValue Now;
211 osl_getSystemTime(&Now);
213 sal_Int32 secs = m_aExpired.Seconds - Now.Seconds;
215 if (secs < 0)
216 return TTimeValue(0, 0);
218 sal_Int32 nsecs = m_aExpired.Nanosec - Now.Nanosec;
220 if (nsecs < 0)
222 if (secs > 0)
224 secs -= 1;
225 nsecs += 1000000000L;
227 else
228 return TTimeValue(0, 0);
231 return TTimeValue(secs, nsecs);
235 /////////////////////////////////////////////////////////////////////////////
237 // Timer manager
239 namespace
241 // Synchronize access to TimerManager
242 struct theTimerManagerMutex : public rtl::Static< osl::Mutex, theTimerManagerMutex> {};
245 TimerManager* salhelper::TimerManager::m_pManager = NULL;
247 TimerManager::TimerManager()
249 osl::MutexGuard Guard(theTimerManagerMutex::get());
251 OSL_ASSERT(m_pManager == 0);
253 m_pManager = this;
255 m_pHead= 0;
257 m_notEmpty.reset();
259 // start thread
260 create();
263 TimerManager::~TimerManager()
265 osl::MutexGuard Guard(theTimerManagerMutex::get());
267 if ( m_pManager == this )
268 m_pManager = 0;
271 void TimerManager::onTerminated()
273 delete this; // mfe: AAARRRGGGHHH!!!
276 TimerManager* TimerManager::getTimerManager()
278 osl::MutexGuard Guard(theTimerManagerMutex::get());
280 if (! m_pManager)
281 new TimerManager;
283 return m_pManager;
286 sal_Bool TimerManager::registerTimer(Timer* pTimer)
288 OSL_ASSERT(pTimer);
290 if ( pTimer == 0 )
292 return sal_False;
295 osl::MutexGuard Guard(m_Lock);
297 // try to find one with equal or lower remaining time.
298 Timer** ppIter = &m_pHead;
300 while (*ppIter)
302 if (pTimer->expiresBefore(*ppIter))
304 // next element has higher remaining time,
305 // => insert new timer before
306 break;
308 ppIter= &((*ppIter)->m_pNext);
311 // next element has higher remaining time,
312 // => insert new timer before
313 pTimer->m_pNext= *ppIter;
314 *ppIter = pTimer;
317 if (pTimer == m_pHead)
319 // it was inserted as new head
320 // signal it to TimerManager Thread
321 m_notEmpty.set();
324 return sal_True;
327 sal_Bool TimerManager::unregisterTimer(Timer* pTimer)
329 OSL_ASSERT(pTimer);
331 if ( pTimer == 0 )
333 return sal_False;
336 // lock access
337 osl::MutexGuard Guard(m_Lock);
339 Timer** ppIter = &m_pHead;
341 while (*ppIter)
343 if (pTimer == (*ppIter))
345 // remove timer from list
346 *ppIter = (*ppIter)->m_pNext;
347 return sal_True;
349 ppIter= &((*ppIter)->m_pNext);
352 return sal_False;
355 sal_Bool TimerManager::lookupTimer(const Timer* pTimer)
357 OSL_ASSERT(pTimer);
359 if ( pTimer == 0 )
361 return sal_False;
364 // lock access
365 osl::MutexGuard Guard(m_Lock);
367 // check the list
368 for (Timer* pIter = m_pHead; pIter != 0; pIter= pIter->m_pNext)
370 if (pIter == pTimer)
372 return sal_True;
376 return sal_False;
379 void TimerManager::checkForTimeout()
382 m_Lock.acquire();
384 if ( m_pHead == 0 )
386 m_Lock.release();
387 return;
390 Timer* pTimer = m_pHead;
392 if (pTimer->isExpired())
394 // remove expired timer
395 m_pHead = pTimer->m_pNext;
397 pTimer->acquire();
399 m_Lock.release();
401 pTimer->onShot();
403 // restart timer if specified
404 if ( ! pTimer->m_aRepeatDelta.isEmpty() )
406 TTimeValue Now;
408 osl_getSystemTime(&Now);
410 Now.Seconds += pTimer->m_aRepeatDelta.Seconds;
411 Now.Nanosec += pTimer->m_aRepeatDelta.Nanosec;
413 pTimer->m_aExpired = Now;
415 registerTimer(pTimer);
417 pTimer->release();
419 else
421 m_Lock.release();
425 return;
428 void TimerManager::run()
430 setPriority( osl_Thread_PriorityBelowNormal );
432 while (schedule())
434 TTimeValue delay;
435 TTimeValue* pDelay=0;
438 m_Lock.acquire();
440 if (m_pHead != 0)
442 delay = m_pHead->getRemainingTime();
443 pDelay=&delay;
445 else
447 pDelay=0;
451 m_notEmpty.reset();
453 m_Lock.release();
456 m_notEmpty.wait(pDelay);
458 checkForTimeout();
464 /////////////////////////////////////////////////////////////////////////////
466 // Timer manager cleanup
469 // jbu:
470 // The timer manager cleanup has been removed (no thread is killed anymore).
471 // So the thread leaks.
472 // This will result in a GPF in case the salhelper-library gets unloaded before
473 // process termination.
474 // -> TODO : rewrite this file, so that the timerManager thread gets destroyed,
475 // when there are no timers anymore !
477 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */