]>
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"
20 #include "wx/module.h"
21 #include "wx/mgl/private.h"
23 extern "C" ulong
_EVT_getTicks();
25 // ----------------------------------------------------------------------------
26 // helper structures and wxTimerScheduler
27 // ----------------------------------------------------------------------------
32 wxTimerDesc(wxTimer
*t
) :
33 timer(t
), running(FALSE
), next(NULL
), prev(NULL
),
34 shotTime(0), deleteFlag(NULL
) {}
38 wxTimerDesc
*next
, *prev
;
39 unsigned long shotTime
;
40 volatile bool *deleteFlag
; // see comment in ~wxTimer
43 class wxTimerScheduler
46 wxTimerScheduler() : m_timers(NULL
) {}
48 void QueueTimer(wxTimerDesc
*desc
, unsigned long when
= 0);
49 void RemoveTimer(wxTimerDesc
*desc
);
53 wxTimerDesc
*m_timers
;
56 void wxTimerScheduler::QueueTimer(wxTimerDesc
*desc
, unsigned long when
)
59 return; // already scheduled
62 when
= _EVT_getTicks() + desc
->timer
->GetInterval();
63 desc
->shotTime
= when
;
66 wxLogTrace("mgl_timer", "queued timer %p at tick %i",
71 wxTimerDesc
*d
= m_timers
;
72 while ( d
->next
&& d
->next
->shotTime
< when
) d
= d
->next
;
82 desc
->prev
= desc
->next
= NULL
;
86 void wxTimerScheduler::RemoveTimer(wxTimerDesc
*desc
)
88 desc
->running
= FALSE
;
89 if ( desc
== m_timers
)
90 m_timers
= desc
->next
;
92 desc
->prev
->next
= desc
->next
;
94 desc
->next
->prev
= desc
->prev
;
95 desc
->prev
= desc
->next
= NULL
;
98 void wxTimerScheduler::NotifyTimers()
103 volatile bool timerDeleted
;
104 unsigned long now
= _EVT_getTicks();
107 while ( m_timers
&& m_timers
->shotTime
<= now
)
110 oneShot
= desc
->timer
->IsOneShot();
113 timerDeleted
= FALSE
;
114 desc
->deleteFlag
= &timerDeleted
;
115 desc
->timer
->Notify();
119 wxLogTrace("mgl_timer", "notified timer %p sheduled for %i",
120 desc
->timer
, desc
->shotTime
);
122 desc
->deleteFlag
= NULL
;
124 QueueTimer(desc
, now
+ desc
->timer
->GetInterval());
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 IMPLEMENT_ABSTRACT_CLASS(wxTimer
,wxObject
)
138 wxTimerScheduler
*gs_scheduler
= NULL
;
143 gs_scheduler
= new wxTimerScheduler
;
144 m_desc
= new wxTimerDesc(this);
149 wxLogTrace("mgl_timer", "destroying timer %p...", this);
153 // NB: this is a hack: wxTimerScheduler must have some way of knowing
154 // that wxTimer object was deleted under its hands -- this may
155 // happen if somebody is really nasty and deletes the timer
156 // from wxTimer::Notify()
157 if ( m_desc
->deleteFlag
!= NULL
)
158 *m_desc
->deleteFlag
= TRUE
;
161 wxLogTrace("mgl_timer", " ...done destroying timer %p...", this);
164 bool wxTimer::IsRunning() const
166 return m_desc
->running
;
169 bool wxTimer::Start(int millisecs
, bool oneShot
)
171 wxLogTrace("mgl_timer", "started timer %p: %i ms, oneshot=%i",
172 this, millisecs
, oneShot
);
174 if ( !wxTimerBase::Start(millisecs
, oneShot
) )
177 gs_scheduler
->QueueTimer(m_desc
);
183 if ( !m_desc
->running
) return;
185 gs_scheduler
->RemoveTimer(m_desc
);
188 /*static*/ void wxTimer::NotifyTimers()
191 gs_scheduler
->NotifyTimers();
196 // A module to deallocate memory properly:
197 class wxTimerModule
: public wxModule
199 DECLARE_DYNAMIC_CLASS(wxTimerModule
)
202 bool OnInit() { return TRUE
; }
203 void OnExit() { delete gs_scheduler
; gs_scheduler
= NULL
; }
206 IMPLEMENT_DYNAMIC_CLASS(wxTimerModule
, wxModule
)