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