Upstream tarball 20080901
[amule.git] / src / Timer.cpp
blobbc117821b503bfbba90629d2c4b3d48392a51c50
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 //
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
8 // respective authors.
9 //
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.
19 //
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
34 public:
35 CTimerThread()
36 : CMuleThread(wxTHREAD_JOINABLE)
40 void* Entry() {
41 CTimerEvent evt(m_id);
43 uint64 lastEvent = GetTickCountFullRes();
44 do {
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
63 // wants us to exit.
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);
69 } else {
70 break;
72 } while (!m_oneShot);
74 return NULL;
77 unsigned long m_period;
78 bool m_oneShot;
79 wxEvtHandler* m_owner;
80 int m_id;
81 wxSemaphore m_sleepSemaphore;
85 ////////////////////// CTimer ////////////////////////
87 CTimer::~CTimer()
89 Stop();
93 CTimer::CTimer(wxEvtHandler* owner, int id)
95 wxASSERT(owner);
96 m_owner = owner;
97 m_id = id;
98 m_thread = NULL;
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.
114 Stop();
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) {
124 return true;
128 // Something went wrong ...
129 m_thread->Stop();
130 delete m_thread;
131 m_thread = NULL;
133 return false;
137 void CTimer::Stop()
139 if (m_thread) {
140 m_thread->m_sleepSemaphore.Post();
141 m_thread->Stop();
142 delete m_thread;
143 m_thread = NULL;
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