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 // ----------------------------------------------------------------------------
29 #include "wx/module.h"
32 // ----------------------------------------------------------------------------
33 // Time input function
34 // ----------------------------------------------------------------------------
37 // We take advantage of wxMGL's _EVT_getTicks because it is faster
38 // (especially under MS-DOS!) and more precise than wxGetLocalTimeMillis
39 // if we are unlucky and the latter combines information from two sources.
40 #include "wx/mgl/private.h"
41 extern "C" ulong
_EVT_getTicks();
42 #define GetMillisecondsTime _EVT_getTicks
44 typedef ulong wxTimerTick_t
;
46 #define wxTimerTickFmtSpec _T("lu")
47 #define wxTimerTickPrintfArg(tt) (tt)
50 // Under DOS the MGL timer has a 24hr period, so consider the 12 hours
51 // before y to be 'less' and the the 12 hours after 'greater' modulo
53 inline bool wxTickGreaterEqual(wxTimerTick_t x
, wxTimerTick_t y
)
55 // _EVT_getTicks wraps at 1573040 * 55
56 const wxTimerTick_t modulus
= 1573040 * 55;
57 return (2 * modulus
+ x
- y
) % modulus
< modulus
/ 2;
60 // If wxTimerTick_t is 32-bits then it'll wrap in around 50 days. So
61 // let the 25 days before y be 'less' and 25 days after be 'greater'.
62 inline bool wxTickGreaterEqual(wxTimerTick_t x
, wxTimerTick_t y
)
64 // This code assumes wxTimerTick_t is an unsigned type.
65 // Set half_modulus with top bit set and the rest zeros.
66 const wxTimerTick_t half_modulus
= ~((~(wxTimerTick_t
)0) >> 1);
67 return x
- y
< half_modulus
;
71 #define GetMillisecondsTime wxGetLocalTimeMillis
73 typedef wxLongLong wxTimerTick_t
;
76 #define wxTimerTickFmtSpec wxLongLongFmtSpec _T("d")
77 #define wxTimerTickPrintfArg(tt) (tt.GetValue())
78 #else // using native wxLongLong
79 #define wxTimerTickFmtSpec _T("s")
80 #define wxTimerTickPrintfArg(tt) (tt.ToString().c_str())
81 #endif // wx/native long long
83 inline bool wxTickGreaterEqual(wxTimerTick_t x
, wxTimerTick_t y
)
87 #endif // __WXMGL__/!__WXMGL__
89 // ----------------------------------------------------------------------------
90 // helper structures and wxTimerScheduler
91 // ----------------------------------------------------------------------------
96 wxTimerDesc(wxTimer
*t
) :
97 timer(t
), running(false), next(NULL
), prev(NULL
),
98 shotTime(0), deleteFlag(NULL
) {}
102 wxTimerDesc
*next
, *prev
;
103 wxTimerTick_t shotTime
;
104 volatile bool *deleteFlag
; // see comment in ~wxTimer
107 class wxTimerScheduler
110 wxTimerScheduler() : m_timers(NULL
) {}
112 void QueueTimer(wxTimerDesc
*desc
, wxTimerTick_t when
= 0);
113 void RemoveTimer(wxTimerDesc
*desc
);
117 wxTimerDesc
*m_timers
;
120 void wxTimerScheduler::QueueTimer(wxTimerDesc
*desc
, wxTimerTick_t when
)
123 return; // already scheduled
126 when
= GetMillisecondsTime() + desc
->timer
->GetInterval();
127 desc
->shotTime
= when
;
128 desc
->running
= true;
130 wxLogTrace( wxT("timer"),
131 wxT("queued timer %p at tick %") wxTimerTickFmtSpec
,
132 desc
->timer
, wxTimerTickPrintfArg(when
));
136 wxTimerDesc
*d
= m_timers
;
137 while ( d
->next
&& d
->next
->shotTime
< when
) d
= d
->next
;
138 desc
->next
= d
->next
;
141 d
->next
->prev
= desc
;
147 desc
->prev
= desc
->next
= NULL
;
151 void wxTimerScheduler::RemoveTimer(wxTimerDesc
*desc
)
153 desc
->running
= false;
154 if ( desc
== m_timers
)
155 m_timers
= desc
->next
;
157 desc
->prev
->next
= desc
->next
;
159 desc
->next
->prev
= desc
->prev
;
160 desc
->prev
= desc
->next
= NULL
;
163 void wxTimerScheduler::NotifyTimers()
168 volatile bool timerDeleted
;
169 wxTimerTick_t now
= GetMillisecondsTime();
171 for ( wxTimerDesc
*desc
= m_timers
; desc
; desc
= desc
->next
)
173 if ( desc
->running
&& wxTickGreaterEqual(now
, desc
->shotTime
) )
175 oneShot
= desc
->timer
->IsOneShot();
178 timerDeleted
= false;
179 desc
->deleteFlag
= &timerDeleted
;
180 desc
->timer
->Notify();
184 wxLogTrace( wxT("timer"),
185 wxT("notified timer %p sheduled for %")
188 wxTimerTickPrintfArg(desc
->shotTime
) );
190 desc
->deleteFlag
= NULL
;
192 QueueTimer(desc
, now
+ desc
->timer
->GetInterval());
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
210 IMPLEMENT_ABSTRACT_CLASS(wxTimer
, wxEvtHandler
)
212 wxTimerScheduler
*gs_scheduler
= NULL
;
217 gs_scheduler
= new wxTimerScheduler
;
218 m_desc
= new wxTimerDesc(this);
223 wxLogTrace( wxT("timer"), wxT("destroying timer %p..."), this);
227 // NB: this is a hack: wxTimerScheduler must have some way of knowing
228 // that wxTimer object was deleted under its hands -- this may
229 // happen if somebody is really nasty and deletes the timer
230 // from wxTimer::Notify()
231 if ( m_desc
->deleteFlag
!= NULL
)
232 *m_desc
->deleteFlag
= true;
235 wxLogTrace( wxT("timer"), wxT(" ...done destroying timer %p..."), this);
238 bool wxTimer::IsRunning() const
240 return m_desc
->running
;
243 bool wxTimer::Start(int millisecs
, bool oneShot
)
245 wxLogTrace( wxT("timer"), wxT("started timer %p: %i ms, oneshot=%i"),
246 this, millisecs
, oneShot
);
248 if ( !wxTimerBase::Start(millisecs
, oneShot
) )
251 gs_scheduler
->QueueTimer(m_desc
);
257 if ( !m_desc
->running
) return;
259 gs_scheduler
->RemoveTimer(m_desc
);
262 /*static*/ void wxTimer::NotifyTimers()
265 gs_scheduler
->NotifyTimers();
270 // A module to deallocate memory properly:
271 class wxTimerModule
: public wxModule
273 DECLARE_DYNAMIC_CLASS(wxTimerModule
)
276 bool OnInit() { return true; }
277 void OnExit() { delete gs_scheduler
; gs_scheduler
= NULL
; }
280 IMPLEMENT_DYNAMIC_CLASS(wxTimerModule
, wxModule
)