2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
6 // Any parts of this program derived from the xMule, lMule or eMule project,
7 // or contributed by third-party developers are copyrighted by their
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "Timer.h" // Interface declaration
26 #include "GetTickCount.h" // Needed for GetTickCountFullRes
27 #include "MuleThread.h" // Needed for CMuleThread
30 //////////////////////// Timer Thread ////////////////////
32 class CTimerThread
: public CMuleThread
36 : CMuleThread(wxTHREAD_JOINABLE
)
41 CTimerEvent
evt(m_id
);
43 uint32 lastEvent
= GetTickCountFullRes();
46 uint32 now
= GetTickCountFullRes();
47 // This is typically zero, because lastEvent was already incremented by one period.
48 sint32 delta
= now
- lastEvent
;
49 if (delta
> 100 * m_period
) {
50 // We're way too far behind. Probably what really happened is
51 // the system time was adjusted backwards a bit. So,
52 // the calculation of delta has produced an absurd value.
53 delta
= 100 * m_period
;
54 lastEvent
= now
- delta
;
57 // Wait one period (adjusted by the difference just calculated)
58 sint32 timeout
= ((m_period
< delta
) ? 0 : (m_period
- delta
));
60 // In normal operation, we will never actually acquire the
61 // semaphore; we will always timeout. This is used to
62 // implement a Sleep operation which the owning CTimer can
63 // interrupt by posting to the semaphore. So, it follows
64 // that if we do acquire the semaphore it means the owner
66 if (m_sleepSemaphore
.WaitTimeout(timeout
) == wxSEMA_TIMEOUT
) {
67 // Increment for one event only, so no events can be lost.
68 lastEvent
+= m_period
;
70 wxPostEvent(m_owner
, evt
);
81 wxEvtHandler
* m_owner
;
83 wxSemaphore m_sleepSemaphore
;
87 ////////////////////// CTimer ////////////////////////
95 CTimer::CTimer(wxEvtHandler
* owner
, int id
)
104 bool CTimer::IsRunning() const
106 return (m_thread
&& m_thread
->IsRunning());
110 bool CTimer::Start(int millisecs
, bool oneShot
)
112 wxCHECK_MSG(m_id
!= -1, false, wxT("Invalid target-ID for timer-events."));
114 // Since this class generally matches wxTimer, calling
115 // start on a running timer stops and then restarts it.
118 m_thread
= new CTimerThread();
119 m_thread
->m_period
= millisecs
;
120 m_thread
->m_oneShot
= oneShot
;
121 m_thread
->m_owner
= m_owner
;
122 m_thread
->m_id
= m_id
;
124 if (m_thread
->Create() == wxTHREAD_NO_ERROR
) {
125 if (m_thread
->Run() == wxTHREAD_NO_ERROR
) {
130 // Something went wrong ...
142 m_thread
->m_sleepSemaphore
.Post();
150 DEFINE_LOCAL_EVENT_TYPE(MULE_EVT_TIMER
)
152 CTimerEvent::CTimerEvent(int id
)
153 : wxEvent(id
, MULE_EVT_TIMER
)
158 wxEvent
* CTimerEvent::Clone() const
160 return new CTimerEvent(GetId());
163 // File_checked_for_headers