]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/timerunx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/timerunx.cpp
3 // Purpose: wxTimer implementation for non-GUI applications under Unix
4 // Author: Lukasz Michalski
7 // Copyright: (c) 2007 Lukasz Michalski
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
23 #include "wx/module.h"
26 #include "wx/hashmap.h"
30 #include "wx/longlong.h"
35 #include "wx/unix/private/timer.h"
37 #include "wx/listimpl.cpp"
38 WX_DEFINE_LIST(wxTimerList
);
40 // trace mask for the debugging messages used here
41 #define wxTrace_Timer wxT("timer")
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // helper function to format wxUsecClock_t
48 static inline wxString
wxUsecClockAsString(wxUsecClock_t usec
)
51 return usec
.ToString();
52 #else // wxUsecClock_t == double
53 return wxString::Format(_T("%.0f"), usec
);
57 // ============================================================================
58 // wxTimerScheduler implementation
59 // ============================================================================
61 wxTimerScheduler
*wxTimerScheduler::ms_instance
= NULL
;
63 wxTimerScheduler::~wxTimerScheduler()
65 for ( wxTimerList::iterator node
= m_timers
.begin();
66 node
!= m_timers
.end();
73 void wxTimerScheduler::AddTimer(wxUnixTimerImpl
*timer
, wxUsecClock_t expiration
)
75 DoAddTimer(new wxTimerSchedule(timer
, expiration
));
78 void wxTimerScheduler::DoAddTimer(wxTimerSchedule
*s
)
80 // do an insertion sort to keep the list sorted in expiration order
81 wxTimerList::iterator node
;
82 for ( node
= m_timers
.begin(); node
!= m_timers
.end(); ++node
)
84 wxASSERT_MSG( (*node
)->m_timer
!= s
->m_timer
,
85 _T("adding the same timer twice?") );
87 if ( (*node
)->m_expiration
> s
->m_expiration
)
91 m_timers
.insert(node
, s
);
93 wxLogTrace(wxTrace_Timer
, wxT("Inserted timer %d expiring at %s"),
95 wxUsecClockAsString(s
->m_expiration
).c_str());
98 void wxTimerScheduler::RemoveTimer(wxUnixTimerImpl
*timer
)
100 wxLogTrace(wxTrace_Timer
, wxT("Removing timer %d"), timer
->GetId());
102 for ( wxTimerList::iterator node
= m_timers
.begin();
103 node
!= m_timers
.end();
106 if ( (*node
)->m_timer
== timer
)
109 m_timers
.erase(node
);
114 wxFAIL_MSG( _T("removing inexistent timer?") );
117 bool wxTimerScheduler::GetNext(wxUsecClock_t
*remaining
) const
119 if ( m_timers
.empty() )
122 wxCHECK_MSG( remaining
, false, _T("NULL pointer") );
124 *remaining
= (*m_timers
.begin())->m_expiration
- wxGetLocalTimeUsec();
125 if ( *remaining
< 0 )
127 // timer already expired, don't wait at all before notifying it
134 void wxTimerScheduler::NotifyExpired()
136 if ( m_timers
.empty() )
139 const wxUsecClock_t now
= wxGetLocalTimeUsec();
141 wxTimerList::iterator cur
,
143 for ( cur
= m_timers
.begin(); cur
!= m_timers
.end(); cur
= next
)
145 wxTimerSchedule
* const s
= *cur
;
146 if ( s
->m_expiration
> now
)
148 // as the list is sorted by expiration time, we can skip the rest
152 // remember next as we will delete the node pointed to by cur
158 // check whether we need to keep this timer
159 wxUnixTimerImpl
* const timer
= s
->m_timer
;
160 if ( timer
->IsOneShot() )
162 // the timer needs to be stopped but don't call its Stop() from
163 // here as it would attempt to remove the timer from our list and
164 // we had already done it, so we just need to reset its state
165 timer
->MarkStopped();
167 // don't need it any more
170 else // reschedule the next timer expiration
172 s
->m_expiration
+= timer
->GetInterval()*1000;
176 // and finally notify the timer
181 // ============================================================================
182 // wxUnixTimerImpl implementation
183 // ============================================================================
185 wxUnixTimerImpl::wxUnixTimerImpl(wxTimer
* timer
)
191 bool wxUnixTimerImpl::Start(int milliseconds
, bool oneShot
)
193 // notice that this will stop an already running timer
194 wxTimerImpl::Start(milliseconds
, oneShot
);
196 wxTimerScheduler::Get().AddTimer(this, wxGetLocalTimeUsec() + m_milli
*1000);
202 void wxUnixTimerImpl::Stop()
206 wxTimerScheduler::Get().RemoveTimer(this);
212 bool wxUnixTimerImpl::IsRunning() const
217 wxUnixTimerImpl::~wxUnixTimerImpl()
219 wxASSERT_MSG( !m_isRunning
, _T("must have been stopped before") );
222 // ============================================================================
223 // wxTimerUnixModule: responsible for freeing the global timer scheduler
224 // ============================================================================
226 class wxTimerUnixModule
: public wxModule
229 wxTimerUnixModule() {}
230 virtual bool OnInit() { return true; }
231 virtual void OnExit() { wxTimerScheduler::Shutdown(); }
233 DECLARE_DYNAMIC_CLASS(wxTimerUnixModule
)
236 IMPLEMENT_DYNAMIC_CLASS(wxTimerUnixModule
, wxModule
)
238 // ============================================================================
240 // ============================================================================
242 wxUsecClock_t
wxGetLocalTimeUsec()
244 #ifdef HAVE_GETTIMEOFDAY
246 if ( wxGetTimeOfDay(&tv
) != -1 )
248 wxUsecClock_t val
= 1000000L; // usec/sec
250 return val
+ tv
.tv_usec
;
252 #endif // HAVE_GETTIMEOFDAY
254 return wxGetLocalTimeMillis() * 1000L;