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 uint64 lastEvent
= GetTickCountFullRes();
45 uint64 now
= GetTickCountFullRes();
46 uint64 sinceLast
= now
- lastEvent
;
47 if (sinceLast
> 100 * m_period
) {
48 // We're way too far behind. Probably what really happened is
49 // the system time was adjusted backwards a bit, or (less
50 // likely) the time wrapped past the limit of a uint64. So,
51 // the calculation of sinceLast has produced an absurd value.
52 sinceLast
= 100 * m_period
;
53 lastEvent
= now
- sinceLast
;
56 unsigned long timeout
= ((m_period
< sinceLast
) ? 0 : (m_period
- sinceLast
));
58 // In normal operation, we will never actually acquire the
59 // semaphore; we will always timeout. This is used to
60 // implement a Sleep operation which the owning CTimer can
61 // interrupt by posting to the semaphore. So, it follows
62 // that if we do acquire the semaphore it means the owner
64 if (m_sleepSemaphore
.WaitTimeout(timeout
) == wxSEMA_TIMEOUT
) {
65 // Increment for one event only, so no events can be lost.
66 lastEvent
+= m_period
;
68 wxPostEvent(m_owner
, evt
);
77 unsigned long m_period
;
79 wxEvtHandler
* m_owner
;
81 wxSemaphore m_sleepSemaphore
;
85 ////////////////////// CTimer ////////////////////////
93 CTimer::CTimer(wxEvtHandler
* owner
, int id
)
102 bool CTimer::IsRunning() const
104 return (m_thread
&& m_thread
->IsRunning());
108 bool CTimer::Start(int millisecs
, bool oneShot
)
110 wxCHECK_MSG(m_id
!= -1, false, wxT("Invalid target-ID for timer-events."));
112 // Since this class generally matches wxTimer, calling
113 // start on a running timer stops and then restarts it.
116 m_thread
= new CTimerThread();
117 m_thread
->m_period
= millisecs
;
118 m_thread
->m_oneShot
= oneShot
;
119 m_thread
->m_owner
= m_owner
;
120 m_thread
->m_id
= m_id
;
122 if (m_thread
->Create() == wxTHREAD_NO_ERROR
) {
123 if (m_thread
->Run() == wxTHREAD_NO_ERROR
) {
128 // Something went wrong ...
140 m_thread
->m_sleepSemaphore
.Post();
148 DEFINE_LOCAL_EVENT_TYPE(MULE_EVT_TIMER
)
150 CTimerEvent::CTimerEvent(int id
)
151 : wxEvent(id
, MULE_EVT_TIMER
)
156 wxEvent
* CTimerEvent::Clone() const
158 return new CTimerEvent(GetId());
161 // File_checked_for_headers