1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTimer implementation
4 // Author: Vaclav Slavik
6 // Copyright: (c) Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "timer.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
21 // ----------------------------------------------------------------------------
22 // NB: when using generic wxTimer implementation in your port, you *must* call
23 // wxTimer::NotifyTimers() often enough. The ideal place for this
24 // is in wxEventLoop::Dispatch().
25 // ----------------------------------------------------------------------------
32 #include "wx/module.h"
34 // ----------------------------------------------------------------------------
35 // Time input function
36 // ----------------------------------------------------------------------------
39 // We take advantage of wxMGL's _EVT_getTicks because it is faster
40 // (especially under MS-DOS!) and more precise than wxGetLocalTimeMillis
41 // if we are unlucky and the latter combines information from two sources.
42 #include "wx/mgl/private.h"
43 extern "C" ulong
_EVT_getTicks();
44 #define GetMillisecondsTime _EVT_getTicks
46 typedef ulong wxTimerTick_t
;
48 #define wxTimerTickFmtSpec _T("lu")
49 #define wxTimerTickPrintfArg(tt) (tt)
52 // Under DOS the MGL timer has a 24hr period, so consider the 12 hours
53 // before y to be 'less' and the the 12 hours after 'greater' modulo
55 inline bool wxTickGreaterEqual(wxTimerTick_t x
, wxTimerTick_t y
)
57 // _EVT_getTicks wraps at 1573040 * 55
58 const wxTimerTick_t modulus
= 1573040 * 55;
59 return (2 * modulus
+ x
- y
) % modulus
< modulus
/ 2;
62 // If wxTimerTick_t is 32-bits then it'll wrap in around 50 days. So
63 // let the 25 days before y be 'less' and 25 days after be 'greater'.
64 inline bool wxTickGreaterEqual(wxTimerTick_t x
, wxTimerTick_t y
)
66 // This code assumes wxTimerTick_t is an unsigned type.
67 // Set half_modulus with top bit set and the rest zeros.
68 const wxTimerTick_t half_modulus
= ~((~(wxTimerTick_t
)0) >> 1);
69 return x
- y
< half_modulus
;
73 #define GetMillisecondsTime wxGetLocalTimeMillis
75 typedef wxLongLong wxTimerTick_t
;
78 #define wxTimerTickFmtSpec wxLongLongFmtSpec _T("d")
79 #define wxTimerTickPrintfArg(tt) (tt.GetValue())
80 #else // using native wxLongLong
81 #define wxTimerTickFmtSpec _T("s")
82 #define wxTimerTickPrintfArg(tt) (tt.ToString().c_str())
83 #endif // wx/native long long
85 inline bool wxTickGreaterEqual(wxTimerTick_t x
, wxTimerTick_t y
)
89 #endif // __WXMGL__/!__WXMGL__
91 // ----------------------------------------------------------------------------
92 // helper structures and wxTimerScheduler
93 // ----------------------------------------------------------------------------
98 wxTimerDesc(wxTimer
*t
) :
99 timer(t
), running(false), next(NULL
), prev(NULL
),
100 shotTime(0), deleteFlag(NULL
) {}
104 wxTimerDesc
*next
, *prev
;
105 wxTimerTick_t shotTime
;
106 volatile bool *deleteFlag
; // see comment in ~wxTimer
109 class wxTimerScheduler
112 wxTimerScheduler() : m_timers(NULL
) {}
114 void QueueTimer(wxTimerDesc
*desc
, wxTimerTick_t when
= 0);
115 void RemoveTimer(wxTimerDesc
*desc
);
119 wxTimerDesc
*m_timers
;
122 void wxTimerScheduler::QueueTimer(wxTimerDesc
*desc
, wxTimerTick_t when
)
125 return; // already scheduled
128 when
= GetMillisecondsTime() + desc
->timer
->GetInterval();
129 desc
->shotTime
= when
;
130 desc
->running
= true;
132 wxLogTrace( wxT("timer"),
133 wxT("queued timer %p at tick %") wxTimerTickFmtSpec
,
134 desc
->timer
, wxTimerTickPrintfArg(when
));
138 wxTimerDesc
*d
= m_timers
;
139 while ( d
->next
&& d
->next
->shotTime
< when
) d
= d
->next
;
140 desc
->next
= d
->next
;
143 d
->next
->prev
= desc
;
149 desc
->prev
= desc
->next
= NULL
;
153 void wxTimerScheduler::RemoveTimer(wxTimerDesc
*desc
)
155 desc
->running
= false;
156 if ( desc
== m_timers
)
157 m_timers
= desc
->next
;
159 desc
->prev
->next
= desc
->next
;
161 desc
->next
->prev
= desc
->prev
;
162 desc
->prev
= desc
->next
= NULL
;
165 void wxTimerScheduler::NotifyTimers()
170 volatile bool timerDeleted
;
171 wxTimerTick_t now
= GetMillisecondsTime();
173 for ( wxTimerDesc
*desc
= m_timers
; desc
; desc
= desc
->next
)
175 if ( desc
->running
&& wxTickGreaterEqual(now
, desc
->shotTime
) )
177 oneShot
= desc
->timer
->IsOneShot();
180 timerDeleted
= false;
181 desc
->deleteFlag
= &timerDeleted
;
182 desc
->timer
->Notify();
186 wxLogTrace( wxT("timer"),
187 wxT("notified timer %p sheduled for %")
190 wxTimerTickPrintfArg(desc
->shotTime
) );
192 desc
->deleteFlag
= NULL
;
194 QueueTimer(desc
, now
+ desc
->timer
->GetInterval());
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
208 IMPLEMENT_ABSTRACT_CLASS(wxTimer
, wxEvtHandler
)
210 wxTimerScheduler
*gs_scheduler
= NULL
;
215 gs_scheduler
= new wxTimerScheduler
;
216 m_desc
= new wxTimerDesc(this);
221 wxLogTrace( wxT("timer"), wxT("destroying timer %p..."), this);
225 // NB: this is a hack: wxTimerScheduler must have some way of knowing
226 // that wxTimer object was deleted under its hands -- this may
227 // happen if somebody is really nasty and deletes the timer
228 // from wxTimer::Notify()
229 if ( m_desc
->deleteFlag
!= NULL
)
230 *m_desc
->deleteFlag
= true;
233 wxLogTrace( wxT("timer"), wxT(" ...done destroying timer %p..."), this);
236 bool wxTimer::IsRunning() const
238 return m_desc
->running
;
241 bool wxTimer::Start(int millisecs
, bool oneShot
)
243 wxLogTrace( wxT("timer"), wxT("started timer %p: %i ms, oneshot=%i"),
244 this, millisecs
, oneShot
);
246 if ( !wxTimerBase::Start(millisecs
, oneShot
) )
249 gs_scheduler
->QueueTimer(m_desc
);
255 if ( !m_desc
->running
) return;
257 gs_scheduler
->RemoveTimer(m_desc
);
260 /*static*/ void wxTimer::NotifyTimers()
263 gs_scheduler
->NotifyTimers();
268 // A module to deallocate memory properly:
269 class wxTimerModule
: public wxModule
271 DECLARE_DYNAMIC_CLASS(wxTimerModule
)
274 bool OnInit() { return true; }
275 void OnExit() { delete gs_scheduler
; gs_scheduler
= NULL
; }
278 IMPLEMENT_DYNAMIC_CLASS(wxTimerModule
, wxModule
)