]>
git.saurik.com Git - wxWidgets.git/blob - src/mgl/timer.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTimer implementation
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "timer.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
27 #include "wx/module.h"
28 #include "wx/mgl/private.h"
30 extern "C" ulong
_EVT_getTicks();
32 // ----------------------------------------------------------------------------
33 // helper structures and wxTimerScheduler
34 // ----------------------------------------------------------------------------
39 wxTimerDesc(wxTimer
*t
) :
40 timer(t
), running(FALSE
), next(NULL
), prev(NULL
),
41 shotTime(0), deleteFlag(NULL
) {}
45 wxTimerDesc
*next
, *prev
;
46 unsigned long shotTime
;
47 volatile bool *deleteFlag
; // see comment in ~wxTimer
50 class wxTimerScheduler
53 wxTimerScheduler() : m_timers(NULL
) {}
55 void QueueTimer(wxTimerDesc
*desc
, unsigned long when
= 0);
56 void RemoveTimer(wxTimerDesc
*desc
);
60 wxTimerDesc
*m_timers
;
63 void wxTimerScheduler::QueueTimer(wxTimerDesc
*desc
, unsigned long when
)
66 return; // already scheduled
69 when
= _EVT_getTicks() + desc
->timer
->GetInterval();
70 desc
->shotTime
= when
;
73 wxLogTrace("mgl_timer", "queued timer %p at tick %i",
78 wxTimerDesc
*d
= m_timers
;
79 while ( d
->next
&& d
->next
->shotTime
< when
) d
= d
->next
;
89 desc
->prev
= desc
->next
= NULL
;
93 void wxTimerScheduler::RemoveTimer(wxTimerDesc
*desc
)
95 desc
->running
= FALSE
;
96 if ( desc
== m_timers
)
97 m_timers
= desc
->next
;
99 desc
->prev
->next
= desc
->next
;
101 desc
->next
->prev
= desc
->prev
;
102 desc
->prev
= desc
->next
= NULL
;
105 void wxTimerScheduler::NotifyTimers()
110 volatile bool timerDeleted
;
111 unsigned long now
= _EVT_getTicks();
114 while ( m_timers
&& m_timers
->shotTime
<= now
)
117 oneShot
= desc
->timer
->IsOneShot();
120 timerDeleted
= FALSE
;
121 desc
->deleteFlag
= &timerDeleted
;
122 desc
->timer
->Notify();
126 wxLogTrace("mgl_timer", "notified timer %p sheduled for %i",
127 desc
->timer
, desc
->shotTime
);
129 desc
->deleteFlag
= NULL
;
131 QueueTimer(desc
, now
+ desc
->timer
->GetInterval());
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 IMPLEMENT_ABSTRACT_CLASS(wxTimer
,wxObject
)
145 wxTimerScheduler
*gs_scheduler
= NULL
;
150 gs_scheduler
= new wxTimerScheduler
;
151 m_desc
= new wxTimerDesc(this);
156 wxLogTrace("mgl_timer", "destroying timer %p...", this);
160 // NB: this is a hack: wxTimerScheduler must have some way of knowing
161 // that wxTimer object was deleted under its hands -- this may
162 // happen if somebody is really nasty and deletes the timer
163 // from wxTimer::Notify()
164 if ( m_desc
->deleteFlag
!= NULL
)
165 *m_desc
->deleteFlag
= TRUE
;
168 wxLogTrace("mgl_timer", " ...done destroying timer %p...", this);
171 bool wxTimer::IsRunning() const
173 return m_desc
->running
;
176 bool wxTimer::Start(int millisecs
, bool oneShot
)
178 wxLogTrace("mgl_timer", "started timer %p: %i ms, oneshot=%i",
179 this, millisecs
, oneShot
);
181 if ( !wxTimerBase::Start(millisecs
, oneShot
) )
184 gs_scheduler
->QueueTimer(m_desc
);
190 if ( !m_desc
->running
) return;
192 gs_scheduler
->RemoveTimer(m_desc
);
195 /*static*/ void wxTimer::NotifyTimers()
198 gs_scheduler
->NotifyTimers();
203 // A module to deallocate memory properly:
204 class wxTimerModule
: public wxModule
206 DECLARE_DYNAMIC_CLASS(wxTimerModule
)
209 bool OnInit() { return TRUE
; }
210 void OnExit() { delete gs_scheduler
; gs_scheduler
= NULL
; }
213 IMPLEMENT_DYNAMIC_CLASS(wxTimerModule
, wxModule
)