]>
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 // don't need it any more
165 else // reschedule the next timer expiration
167 s
->m_expiration
+= timer
->GetInterval()*1000;
171 // and finally notify the timer
176 // ============================================================================
177 // wxUnixTimerImpl implementation
178 // ============================================================================
180 wxUnixTimerImpl::wxUnixTimerImpl(wxTimer
* timer
)
186 bool wxUnixTimerImpl::Start(int milliseconds
, bool oneShot
)
188 // notice that this will stop an already running timer
189 wxTimerImpl::Start(milliseconds
, oneShot
);
191 wxTimerScheduler::Get().AddTimer(this, wxGetLocalTimeUsec() + m_milli
*1000);
197 void wxUnixTimerImpl::Stop()
201 wxTimerScheduler::Get().RemoveTimer(this);
207 bool wxUnixTimerImpl::IsRunning() const
212 wxUnixTimerImpl::~wxUnixTimerImpl()
214 wxASSERT_MSG( !m_isRunning
, _T("must have been stopped before") );
217 // ============================================================================
218 // wxTimerUnixModule: responsible for freeing the global timer scheduler
219 // ============================================================================
221 class wxTimerUnixModule
: public wxModule
224 wxTimerUnixModule() {}
225 virtual bool OnInit() { return true; }
226 virtual void OnExit() { wxTimerScheduler::Shutdown(); }
228 DECLARE_DYNAMIC_CLASS(wxTimerUnixModule
)
231 IMPLEMENT_DYNAMIC_CLASS(wxTimerUnixModule
, wxModule
)
233 // ============================================================================
235 // ============================================================================
237 wxUsecClock_t
wxGetLocalTimeUsec()
239 #ifdef HAVE_GETTIMEOFDAY
241 if ( wxGetTimeOfDay(&tv
) != -1 )
243 wxUsecClock_t val
= 1000000L; // usec/sec
245 return val
+ tv
.tv_usec
;
247 #endif // HAVE_GETTIMEOFDAY
249 return wxGetLocalTimeMillis() * 1000L;