1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTimer implementation
4 // Author: Vaclav Slavik
6 // Copyright: (c) Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
17 // ----------------------------------------------------------------------------
18 // NB: when using generic wxTimer implementation in your port, you *must* call
19 // wxTimer::NotifyTimers() often enough. The ideal place for this
20 // is in wxEventLoop::Dispatch().
21 // ----------------------------------------------------------------------------
28 #include "wx/module.h"
30 // ----------------------------------------------------------------------------
31 // Time input function
32 // ----------------------------------------------------------------------------
35 // We take advantage of wxMGL's _EVT_getTicks because it is faster
36 // (especially under MS-DOS!) and more precise than wxGetLocalTimeMillis
37 // if we are unlucky and the latter combines information from two sources.
38 #include "wx/mgl/private.h"
39 extern "C" ulong
_EVT_getTicks();
40 #define GetMillisecondsTime _EVT_getTicks
42 typedef ulong wxTimerTick_t
;
44 #define wxTimerTickFmtSpec _T("lu")
45 #define wxTimerTickPrintfArg(tt) (tt)
48 // Under DOS the MGL timer has a 24hr period, so consider the 12 hours
49 // before y to be 'less' and the the 12 hours after 'greater' modulo
51 inline bool wxTickGreaterEqual(wxTimerTick_t x
, wxTimerTick_t y
)
53 // _EVT_getTicks wraps at 1573040 * 55
54 const wxTimerTick_t modulus
= 1573040 * 55;
55 return (2 * modulus
+ x
- y
) % modulus
< modulus
/ 2;
58 // If wxTimerTick_t is 32-bits then it'll wrap in around 50 days. So
59 // let the 25 days before y be 'less' and 25 days after be 'greater'.
60 inline bool wxTickGreaterEqual(wxTimerTick_t x
, wxTimerTick_t y
)
62 // This code assumes wxTimerTick_t is an unsigned type.
63 // Set half_modulus with top bit set and the rest zeros.
64 const wxTimerTick_t half_modulus
= ~((~(wxTimerTick_t
)0) >> 1);
65 return x
- y
< half_modulus
;
69 #define GetMillisecondsTime wxGetLocalTimeMillis
71 typedef wxLongLong wxTimerTick_t
;
74 #define wxTimerTickFmtSpec wxLongLongFmtSpec _T("d")
75 #define wxTimerTickPrintfArg(tt) (tt.GetValue())
76 #else // using native wxLongLong
77 #define wxTimerTickFmtSpec _T("s")
78 #define wxTimerTickPrintfArg(tt) (tt.ToString().c_str())
79 #endif // wx/native long long
81 inline bool wxTickGreaterEqual(wxTimerTick_t x
, wxTimerTick_t y
)
85 #endif // __WXMGL__/!__WXMGL__
87 // ----------------------------------------------------------------------------
88 // helper structures and wxTimerScheduler
89 // ----------------------------------------------------------------------------
94 wxTimerDesc(wxTimer
*t
) :
95 timer(t
), running(false), next(NULL
), prev(NULL
),
96 shotTime(0), deleteFlag(NULL
) {}
100 wxTimerDesc
*next
, *prev
;
101 wxTimerTick_t shotTime
;
102 volatile bool *deleteFlag
; // see comment in ~wxTimer
105 class wxTimerScheduler
108 wxTimerScheduler() : m_timers(NULL
) {}
110 void QueueTimer(wxTimerDesc
*desc
, wxTimerTick_t when
= 0);
111 void RemoveTimer(wxTimerDesc
*desc
);
115 wxTimerDesc
*m_timers
;
118 void wxTimerScheduler::QueueTimer(wxTimerDesc
*desc
, wxTimerTick_t when
)
121 return; // already scheduled
124 when
= GetMillisecondsTime() + desc
->timer
->GetInterval();
125 desc
->shotTime
= when
;
126 desc
->running
= true;
128 wxLogTrace( wxT("timer"),
129 wxT("queued timer %p at tick %") wxTimerTickFmtSpec
,
130 desc
->timer
, wxTimerTickPrintfArg(when
));
134 wxTimerDesc
*d
= m_timers
;
135 while ( d
->next
&& d
->next
->shotTime
< when
) d
= d
->next
;
136 desc
->next
= d
->next
;
139 d
->next
->prev
= desc
;
145 desc
->prev
= desc
->next
= NULL
;
149 void wxTimerScheduler::RemoveTimer(wxTimerDesc
*desc
)
151 desc
->running
= false;
152 if ( desc
== m_timers
)
153 m_timers
= desc
->next
;
155 desc
->prev
->next
= desc
->next
;
157 desc
->next
->prev
= desc
->prev
;
158 desc
->prev
= desc
->next
= NULL
;
161 void wxTimerScheduler::NotifyTimers()
166 volatile bool timerDeleted
;
167 wxTimerTick_t now
= GetMillisecondsTime();
169 for ( wxTimerDesc
*desc
= m_timers
; desc
; desc
= desc
->next
)
171 if ( desc
->running
&& wxTickGreaterEqual(now
, desc
->shotTime
) )
173 oneShot
= desc
->timer
->IsOneShot();
176 timerDeleted
= false;
177 desc
->deleteFlag
= &timerDeleted
;
178 desc
->timer
->Notify();
182 wxLogTrace( wxT("timer"),
183 wxT("notified timer %p sheduled for %")
186 wxTimerTickPrintfArg(desc
->shotTime
) );
188 desc
->deleteFlag
= NULL
;
190 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
)