Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : : #include <salhelper/timer.hxx>
20 : :
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>
27 : :
28 : : using namespace salhelper;
29 : :
30 : : class salhelper::TimerManager : public osl::Thread
31 : : {
32 : :
33 : : public:
34 : :
35 : : ///
36 : : TimerManager();
37 : :
38 : : ///
39 : : ~TimerManager();
40 : :
41 : : /// register timer
42 : : sal_Bool SAL_CALL registerTimer(salhelper::Timer* pTimer);
43 : :
44 : : /// unregister timer
45 : : sal_Bool SAL_CALL unregisterTimer(salhelper::Timer* pTimer);
46 : :
47 : : /// lookup timer
48 : : sal_Bool SAL_CALL lookupTimer(const salhelper::Timer* pTimer);
49 : :
50 : : /// retrieves the "Singleton" TimerManager Instance
51 : : static TimerManager* SAL_CALL getTimerManager();
52 : :
53 : :
54 : : protected:
55 : :
56 : : /// worker-function of thread
57 : : virtual void SAL_CALL run();
58 : :
59 : : // Checking and triggering of a timer event
60 : : void SAL_CALL checkForTimeout();
61 : :
62 : : // cleanup Method
63 : : virtual void SAL_CALL onTerminated();
64 : :
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;
71 : :
72 : : // "Singleton Pattern"
73 : : static salhelper::TimerManager* m_pManager;
74 : :
75 : : };
76 : :
77 : : using namespace salhelper;
78 : :
79 : : /////////////////////////////////////////////////////////////////////////////
80 : : //
81 : : // Timer class
82 : : //
83 : :
84 : 0 : Timer::Timer()
85 : : : m_aTimeOut( 0 ),
86 : : m_aExpired( 0 ),
87 : : m_aRepeatDelta( 0 ),
88 [ # # ][ # # ]: 0 : m_pNext( NULL )
[ # # ]
89 : : {
90 : 0 : }
91 : :
92 : 0 : Timer::Timer( const TTimeValue& Time )
93 : : : m_aTimeOut( Time ),
94 : : m_aExpired( 0 ),
95 : : m_aRepeatDelta( 0 ),
96 [ # # ][ # # ]: 0 : m_pNext( NULL )
[ # # ]
97 : : {
98 : 0 : }
99 : :
100 : 0 : Timer::Timer( const TTimeValue& Time, const TTimeValue& Repeat )
101 : : : m_aTimeOut( Time ),
102 : : m_aExpired( 0 ),
103 : : m_aRepeatDelta( Repeat ),
104 [ # # ][ # # ]: 0 : m_pNext( NULL )
[ # # ]
105 : : {
106 : 0 : }
107 : :
108 : 0 : Timer::~Timer()
109 : : {
110 [ # # ]: 0 : stop();
111 [ # # ]: 0 : }
112 : :
113 : 0 : void Timer::start()
114 : : {
115 [ # # ]: 0 : if (! isTicking())
116 : : {
117 [ # # ]: 0 : if (! m_aTimeOut.isEmpty())
118 : 0 : setRemainingTime(m_aTimeOut);
119 : :
120 : 0 : TimerManager *pManager = TimerManager::getTimerManager();
121 : :
122 : : OSL_ASSERT(pManager);
123 : :
124 [ # # ]: 0 : if ( pManager != 0 )
125 : : {
126 : 0 : pManager->registerTimer(this);
127 : : }
128 : : }
129 : 0 : }
130 : :
131 : 0 : void Timer::stop()
132 : : {
133 : 0 : TimerManager *pManager = TimerManager::getTimerManager();
134 : :
135 : : OSL_ASSERT(pManager);
136 : :
137 [ # # ]: 0 : if ( pManager != 0 )
138 : : {
139 : 0 : pManager->unregisterTimer(this);
140 : : }
141 : 0 : }
142 : :
143 : 0 : sal_Bool Timer::isTicking() const
144 : : {
145 : 0 : TimerManager *pManager = TimerManager::getTimerManager();
146 : :
147 : : OSL_ASSERT(pManager);
148 : :
149 [ # # ]: 0 : if (pManager)
150 : 0 : return pManager->lookupTimer(this);
151 : : else
152 : 0 : return sal_False;
153 : :
154 : : }
155 : :
156 : 0 : sal_Bool Timer::isExpired() const
157 : : {
158 : 0 : TTimeValue Now;
159 : :
160 [ # # ]: 0 : osl_getSystemTime(&Now);
161 : :
162 : 0 : return !(Now < m_aExpired);
163 : : }
164 : :
165 : 0 : sal_Bool Timer::expiresBefore(const Timer* pTimer) const
166 : : {
167 : : OSL_ASSERT(pTimer);
168 : :
169 [ # # ]: 0 : if ( pTimer != 0 )
170 : : {
171 : 0 : return m_aExpired < pTimer->m_aExpired;
172 : : }
173 : : else
174 : : {
175 : 0 : return sal_False;
176 : : }
177 : : }
178 : :
179 : 0 : void Timer::setAbsoluteTime(const TTimeValue& Time)
180 : : {
181 : 0 : m_aTimeOut = 0;
182 : 0 : m_aExpired = Time;
183 : 0 : m_aRepeatDelta = 0;
184 : :
185 : 0 : m_aExpired.normalize();
186 : 0 : }
187 : :
188 : 0 : void Timer::setRemainingTime(const TTimeValue& Remaining)
189 : : {
190 : 0 : osl_getSystemTime(&m_aExpired);
191 : :
192 : 0 : m_aExpired.addTime(Remaining);
193 : 0 : }
194 : :
195 : 0 : void Timer::setRemainingTime(const TTimeValue& Remaining, const TTimeValue& Repeat)
196 : : {
197 : 0 : osl_getSystemTime(&m_aExpired);
198 : :
199 : 0 : m_aExpired.addTime(Remaining);
200 : :
201 : 0 : m_aRepeatDelta = Repeat;
202 : 0 : }
203 : :
204 : 0 : void Timer::addTime(const TTimeValue& Delta)
205 : : {
206 : 0 : m_aExpired.addTime(Delta);
207 : 0 : }
208 : :
209 : 0 : TTimeValue Timer::getRemainingTime() const
210 : : {
211 : 0 : TTimeValue Now;
212 : :
213 [ # # ]: 0 : osl_getSystemTime(&Now);
214 : :
215 : 0 : sal_Int32 secs = m_aExpired.Seconds - Now.Seconds;
216 : :
217 [ # # ]: 0 : if (secs < 0)
218 [ # # ]: 0 : return TTimeValue(0, 0);
219 : :
220 : 0 : sal_Int32 nsecs = m_aExpired.Nanosec - Now.Nanosec;
221 : :
222 [ # # ]: 0 : if (nsecs < 0)
223 : : {
224 [ # # ]: 0 : if (secs > 0)
225 : : {
226 : 0 : secs -= 1;
227 : 0 : nsecs += 1000000000L;
228 : : }
229 : : else
230 [ # # ]: 0 : return TTimeValue(0, 0);
231 : : }
232 : :
233 [ # # ]: 0 : return TTimeValue(secs, nsecs);
234 : : }
235 : :
236 : :
237 : : /////////////////////////////////////////////////////////////////////////////
238 : : //
239 : : // Timer manager
240 : : //
241 : : namespace
242 : : {
243 : : // Synchronize access to TimerManager
244 : : struct theTimerManagerMutex : public rtl::Static< osl::Mutex, theTimerManagerMutex> {};
245 : : }
246 : :
247 : : TimerManager* salhelper::TimerManager::m_pManager = NULL;
248 : :
249 [ # # ][ # # ]: 0 : TimerManager::TimerManager()
250 : : {
251 [ # # ][ # # ]: 0 : osl::MutexGuard Guard(theTimerManagerMutex::get());
252 : :
253 : : OSL_ASSERT(m_pManager == 0);
254 : :
255 : 0 : m_pManager = this;
256 : :
257 : 0 : m_pHead= 0;
258 : :
259 [ # # ]: 0 : m_notEmpty.reset();
260 : :
261 : : // start thread
262 [ # # ][ # # ]: 0 : create();
263 : 0 : }
264 : :
265 [ # # ][ # # ]: 0 : TimerManager::~TimerManager()
266 : : {
267 [ # # ][ # # ]: 0 : osl::MutexGuard Guard(theTimerManagerMutex::get());
268 : :
269 [ # # ]: 0 : if ( m_pManager == this )
270 [ # # ]: 0 : m_pManager = 0;
271 [ # # ]: 0 : }
272 : :
273 : 0 : void TimerManager::onTerminated()
274 : : {
275 [ # # ]: 0 : delete this; // mfe: AAARRRGGGHHH!!!
276 : 0 : }
277 : :
278 : 0 : TimerManager* TimerManager::getTimerManager()
279 : : {
280 [ # # ][ # # ]: 0 : osl::MutexGuard Guard(theTimerManagerMutex::get());
281 : :
282 [ # # ]: 0 : if (! m_pManager)
283 [ # # ]: 0 : new TimerManager;
284 : :
285 [ # # ]: 0 : return m_pManager;
286 : : }
287 : :
288 : 0 : sal_Bool TimerManager::registerTimer(Timer* pTimer)
289 : : {
290 : : OSL_ASSERT(pTimer);
291 : :
292 [ # # ]: 0 : if ( pTimer == 0 )
293 : : {
294 : 0 : return sal_False;
295 : : }
296 : :
297 [ # # ]: 0 : osl::MutexGuard Guard(m_Lock);
298 : :
299 : : // try to find one with equal or lower remaining time.
300 : 0 : Timer** ppIter = &m_pHead;
301 : :
302 [ # # ]: 0 : while (*ppIter)
303 : : {
304 [ # # ][ # # ]: 0 : if (pTimer->expiresBefore(*ppIter))
305 : : {
306 : : // next element has higher remaining time,
307 : : // => insert new timer before
308 : 0 : break;
309 : : }
310 : 0 : ppIter= &((*ppIter)->m_pNext);
311 : : }
312 : :
313 : : // next element has higher remaining time,
314 : : // => insert new timer before
315 : 0 : pTimer->m_pNext= *ppIter;
316 : 0 : *ppIter = pTimer;
317 : :
318 : :
319 [ # # ]: 0 : if (pTimer == m_pHead)
320 : : {
321 : : // it was inserted as new head
322 : : // signal it to TimerManager Thread
323 [ # # ]: 0 : m_notEmpty.set();
324 : : }
325 : :
326 [ # # ]: 0 : return sal_True;
327 : : }
328 : :
329 : 0 : sal_Bool TimerManager::unregisterTimer(Timer* pTimer)
330 : : {
331 : : OSL_ASSERT(pTimer);
332 : :
333 [ # # ]: 0 : if ( pTimer == 0 )
334 : : {
335 : 0 : return sal_False;
336 : : }
337 : :
338 : : // lock access
339 [ # # ]: 0 : osl::MutexGuard Guard(m_Lock);
340 : :
341 : 0 : Timer** ppIter = &m_pHead;
342 : :
343 [ # # ]: 0 : while (*ppIter)
344 : : {
345 [ # # ]: 0 : if (pTimer == (*ppIter))
346 : : {
347 : : // remove timer from list
348 : 0 : *ppIter = (*ppIter)->m_pNext;
349 : 0 : return sal_True;
350 : : }
351 : 0 : ppIter= &((*ppIter)->m_pNext);
352 : : }
353 : :
354 [ # # ]: 0 : return sal_False;
355 : : }
356 : :
357 : 0 : sal_Bool TimerManager::lookupTimer(const Timer* pTimer)
358 : : {
359 : : OSL_ASSERT(pTimer);
360 : :
361 [ # # ]: 0 : if ( pTimer == 0 )
362 : : {
363 : 0 : return sal_False;
364 : : }
365 : :
366 : : // lock access
367 [ # # ]: 0 : osl::MutexGuard Guard(m_Lock);
368 : :
369 : : // check the list
370 [ # # ]: 0 : for (Timer* pIter = m_pHead; pIter != 0; pIter= pIter->m_pNext)
371 : : {
372 [ # # ]: 0 : if (pIter == pTimer)
373 : : {
374 : 0 : return sal_True;
375 : : }
376 : : }
377 : :
378 [ # # ]: 0 : return sal_False;
379 : : }
380 : :
381 : 0 : void TimerManager::checkForTimeout()
382 : : {
383 : :
384 : 0 : m_Lock.acquire();
385 : :
386 [ # # ]: 0 : if ( m_pHead == 0 )
387 : : {
388 : 0 : m_Lock.release();
389 : 0 : return;
390 : : }
391 : :
392 : 0 : Timer* pTimer = m_pHead;
393 : :
394 [ # # ]: 0 : if (pTimer->isExpired())
395 : : {
396 : : // remove expired timer
397 : 0 : m_pHead = pTimer->m_pNext;
398 : :
399 : 0 : pTimer->acquire();
400 : :
401 : 0 : m_Lock.release();
402 : :
403 : 0 : pTimer->onShot();
404 : :
405 : : // restart timer if specified
406 [ # # ]: 0 : if ( ! pTimer->m_aRepeatDelta.isEmpty() )
407 : : {
408 : 0 : TTimeValue Now;
409 : :
410 [ # # ]: 0 : osl_getSystemTime(&Now);
411 : :
412 : 0 : Now.Seconds += pTimer->m_aRepeatDelta.Seconds;
413 : 0 : Now.Nanosec += pTimer->m_aRepeatDelta.Nanosec;
414 : :
415 : 0 : pTimer->m_aExpired = Now;
416 : :
417 [ # # ]: 0 : registerTimer(pTimer);
418 : : }
419 : 0 : pTimer->release();
420 : : }
421 : : else
422 : : {
423 : 0 : m_Lock.release();
424 : : }
425 : :
426 : :
427 : 0 : return;
428 : : }
429 : :
430 : 0 : void TimerManager::run()
431 : : {
432 : 0 : setPriority( osl_Thread_PriorityBelowNormal );
433 : :
434 [ # # ]: 0 : while (schedule())
435 : : {
436 : 0 : TTimeValue delay;
437 : 0 : TTimeValue* pDelay=0;
438 : :
439 : :
440 [ # # ]: 0 : m_Lock.acquire();
441 : :
442 [ # # ]: 0 : if (m_pHead != 0)
443 : : {
444 [ # # ]: 0 : delay = m_pHead->getRemainingTime();
445 : 0 : pDelay=&delay;
446 : : }
447 : : else
448 : : {
449 : 0 : pDelay=0;
450 : : }
451 : :
452 : :
453 [ # # ]: 0 : m_notEmpty.reset();
454 : :
455 [ # # ]: 0 : m_Lock.release();
456 : :
457 : :
458 [ # # ]: 0 : m_notEmpty.wait(pDelay);
459 : :
460 [ # # ]: 0 : checkForTimeout();
461 : : }
462 : :
463 : 0 : }
464 : :
465 : :
466 : : /////////////////////////////////////////////////////////////////////////////
467 : : //
468 : : // Timer manager cleanup
469 : : //
470 : :
471 : : // jbu:
472 : : // The timer manager cleanup has been removed (no thread is killed anymore).
473 : : // So the thread leaks.
474 : : // This will result in a GPF in case the salhelper-library gets unloaded before
475 : : // process termination.
476 : : // -> TODO : rewrite this file, so that the timerManager thread gets destroyed,
477 : : // when there are no timers anymore !
478 : :
479 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|