]>
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
67 when
= wxGetLocalTimeMillis() + desc
->timer
->GetInterval();
68 desc
->shotTime
= when
;
71 wxLogTrace("mgl_timer", "queued timer %p at tick %i",
76 wxTimerDesc
*d
= m_timers
;
77 while ( d
->next
&& d
->next
->shotTime
< when
) d
= d
->next
;
87 desc
->prev
= desc
->next
= NULL
;
91 void wxTimerScheduler::RemoveTimer(wxTimerDesc
*desc
)
93 desc
->running
= FALSE
;
94 if ( desc
== m_timers
)
95 m_timers
= desc
->next
;
97 desc
->prev
->next
= desc
->next
;
99 desc
->next
->prev
= desc
->prev
;
100 desc
->prev
= desc
->next
= NULL
;
103 void wxTimerScheduler::NotifyTimers()
108 volatile bool timerDeleted
;
109 unsigned long now
= wxGetLocalTimeMillis();
112 while ( m_timers
&& m_timers
->shotTime
<= now
)
115 oneShot
= desc
->timer
->IsOneShot();
118 timerDeleted
= FALSE
;
119 desc
->deleteFlag
= &timerDeleted
;
120 desc
->timer
->Notify();
124 wxLogTrace("mgl_timer", "notified timer %p sheduled for %i",
125 desc
->timer
, desc
->shotTime
);
127 desc
->deleteFlag
= NULL
;
129 QueueTimer(desc
, now
+ desc
->timer
->GetInterval());
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 IMPLEMENT_ABSTRACT_CLASS(wxTimer
,wxObject
)
143 wxTimerScheduler
*gs_scheduler
= NULL
;
148 gs_scheduler
= new wxTimerScheduler
;
149 m_desc
= new wxTimerDesc(this);
154 wxLogTrace("mgl_timer", "destroying timer %p...", this);
158 // NB: this is a hack: wxTimerScheduler must have some way of knowing
159 // that wxTimer object was deleted under its hands -- this may
160 // happen if somebody is really nasty and deletes the timer
161 // from wxTimer::Notify()
162 if ( m_desc
->deleteFlag
!= NULL
)
163 *m_desc
->deleteFlag
= TRUE
;
166 wxLogTrace("mgl_timer", " ...done destroying timer %p...", this);
169 bool wxTimer::IsRunning() const
171 return m_desc
->running
;
174 bool wxTimer::Start(int millisecs
, bool oneShot
)
176 wxLogTrace("mgl_timer", "started timer %p: %i ms, oneshot=%i",
177 this, millisecs
, oneShot
);
179 if ( !wxTimerBase::Start(millisecs
, oneShot
) )
182 gs_scheduler
->QueueTimer(m_desc
);
188 if ( !m_desc
->running
) return;
190 gs_scheduler
->RemoveTimer(m_desc
);
193 /*static*/ void wxTimer::NotifyTimers()
196 gs_scheduler
->NotifyTimers();
201 // A module to deallocate memory properly:
202 class wxTimerModule
: public wxModule
204 DECLARE_DYNAMIC_CLASS(wxTimerModule
)
207 bool OnInit() { return TRUE
; }
208 void OnExit() { delete gs_scheduler
; gs_scheduler
= NULL
; }
211 IMPLEMENT_DYNAMIC_CLASS(wxTimerModule
, wxModule
)