1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/timer.cpp
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 // ----------------------------------------------------------------------------
31 #include "wx/module.h"
33 // ----------------------------------------------------------------------------
34 // Time input function
35 // ----------------------------------------------------------------------------
38 // We take advantage of wxMGL's _EVT_getTicks because it is faster
39 // (especially under MS-DOS!) and more precise than wxGetLocalTimeMillis
40 // if we are unlucky and the latter combines information from two sources.
41 #include "wx/mgl/private.h"
42 extern "C" ulong
_EVT_getTicks();
43 #define GetMillisecondsTime _EVT_getTicks
45 typedef ulong wxTimerTick_t
;
47 #define wxTimerTickFmtSpec _T("lu")
48 #define wxTimerTickPrintfArg(tt) (tt)
51 // Under DOS the MGL timer has a 24hr period, so consider the 12 hours
52 // before y to be 'less' and the the 12 hours after 'greater' modulo
54 inline bool wxTickGreaterEqual(wxTimerTick_t x
, wxTimerTick_t y
)
56 // _EVT_getTicks wraps at 1573040 * 55
57 const wxTimerTick_t modulus
= 1573040 * 55;
58 return (2 * modulus
+ x
- y
) % modulus
< modulus
/ 2;
61 // If wxTimerTick_t is 32-bits then it'll wrap in around 50 days. So
62 // let the 25 days before y be 'less' and 25 days after be 'greater'.
63 inline bool wxTickGreaterEqual(wxTimerTick_t x
, wxTimerTick_t y
)
65 // This code assumes wxTimerTick_t is an unsigned type.
66 // Set half_modulus with top bit set and the rest zeros.
67 const wxTimerTick_t half_modulus
= ~((~(wxTimerTick_t
)0) >> 1);
68 return x
- y
< half_modulus
;
72 #define GetMillisecondsTime wxGetLocalTimeMillis
74 typedef wxLongLong wxTimerTick_t
;
77 #define wxTimerTickFmtSpec wxLongLongFmtSpec _T("d")
78 #define wxTimerTickPrintfArg(tt) (tt.GetValue())
79 #else // using native wxLongLong
80 #define wxTimerTickFmtSpec _T("s")
81 #define wxTimerTickPrintfArg(tt) (tt.ToString().c_str())
82 #endif // wx/native long long
84 inline bool wxTickGreaterEqual(wxTimerTick_t x
, wxTimerTick_t y
)
88 #endif // __WXMGL__/!__WXMGL__
90 // ----------------------------------------------------------------------------
91 // helper structures and wxTimerScheduler
92 // ----------------------------------------------------------------------------
97 wxTimerDesc(wxTimer
*t
) :
98 timer(t
), running(false), next(NULL
), prev(NULL
),
99 shotTime(0), deleteFlag(NULL
) {}
103 wxTimerDesc
*next
, *prev
;
104 wxTimerTick_t shotTime
;
105 volatile bool *deleteFlag
; // see comment in ~wxTimer
108 class wxTimerScheduler
111 wxTimerScheduler() : m_timers(NULL
) {}
113 void QueueTimer(wxTimerDesc
*desc
, wxTimerTick_t when
= 0);
114 void RemoveTimer(wxTimerDesc
*desc
);
118 wxTimerDesc
*m_timers
;
121 void wxTimerScheduler::QueueTimer(wxTimerDesc
*desc
, wxTimerTick_t when
)
124 return; // already scheduled
127 when
= GetMillisecondsTime() + desc
->timer
->GetInterval();
128 desc
->shotTime
= when
;
129 desc
->running
= true;
131 wxLogTrace( wxT("timer"),
132 wxT("queued timer %p at tick %") wxTimerTickFmtSpec
,
133 desc
->timer
, wxTimerTickPrintfArg(when
));
137 wxTimerDesc
*d
= m_timers
;
138 while ( d
->next
&& d
->next
->shotTime
< when
) d
= d
->next
;
139 desc
->next
= d
->next
;
142 d
->next
->prev
= desc
;
148 desc
->prev
= desc
->next
= NULL
;
152 void wxTimerScheduler::RemoveTimer(wxTimerDesc
*desc
)
154 desc
->running
= false;
155 if ( desc
== m_timers
)
156 m_timers
= desc
->next
;
158 desc
->prev
->next
= desc
->next
;
160 desc
->next
->prev
= desc
->prev
;
161 desc
->prev
= desc
->next
= NULL
;
164 void wxTimerScheduler::NotifyTimers()
169 volatile bool timerDeleted
;
170 wxTimerTick_t now
= GetMillisecondsTime();
172 for ( wxTimerDesc
*desc
= m_timers
; desc
; desc
= desc
->next
)
174 if ( desc
->running
&& wxTickGreaterEqual(now
, desc
->shotTime
) )
176 oneShot
= desc
->timer
->IsOneShot();
179 timerDeleted
= false;
180 desc
->deleteFlag
= &timerDeleted
;
181 desc
->timer
->Notify();
185 wxLogTrace( wxT("timer"),
186 wxT("notified timer %p sheduled for %")
189 wxTimerTickPrintfArg(desc
->shotTime
) );
191 desc
->deleteFlag
= NULL
;
193 QueueTimer(desc
, now
+ desc
->timer
->GetInterval());
207 // ----------------------------------------------------------------------------
209 // ----------------------------------------------------------------------------
211 IMPLEMENT_ABSTRACT_CLASS(wxTimer
, wxEvtHandler
)
213 wxTimerScheduler
*gs_scheduler
= NULL
;
218 gs_scheduler
= new wxTimerScheduler
;
219 m_desc
= new wxTimerDesc(this);
224 wxLogTrace( wxT("timer"), wxT("destroying timer %p..."), this);
228 // NB: this is a hack: wxTimerScheduler must have some way of knowing
229 // that wxTimer object was deleted under its hands -- this may
230 // happen if somebody is really nasty and deletes the timer
231 // from wxTimer::Notify()
232 if ( m_desc
->deleteFlag
!= NULL
)
233 *m_desc
->deleteFlag
= true;
236 wxLogTrace( wxT("timer"), wxT(" ...done destroying timer %p..."), this);
239 bool wxTimer::IsRunning() const
241 return m_desc
->running
;
244 bool wxTimer::Start(int millisecs
, bool oneShot
)
246 wxLogTrace( wxT("timer"), wxT("started timer %p: %i ms, oneshot=%i"),
247 this, millisecs
, oneShot
);
249 if ( !wxTimerBase::Start(millisecs
, oneShot
) )
252 gs_scheduler
->QueueTimer(m_desc
);
258 if ( !m_desc
->running
) return;
260 gs_scheduler
->RemoveTimer(m_desc
);
263 /*static*/ void wxTimer::NotifyTimers()
266 gs_scheduler
->NotifyTimers();
271 // A module to deallocate memory properly:
272 class wxTimerModule
: public wxModule
274 DECLARE_DYNAMIC_CLASS(wxTimerModule
)
277 bool OnInit() { return true; }
278 void OnExit() { delete gs_scheduler
; gs_scheduler
= NULL
; }
281 IMPLEMENT_DYNAMIC_CLASS(wxTimerModule
, wxModule
)