]> git.saurik.com Git - wxWidgets.git/blob - src/mgl/timer.cpp
- better behaviour of wxMGL's wxTimer w.r.t. memory allocations
[wxWidgets.git] / src / mgl / timer.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mgl/timer.cpp
3 // Purpose: wxTimer implementation
4 // Author: Vaclav Slavik
5 // Id: $Id$
6 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation "timer.h"
13 #endif
14
15 #include "wx/timer.h"
16
17 #if wxUSE_TIMER
18
19 #include "wx/log.h"
20 #include "wx/module.h"
21 #include "wx/mgl/private.h"
22
23 extern "C" ulong _EVT_getTicks();
24
25 // ----------------------------------------------------------------------------
26 // helper structures and wxTimerScheduler
27 // ----------------------------------------------------------------------------
28
29 class wxTimerDesc
30 {
31 public:
32 wxTimerDesc(wxTimer *t) :
33 timer(t), running(FALSE), next(NULL), prev(NULL),
34 shotTime(0), deleteFlag(NULL) {}
35
36 wxTimer *timer;
37 bool running;
38 wxTimerDesc *next, *prev;
39 unsigned long shotTime;
40 volatile bool *deleteFlag; // see comment in ~wxTimer
41 };
42
43 class wxTimerScheduler
44 {
45 public:
46 wxTimerScheduler() : m_timers(NULL) {}
47
48 void QueueTimer(wxTimerDesc *desc, unsigned long when = 0);
49 void RemoveTimer(wxTimerDesc *desc);
50 void NotifyTimers();
51
52 private:
53 wxTimerDesc *m_timers;
54 };
55
56 void wxTimerScheduler::QueueTimer(wxTimerDesc *desc, unsigned long when)
57 {
58 if ( desc->running )
59 return; // already scheduled
60
61 if ( when == 0 )
62 when = _EVT_getTicks() + desc->timer->GetInterval();
63 desc->shotTime = when;
64 desc->running = TRUE;
65
66 wxLogTrace("mgl_timer", "queued timer %p at tick %i",
67 desc->timer, when);
68
69 if ( m_timers )
70 {
71 wxTimerDesc *d = m_timers;
72 while ( d->next && d->next->shotTime < when ) d = d->next;
73 desc->next = d->next;
74 desc->prev = d;
75 if ( d->next )
76 d->next->prev = desc;
77 d->next = desc;
78 }
79 else
80 {
81 m_timers = desc;
82 desc->prev = desc->next = NULL;
83 }
84 }
85
86 void wxTimerScheduler::RemoveTimer(wxTimerDesc *desc)
87 {
88 desc->running = FALSE;
89 if ( desc == m_timers )
90 m_timers = desc->next;
91 if ( desc->prev )
92 desc->prev->next = desc->next;
93 if ( desc->next )
94 desc->next->prev = desc->prev;
95 desc->prev = desc->next = NULL;
96 }
97
98 void wxTimerScheduler::NotifyTimers()
99 {
100 if ( m_timers )
101 {
102 bool oneShot;
103 volatile bool timerDeleted;
104 unsigned long now = _EVT_getTicks();
105 wxTimerDesc *desc;
106
107 while ( m_timers && m_timers->shotTime <= now )
108 {
109 desc = m_timers;
110 oneShot = desc->timer->IsOneShot();
111 RemoveTimer(desc);
112
113 timerDeleted = FALSE;
114 desc->deleteFlag = &timerDeleted;
115 desc->timer->Notify();
116
117 if ( !timerDeleted )
118 {
119 wxLogTrace("mgl_timer", "notified timer %p sheduled for %i",
120 desc->timer, desc->shotTime);
121
122 desc->deleteFlag = NULL;
123 if ( !oneShot )
124 QueueTimer(desc, now + desc->timer->GetInterval());
125 }
126 }
127 }
128 }
129
130
131
132 // ----------------------------------------------------------------------------
133 // wxTimer
134 // ----------------------------------------------------------------------------
135
136 IMPLEMENT_ABSTRACT_CLASS(wxTimer,wxObject)
137
138 wxTimerScheduler *gs_scheduler = NULL;
139
140 void wxTimer::Init()
141 {
142 if ( !gs_scheduler )
143 gs_scheduler = new wxTimerScheduler;
144 m_desc = new wxTimerDesc(this);
145 }
146
147 wxTimer::~wxTimer()
148 {
149 wxLogTrace("mgl_timer", "destroying timer %p...", this);
150 if ( IsRunning() )
151 Stop();
152
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;
159
160 delete m_desc;
161 wxLogTrace("mgl_timer", " ...done destroying timer %p...", this);
162 }
163
164 bool wxTimer::IsRunning() const
165 {
166 return m_desc->running;
167 }
168
169 bool wxTimer::Start(int millisecs, bool oneShot)
170 {
171 wxLogTrace("mgl_timer", "started timer %p: %i ms, oneshot=%i",
172 this, millisecs, oneShot);
173
174 if ( !wxTimerBase::Start(millisecs, oneShot) )
175 return FALSE;
176
177 gs_scheduler->QueueTimer(m_desc);
178 return TRUE;
179 }
180
181 void wxTimer::Stop()
182 {
183 if ( !m_desc->running ) return;
184
185 gs_scheduler->RemoveTimer(m_desc);
186 }
187
188 /*static*/ void wxTimer::NotifyTimers()
189 {
190 if ( gs_scheduler )
191 gs_scheduler->NotifyTimers();
192 }
193
194
195
196 // A module to deallocate memory properly:
197 class wxTimerModule: public wxModule
198 {
199 DECLARE_DYNAMIC_CLASS(wxTimerModule)
200 public:
201 wxTimerModule() {}
202 bool OnInit() { return TRUE; }
203 void OnExit() { delete gs_scheduler; gs_scheduler = NULL; }
204 };
205
206 IMPLEMENT_DYNAMIC_CLASS(wxTimerModule, wxModule)
207
208
209 #endif //wxUSE_TIMER