]>
git.saurik.com Git - wxWidgets.git/blob - src/x11/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/x11/private.h"
30 // ----------------------------------------------------------------------------
31 // helper structures and wxTimerScheduler
32 // ----------------------------------------------------------------------------
37 wxTimerDesc(wxTimer
*t
) :
38 timer(t
), running(FALSE
), next(NULL
), prev(NULL
),
39 shotTime(0), deleteFlag(NULL
) {}
43 wxTimerDesc
*next
, *prev
;
44 unsigned long shotTime
;
45 volatile bool *deleteFlag
; // see comment in ~wxTimer
48 class wxTimerScheduler
51 wxTimerScheduler() : m_timers(NULL
) {}
53 void QueueTimer(wxTimerDesc
*desc
, unsigned long when
= 0);
54 void RemoveTimer(wxTimerDesc
*desc
);
58 wxTimerDesc
*m_timers
;
61 void wxTimerScheduler::QueueTimer(wxTimerDesc
*desc
, unsigned long when
)
64 return; // already scheduled
69 when
= wxGetLocalTimeMillis() + desc
->timer
->GetInterval();
71 desc
->shotTime
= when
;
74 wxLogTrace("mgl_timer", "queued timer %p at tick %i",
79 wxTimerDesc
*d
= m_timers
;
80 while ( d
->next
&& d
->next
->shotTime
< when
) d
= d
->next
;
90 desc
->prev
= desc
->next
= NULL
;
94 void wxTimerScheduler::RemoveTimer(wxTimerDesc
*desc
)
96 desc
->running
= FALSE
;
97 if ( desc
== m_timers
)
98 m_timers
= desc
->next
;
100 desc
->prev
->next
= desc
->next
;
102 desc
->next
->prev
= desc
->prev
;
103 desc
->prev
= desc
->next
= NULL
;
106 void wxTimerScheduler::NotifyTimers()
111 volatile bool timerDeleted
;
115 now
= wxGetLocalTimeMillis();
119 while ( m_timers
&& m_timers
->shotTime
<= now
)
122 oneShot
= desc
->timer
->IsOneShot();
125 timerDeleted
= FALSE
;
126 desc
->deleteFlag
= &timerDeleted
;
127 desc
->timer
->Notify();
131 wxLogTrace("mgl_timer", "notified timer %p sheduled for %i",
132 desc
->timer
, desc
->shotTime
);
134 desc
->deleteFlag
= NULL
;
136 QueueTimer(desc
, now
+ desc
->timer
->GetInterval());
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 IMPLEMENT_ABSTRACT_CLASS(wxTimer
,wxObject
)
150 wxTimerScheduler
*gs_scheduler
= NULL
;
155 gs_scheduler
= new wxTimerScheduler
;
156 m_desc
= new wxTimerDesc(this);
161 wxLogTrace("mgl_timer", "destroying timer %p...", this);
165 // NB: this is a hack: wxTimerScheduler must have some way of knowing
166 // that wxTimer object was deleted under its hands -- this may
167 // happen if somebody is really nasty and deletes the timer
168 // from wxTimer::Notify()
169 if ( m_desc
->deleteFlag
!= NULL
)
170 *m_desc
->deleteFlag
= TRUE
;
173 wxLogTrace("mgl_timer", " ...done destroying timer %p...", this);
176 bool wxTimer::IsRunning() const
178 return m_desc
->running
;
181 bool wxTimer::Start(int millisecs
, bool oneShot
)
183 wxLogTrace("mgl_timer", "started timer %p: %i ms, oneshot=%i",
184 this, millisecs
, oneShot
);
186 if ( !wxTimerBase::Start(millisecs
, oneShot
) )
189 gs_scheduler
->QueueTimer(m_desc
);
195 if ( !m_desc
->running
) return;
197 gs_scheduler
->RemoveTimer(m_desc
);
200 /*static*/ void wxTimer::NotifyTimers()
203 gs_scheduler
->NotifyTimers();
208 // A module to deallocate memory properly:
209 class wxTimerModule
: public wxModule
211 DECLARE_DYNAMIC_CLASS(wxTimerModule
)
214 bool OnInit() { return TRUE
; }
215 void OnExit() { delete gs_scheduler
; gs_scheduler
= NULL
; }
218 IMPLEMENT_DYNAMIC_CLASS(wxTimerModule
, wxModule
)