xref: /aoo41x/main/vos/source/timer.cxx (revision e8c8fa4b)
1*e8c8fa4bSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*e8c8fa4bSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*e8c8fa4bSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*e8c8fa4bSAndrew Rist  * distributed with this work for additional information
6*e8c8fa4bSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*e8c8fa4bSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*e8c8fa4bSAndrew Rist  * "License"); you may not use this file except in compliance
9*e8c8fa4bSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*e8c8fa4bSAndrew Rist  *
11*e8c8fa4bSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*e8c8fa4bSAndrew Rist  *
13*e8c8fa4bSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*e8c8fa4bSAndrew Rist  * software distributed under the License is distributed on an
15*e8c8fa4bSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e8c8fa4bSAndrew Rist  * KIND, either express or implied.  See the License for the
17*e8c8fa4bSAndrew Rist  * specific language governing permissions and limitations
18*e8c8fa4bSAndrew Rist  * under the License.
19*e8c8fa4bSAndrew Rist  *
20*e8c8fa4bSAndrew Rist  *************************************************************/
21*e8c8fa4bSAndrew Rist 
22*e8c8fa4bSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <osl/time.h>
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <vos/timer.hxx>
27cdf0e10cSrcweir #include <vos/diagnose.hxx>
28cdf0e10cSrcweir #include <vos/ref.hxx>
29cdf0e10cSrcweir #include <vos/thread.hxx>
30cdf0e10cSrcweir #include <vos/conditn.hxx>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir 
33cdf0e10cSrcweir /////////////////////////////////////////////////////////////////////////////
34cdf0e10cSrcweir //
35cdf0e10cSrcweir // Timer manager
36cdf0e10cSrcweir //
37cdf0e10cSrcweir 
38cdf0e10cSrcweir class OTimerManagerCleanup;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir class vos::OTimerManager : public vos::OThread
41cdf0e10cSrcweir {
42cdf0e10cSrcweir 
43cdf0e10cSrcweir public:
44cdf0e10cSrcweir 
45cdf0e10cSrcweir     ///
46cdf0e10cSrcweir   	OTimerManager();
47cdf0e10cSrcweir 
48cdf0e10cSrcweir 	///
49cdf0e10cSrcweir   	~OTimerManager();
50cdf0e10cSrcweir 
51cdf0e10cSrcweir   	/// register timer
52cdf0e10cSrcweir     sal_Bool SAL_CALL registerTimer(vos::OTimer* pTimer);
53cdf0e10cSrcweir 
54cdf0e10cSrcweir   	/// unregister timer
55cdf0e10cSrcweir     sal_Bool SAL_CALL unregisterTimer(vos::OTimer* pTimer);
56cdf0e10cSrcweir 
57cdf0e10cSrcweir   	/// lookup timer
58cdf0e10cSrcweir     sal_Bool SAL_CALL lookupTimer(const vos::OTimer* pTimer);
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 	/// retrieves the "Singleton" TimerManager Instance
61cdf0e10cSrcweir 	static OTimerManager* SAL_CALL getTimerManager();
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 
64cdf0e10cSrcweir protected:
65cdf0e10cSrcweir 
66cdf0e10cSrcweir  	/// worker-function of thread
67cdf0e10cSrcweir   	virtual void SAL_CALL run();
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     // Checking and triggering of a timer event
70cdf0e10cSrcweir     void SAL_CALL checkForTimeout();
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     // cleanup Method
73cdf0e10cSrcweir     virtual void SAL_CALL onTerminated();
74cdf0e10cSrcweir 
75cdf0e10cSrcweir   	// sorted-queue data
76cdf0e10cSrcweir   	vos::OTimer*		m_pHead;
77cdf0e10cSrcweir     // List Protection
78cdf0e10cSrcweir     vos::OMutex		m_Lock;
79cdf0e10cSrcweir     // Signal the insertion of a timer
80cdf0e10cSrcweir     vos::OCondition	m_notEmpty;
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     // Synchronize access to OTimerManager
83cdf0e10cSrcweir 	static vos::OMutex m_Access;
84cdf0e10cSrcweir 
85cdf0e10cSrcweir     // "Singleton Pattern"
86cdf0e10cSrcweir     static vos::OTimerManager* m_pManager;
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     friend class OTimerManagerCleanup;
89cdf0e10cSrcweir 
90cdf0e10cSrcweir };
91cdf0e10cSrcweir 
92cdf0e10cSrcweir using namespace vos;
93cdf0e10cSrcweir 
94cdf0e10cSrcweir /////////////////////////////////////////////////////////////////////////////
95cdf0e10cSrcweir //
96cdf0e10cSrcweir // Timer class
97cdf0e10cSrcweir //
98cdf0e10cSrcweir 
99cdf0e10cSrcweir VOS_IMPLEMENT_CLASSINFO(VOS_CLASSNAME(OTimer, vos),
100cdf0e10cSrcweir                         VOS_NAMESPACE(OTimer, vos),
101cdf0e10cSrcweir                         VOS_NAMESPACE(OObject, vos), 0);
102cdf0e10cSrcweir 
OTimer()103cdf0e10cSrcweir OTimer::OTimer()
104cdf0e10cSrcweir {
105cdf0e10cSrcweir 	m_TimeOut	  = 0;
106cdf0e10cSrcweir 	m_Expired     = 0;
107cdf0e10cSrcweir 	m_RepeatDelta = 0;
108cdf0e10cSrcweir   	m_pNext       = 0;
109cdf0e10cSrcweir }
110cdf0e10cSrcweir 
OTimer(const TTimeValue & Time)111cdf0e10cSrcweir OTimer::OTimer(const TTimeValue& Time)
112cdf0e10cSrcweir {
113cdf0e10cSrcweir 	m_TimeOut	  = Time;
114cdf0e10cSrcweir 	m_RepeatDelta = 0;
115cdf0e10cSrcweir 	m_Expired     = 0;
116cdf0e10cSrcweir   	m_pNext       = 0;
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 	m_TimeOut.normalize();
119cdf0e10cSrcweir }
120cdf0e10cSrcweir 
OTimer(const TTimeValue & Time,const TTimeValue & Repeat)121cdf0e10cSrcweir OTimer::OTimer(const TTimeValue& Time, const TTimeValue& Repeat)
122cdf0e10cSrcweir {
123cdf0e10cSrcweir 	m_TimeOut	  = Time;
124cdf0e10cSrcweir 	m_RepeatDelta = Repeat;
125cdf0e10cSrcweir 	m_Expired     = 0;
126cdf0e10cSrcweir   	m_pNext       = 0;
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 	m_TimeOut.normalize();
129cdf0e10cSrcweir 	m_RepeatDelta.normalize();
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
~OTimer()132cdf0e10cSrcweir OTimer::~OTimer()
133cdf0e10cSrcweir {
134cdf0e10cSrcweir     stop();
135cdf0e10cSrcweir }
136cdf0e10cSrcweir 
start()137cdf0e10cSrcweir void OTimer::start()
138cdf0e10cSrcweir {
139cdf0e10cSrcweir 	if (! isTicking())
140cdf0e10cSrcweir 	{
141cdf0e10cSrcweir 		if (! m_TimeOut.isEmpty())
142cdf0e10cSrcweir 			setRemainingTime(m_TimeOut);
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 		OTimerManager *pManager = OTimerManager::getTimerManager();
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 		VOS_ASSERT(pManager);
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 		if ( pManager != 0 )
149cdf0e10cSrcweir 		{
150cdf0e10cSrcweir 			pManager->registerTimer(this);
151cdf0e10cSrcweir 		}
152cdf0e10cSrcweir 	}
153cdf0e10cSrcweir }
154cdf0e10cSrcweir 
stop()155cdf0e10cSrcweir void OTimer::stop()
156cdf0e10cSrcweir {
157cdf0e10cSrcweir 	OTimerManager *pManager = OTimerManager::getTimerManager();
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 	VOS_ASSERT(pManager);
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 	if ( pManager != 0 )
162cdf0e10cSrcweir 	{
163cdf0e10cSrcweir 		pManager->unregisterTimer(this);
164cdf0e10cSrcweir 	}
165cdf0e10cSrcweir }
166cdf0e10cSrcweir 
isTicking() const167cdf0e10cSrcweir sal_Bool OTimer::isTicking() const
168cdf0e10cSrcweir {
169cdf0e10cSrcweir 	OTimerManager *pManager = OTimerManager::getTimerManager();
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 	VOS_ASSERT(pManager);
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 	if (pManager)
174cdf0e10cSrcweir 		return pManager->lookupTimer(this);
175cdf0e10cSrcweir 	else
176cdf0e10cSrcweir 		return sal_False;
177cdf0e10cSrcweir 
178cdf0e10cSrcweir }
179cdf0e10cSrcweir 
isExpired() const180cdf0e10cSrcweir sal_Bool OTimer::isExpired() const
181cdf0e10cSrcweir {
182cdf0e10cSrcweir 	TTimeValue Now;
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 	osl_getSystemTime(&Now);
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 	return !(Now < m_Expired);
187cdf0e10cSrcweir }
188cdf0e10cSrcweir 
expiresBefore(const OTimer * pTimer) const189cdf0e10cSrcweir sal_Bool OTimer::expiresBefore(const OTimer* pTimer) const
190cdf0e10cSrcweir {
191cdf0e10cSrcweir 	VOS_ASSERT(pTimer);
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 	if ( pTimer != 0 )
194cdf0e10cSrcweir 	{
195cdf0e10cSrcweir 		return m_Expired < pTimer->m_Expired;
196cdf0e10cSrcweir 	}
197cdf0e10cSrcweir 	else
198cdf0e10cSrcweir 	{
199cdf0e10cSrcweir 		return sal_False;
200cdf0e10cSrcweir 	}
201cdf0e10cSrcweir }
202cdf0e10cSrcweir 
setAbsoluteTime(const TTimeValue & Time)203cdf0e10cSrcweir void OTimer::setAbsoluteTime(const TTimeValue& Time)
204cdf0e10cSrcweir {
205cdf0e10cSrcweir 	m_TimeOut     = 0;
206cdf0e10cSrcweir 	m_Expired     = Time;
207cdf0e10cSrcweir 	m_RepeatDelta = 0;
208cdf0e10cSrcweir 
209cdf0e10cSrcweir 	m_Expired.normalize();
210cdf0e10cSrcweir }
211cdf0e10cSrcweir 
setRemainingTime(const TTimeValue & Remaining)212cdf0e10cSrcweir void OTimer::setRemainingTime(const TTimeValue& Remaining)
213cdf0e10cSrcweir {
214cdf0e10cSrcweir 	osl_getSystemTime(&m_Expired);
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 	m_Expired.addTime(Remaining);
217cdf0e10cSrcweir }
218cdf0e10cSrcweir 
setRemainingTime(const TTimeValue & Remaining,const TTimeValue & Repeat)219cdf0e10cSrcweir void OTimer::setRemainingTime(const TTimeValue& Remaining, const TTimeValue& Repeat)
220cdf0e10cSrcweir {
221cdf0e10cSrcweir 	osl_getSystemTime(&m_Expired);
222cdf0e10cSrcweir 
223cdf0e10cSrcweir 	m_Expired.addTime(Remaining);
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 	m_RepeatDelta = Repeat;
226cdf0e10cSrcweir }
227cdf0e10cSrcweir 
addTime(const TTimeValue & Delta)228cdf0e10cSrcweir void OTimer::addTime(const TTimeValue& Delta)
229cdf0e10cSrcweir {
230cdf0e10cSrcweir 	m_Expired.addTime(Delta);
231cdf0e10cSrcweir }
232cdf0e10cSrcweir 
getRemainingTime() const233cdf0e10cSrcweir TTimeValue OTimer::getRemainingTime() const
234cdf0e10cSrcweir {
235cdf0e10cSrcweir 	TTimeValue Now;
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 	osl_getSystemTime(&Now);
238cdf0e10cSrcweir 
239cdf0e10cSrcweir 	sal_Int32 secs = m_Expired.Seconds - Now.Seconds;
240cdf0e10cSrcweir 
241cdf0e10cSrcweir 	if (secs < 0)
242cdf0e10cSrcweir 		return TTimeValue(0, 0);
243cdf0e10cSrcweir 
244cdf0e10cSrcweir 	sal_Int32 nsecs = m_Expired.Nanosec - Now.Nanosec;
245cdf0e10cSrcweir 
246cdf0e10cSrcweir 	if (nsecs < 0)
247cdf0e10cSrcweir 	{
248cdf0e10cSrcweir 		if (secs > 0)
249cdf0e10cSrcweir 		{
250cdf0e10cSrcweir 			secs  -= 1;
251cdf0e10cSrcweir 			nsecs += 1000000000;
252cdf0e10cSrcweir 		}
253cdf0e10cSrcweir 		else
254cdf0e10cSrcweir 			return TTimeValue(0, 0);
255cdf0e10cSrcweir 	}
256cdf0e10cSrcweir 
257cdf0e10cSrcweir 	return TTimeValue(secs, nsecs);
258cdf0e10cSrcweir }
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 
261cdf0e10cSrcweir /////////////////////////////////////////////////////////////////////////////
262cdf0e10cSrcweir //
263cdf0e10cSrcweir // Timer manager
264cdf0e10cSrcweir //
265cdf0e10cSrcweir 
266cdf0e10cSrcweir OMutex vos::OTimerManager::m_Access;
267cdf0e10cSrcweir OTimerManager* vos::OTimerManager::m_pManager=0;
268cdf0e10cSrcweir 
OTimerManager()269cdf0e10cSrcweir OTimerManager::OTimerManager()
270cdf0e10cSrcweir {
271cdf0e10cSrcweir 	OGuard Guard(&m_Access);
272cdf0e10cSrcweir 
273cdf0e10cSrcweir 	VOS_ASSERT(m_pManager == 0);
274cdf0e10cSrcweir 
275cdf0e10cSrcweir 	m_pManager = this;
276cdf0e10cSrcweir 
277cdf0e10cSrcweir 	m_pHead= 0;
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 	m_notEmpty.reset();
280cdf0e10cSrcweir 
281cdf0e10cSrcweir 	// start thread
282cdf0e10cSrcweir 	create();
283cdf0e10cSrcweir }
284cdf0e10cSrcweir 
~OTimerManager()285cdf0e10cSrcweir OTimerManager::~OTimerManager()
286cdf0e10cSrcweir {
287cdf0e10cSrcweir 	OGuard Guard(&m_Access);
288cdf0e10cSrcweir 
289cdf0e10cSrcweir 	if ( m_pManager == this )
290cdf0e10cSrcweir 		m_pManager = 0;
291cdf0e10cSrcweir }
292cdf0e10cSrcweir 
onTerminated()293cdf0e10cSrcweir void OTimerManager::onTerminated()
294cdf0e10cSrcweir {
295cdf0e10cSrcweir     delete this; // mfe: AAARRRGGGHHH!!!
296cdf0e10cSrcweir }
297cdf0e10cSrcweir 
getTimerManager()298cdf0e10cSrcweir OTimerManager* OTimerManager::getTimerManager()
299cdf0e10cSrcweir {
300cdf0e10cSrcweir 	OGuard Guard(&m_Access);
301cdf0e10cSrcweir 
302cdf0e10cSrcweir 	if (! m_pManager)
303cdf0e10cSrcweir 		new OTimerManager;
304cdf0e10cSrcweir 
305cdf0e10cSrcweir 	return (m_pManager);
306cdf0e10cSrcweir }
307cdf0e10cSrcweir 
registerTimer(OTimer * pTimer)308cdf0e10cSrcweir sal_Bool OTimerManager::registerTimer(OTimer* pTimer)
309cdf0e10cSrcweir {
310cdf0e10cSrcweir 	VOS_ASSERT(pTimer);
311cdf0e10cSrcweir 
312cdf0e10cSrcweir 	if ( pTimer == 0 )
313cdf0e10cSrcweir 	{
314cdf0e10cSrcweir 		return sal_False;
315cdf0e10cSrcweir 	}
316cdf0e10cSrcweir 
317cdf0e10cSrcweir 	OGuard Guard(&m_Lock);
318cdf0e10cSrcweir 
319cdf0e10cSrcweir 	// try to find one with equal or lower remaining time.
320cdf0e10cSrcweir 	OTimer** ppIter = &m_pHead;
321cdf0e10cSrcweir 
322cdf0e10cSrcweir 	while (*ppIter)
323cdf0e10cSrcweir 	{
324cdf0e10cSrcweir 		if (pTimer->expiresBefore(*ppIter))
325cdf0e10cSrcweir 		{
326cdf0e10cSrcweir 			// next element has higher remaining time,
327cdf0e10cSrcweir 			// => insert new timer before
328cdf0e10cSrcweir 			break;
329cdf0e10cSrcweir 		}
330cdf0e10cSrcweir 		ppIter= &((*ppIter)->m_pNext);
331cdf0e10cSrcweir 	}
332cdf0e10cSrcweir 
333cdf0e10cSrcweir 	// next element has higher remaining time,
334cdf0e10cSrcweir 	// => insert new timer before
335cdf0e10cSrcweir 	pTimer->m_pNext= *ppIter;
336cdf0e10cSrcweir 	*ppIter = pTimer;
337cdf0e10cSrcweir 
338cdf0e10cSrcweir 
339cdf0e10cSrcweir 	if (pTimer == m_pHead)
340cdf0e10cSrcweir 	{
341cdf0e10cSrcweir 		// it was inserted as new head
342cdf0e10cSrcweir 		// signal it to TimerManager Thread
343cdf0e10cSrcweir         m_notEmpty.set();
344cdf0e10cSrcweir 	}
345cdf0e10cSrcweir 
346cdf0e10cSrcweir 	return sal_True;
347cdf0e10cSrcweir }
348cdf0e10cSrcweir 
unregisterTimer(OTimer * pTimer)349cdf0e10cSrcweir sal_Bool OTimerManager::unregisterTimer(OTimer* pTimer)
350cdf0e10cSrcweir {
351cdf0e10cSrcweir 	VOS_ASSERT(pTimer);
352cdf0e10cSrcweir 
353cdf0e10cSrcweir 	if ( pTimer == 0 )
354cdf0e10cSrcweir 	{
355cdf0e10cSrcweir 		return sal_False;
356cdf0e10cSrcweir 	}
357cdf0e10cSrcweir 
358cdf0e10cSrcweir 	// lock access
359cdf0e10cSrcweir 	OGuard Guard(&m_Lock);
360cdf0e10cSrcweir 
361cdf0e10cSrcweir 	OTimer** ppIter = &m_pHead;
362cdf0e10cSrcweir 
363cdf0e10cSrcweir 	while (*ppIter)
364cdf0e10cSrcweir 	{
365cdf0e10cSrcweir 		if (pTimer == (*ppIter))
366cdf0e10cSrcweir 		{
367cdf0e10cSrcweir 			// remove timer from list
368cdf0e10cSrcweir 			*ppIter = (*ppIter)->m_pNext;
369cdf0e10cSrcweir 			return sal_True;
370cdf0e10cSrcweir 		}
371cdf0e10cSrcweir 		ppIter= &((*ppIter)->m_pNext);
372cdf0e10cSrcweir 	}
373cdf0e10cSrcweir 
374cdf0e10cSrcweir 	return sal_False;
375cdf0e10cSrcweir }
376cdf0e10cSrcweir 
lookupTimer(const OTimer * pTimer)377cdf0e10cSrcweir sal_Bool OTimerManager::lookupTimer(const OTimer* pTimer)
378cdf0e10cSrcweir {
379cdf0e10cSrcweir 	VOS_ASSERT(pTimer);
380cdf0e10cSrcweir 
381cdf0e10cSrcweir 	if ( pTimer == 0 )
382cdf0e10cSrcweir 	{
383cdf0e10cSrcweir 		return sal_False;
384cdf0e10cSrcweir 	}
385cdf0e10cSrcweir 
386cdf0e10cSrcweir 	// lock access
387cdf0e10cSrcweir 	OGuard Guard(&m_Lock);
388cdf0e10cSrcweir 
389cdf0e10cSrcweir 	// check the list
390cdf0e10cSrcweir 	for (OTimer* pIter = m_pHead; pIter != 0; pIter= pIter->m_pNext)
391cdf0e10cSrcweir 	{
392cdf0e10cSrcweir 		if (pIter == pTimer)
393cdf0e10cSrcweir         {
394cdf0e10cSrcweir 			return sal_True;
395cdf0e10cSrcweir         }
396cdf0e10cSrcweir 	}
397cdf0e10cSrcweir 
398cdf0e10cSrcweir 	return sal_False;
399cdf0e10cSrcweir }
400cdf0e10cSrcweir 
checkForTimeout()401cdf0e10cSrcweir void OTimerManager::checkForTimeout()
402cdf0e10cSrcweir {
403cdf0e10cSrcweir 
404cdf0e10cSrcweir     m_Lock.acquire();
405cdf0e10cSrcweir 
406cdf0e10cSrcweir 	if ( m_pHead == 0 )
407cdf0e10cSrcweir 	{
408cdf0e10cSrcweir         m_Lock.release();
409cdf0e10cSrcweir 		return;
410cdf0e10cSrcweir 	}
411cdf0e10cSrcweir 
412cdf0e10cSrcweir 	OTimer* pTimer = m_pHead;
413cdf0e10cSrcweir 
414cdf0e10cSrcweir 	if (pTimer->isExpired())
415cdf0e10cSrcweir 	{
416cdf0e10cSrcweir 		// remove expired timer
417cdf0e10cSrcweir 		m_pHead = pTimer->m_pNext;
418cdf0e10cSrcweir 
419cdf0e10cSrcweir         pTimer->acquire();
420cdf0e10cSrcweir 
421cdf0e10cSrcweir 		m_Lock.release();
422cdf0e10cSrcweir 
423cdf0e10cSrcweir 		pTimer->onShot();
424cdf0e10cSrcweir 
425cdf0e10cSrcweir 		// restart timer if specified
426cdf0e10cSrcweir 		if ( ! pTimer->m_RepeatDelta.isEmpty() )
427cdf0e10cSrcweir 		{
428cdf0e10cSrcweir 			TTimeValue Now;
429cdf0e10cSrcweir 
430cdf0e10cSrcweir 			osl_getSystemTime(&Now);
431cdf0e10cSrcweir 
432cdf0e10cSrcweir 			Now.Seconds += pTimer->m_RepeatDelta.Seconds;
433cdf0e10cSrcweir 			Now.Nanosec += pTimer->m_RepeatDelta.Nanosec;
434cdf0e10cSrcweir 
435cdf0e10cSrcweir 			pTimer->m_Expired = Now;
436cdf0e10cSrcweir 
437cdf0e10cSrcweir 			registerTimer(pTimer);
438cdf0e10cSrcweir 		}
439cdf0e10cSrcweir         pTimer->release();
440cdf0e10cSrcweir 	}
441cdf0e10cSrcweir 	else
442cdf0e10cSrcweir 	{
443cdf0e10cSrcweir 		m_Lock.release();
444cdf0e10cSrcweir 	}
445cdf0e10cSrcweir 
446cdf0e10cSrcweir 
447cdf0e10cSrcweir 	return;
448cdf0e10cSrcweir }
449cdf0e10cSrcweir 
run()450cdf0e10cSrcweir void OTimerManager::run()
451cdf0e10cSrcweir {
452cdf0e10cSrcweir 	setPriority(TPriority_BelowNormal);
453cdf0e10cSrcweir 
454cdf0e10cSrcweir 	while (schedule())
455cdf0e10cSrcweir 	{
456cdf0e10cSrcweir 		TTimeValue		delay;
457cdf0e10cSrcweir 		TTimeValue*		pDelay=0;
458cdf0e10cSrcweir 
459cdf0e10cSrcweir 
460cdf0e10cSrcweir 		m_Lock.acquire();
461cdf0e10cSrcweir 
462cdf0e10cSrcweir 		if (m_pHead != 0)
463cdf0e10cSrcweir         {
464cdf0e10cSrcweir 			delay = m_pHead->getRemainingTime();
465cdf0e10cSrcweir             pDelay=&delay;
466cdf0e10cSrcweir         }
467cdf0e10cSrcweir         else
468cdf0e10cSrcweir         {
469cdf0e10cSrcweir             pDelay=0;
470cdf0e10cSrcweir         }
471cdf0e10cSrcweir 
472cdf0e10cSrcweir 
473cdf0e10cSrcweir         m_notEmpty.reset();
474cdf0e10cSrcweir 
475cdf0e10cSrcweir         m_Lock.release();
476cdf0e10cSrcweir 
477cdf0e10cSrcweir 
478cdf0e10cSrcweir         m_notEmpty.wait(pDelay);
479cdf0e10cSrcweir 
480cdf0e10cSrcweir         checkForTimeout();
481cdf0e10cSrcweir   	}
482cdf0e10cSrcweir 
483cdf0e10cSrcweir }
484cdf0e10cSrcweir 
485cdf0e10cSrcweir 
486cdf0e10cSrcweir /////////////////////////////////////////////////////////////////////////////
487cdf0e10cSrcweir //
488cdf0e10cSrcweir // Timer manager cleanup
489cdf0e10cSrcweir //
490cdf0e10cSrcweir 
491cdf0e10cSrcweir // jbu:
492cdf0e10cSrcweir // The timer manager cleanup has been removed (no thread is killed anymore).
493cdf0e10cSrcweir // So the thread leaks.
494cdf0e10cSrcweir // This will result in a GPF in case the vos-library gets unloaded before
495cdf0e10cSrcweir // process termination.
496cdf0e10cSrcweir // -> TODO : rewrite this file, so that the timerManager thread gets destroyed,
497cdf0e10cSrcweir //           when there are no timers anymore !
498