~ubuntu-branches/ubuntu/lucid/lastfm/lucid

« back to all changes in this revision

Viewing changes to src/libUnicorn/StopWatch.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Pedro Fragoso
  • Date: 2007-12-31 09:49:54 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20071231094954-ix1amvcsj9pk61ya
Tags: 1:1.4.1.57486.dfsg-1ubuntu1
* Merge from Debian unstable (LP: #180254), remaining changes:
  - debian/rules;
    - Added dh_icons
  - Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2005 - 2007 by                                          *
 
3
 *      Christian Muehlhaeuser, Last.fm Ltd <chris@last.fm>                *
 
4
 *      Erik Jaelevik, Last.fm Ltd <erik@last.fm>                          *
 
5
 *                                                                         *
 
6
 *   This program is free software; you can redistribute it and/or modify  *
 
7
 *   it under the terms of the GNU General Public License as published by  *
 
8
 *   the Free Software Foundation; either version 2 of the License, or     *
 
9
 *   (at your option) any later version.                                   *
 
10
 *                                                                         *
 
11
 *   This program is distributed in the hope that it will be useful,       *
 
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
14
 *   GNU General Public License for more details.                          *
 
15
 *                                                                         *
 
16
 *   You should have received a copy of the GNU General Public License     *
 
17
 *   along with this program; if not, write to the                         *
 
18
 *   Free Software Foundation, Inc.,                                       *
 
19
 *   51 Franklin Steet, Fifth Floor, Boston, MA  02110-1301, USA.          *
 
20
 ***************************************************************************/
 
21
 
 
22
#include "StopWatch.h"
 
23
#include "logger.h"
 
24
 
 
25
/******************************************************************************
 
26
    CStopWatch
 
27
******************************************************************************/
 
28
StopWatch::StopWatch() :
 
29
    QThread(),
 
30
    mState(STOPPED),
 
31
    mnTimer(0),
 
32
    mnTimeout(0),
 
33
    mbTimedOut( false )
 
34
{
 
35
}
 
36
 
 
37
/******************************************************************************
 
38
    CStopWatch copy
 
39
******************************************************************************/
 
40
StopWatch::StopWatch(
 
41
    const StopWatch& that) :
 
42
    QThread()
 
43
{
 
44
    clone(that);
 
45
}
 
46
 
 
47
/******************************************************************************
 
48
    operator=
 
49
******************************************************************************/
 
50
StopWatch&
 
51
StopWatch::operator=(
 
52
    const StopWatch& that)
 
53
{
 
54
    // Check for self-assignment
 
55
    if (&that != this)
 
56
    {
 
57
        clone(that);
 
58
    }
 
59
 
 
60
    return *this;
 
61
}
 
62
 
 
63
/******************************************************************************
 
64
    Clone
 
65
******************************************************************************/
 
66
void
 
67
StopWatch::clone(
 
68
    const StopWatch& that)
 
69
{
 
70
    QMutexLocker grab(&mMutex);
 
71
    mnTimer     = that.mnTimer;
 
72
    mnTimeout   = that.mnTimeout;
 
73
    mState      = that.mState;
 
74
}
 
75
 
 
76
/******************************************************************************
 
77
    Start
 
78
******************************************************************************/
 
79
void
 
80
StopWatch::start()
 
81
{
 
82
    mMutex.lock();
 
83
    EStopWatchState state = mState;
 
84
    mMutex.unlock();
 
85
 
 
86
    if (state == RUNNING)
 
87
    {
 
88
        return;
 
89
    }
 
90
 
 
91
    mMutex.lock();
 
92
    mState = RUNNING;
 
93
    mMutex.unlock();
 
94
 
 
95
    QThread::start();
 
96
}
 
97
 
 
98
/******************************************************************************
 
99
    Stop
 
100
******************************************************************************/
 
101
void
 
102
StopWatch::stop()
 
103
{
 
104
    mMutex.lock();
 
105
    mState = STOPPED;
 
106
    mMutex.unlock();
 
107
 
 
108
    // We need to make sure the timer thread has finished since it might call
 
109
    // Notify after we've called Stop. Since it's going back to
 
110
    // PlayerConnection::onScrobbleTimeout to submit a track, we need to ensure
 
111
    // that the track it submits is the same one that it was timing.
 
112
    wait();
 
113
}
 
114
 
 
115
/******************************************************************************
 
116
    Reset
 
117
******************************************************************************/
 
118
void
 
119
StopWatch::reset()
 
120
{
 
121
    mMutex.lock();
 
122
    mnTimer = 0;
 
123
    mbTimedOut = false;
 
124
    mMutex.unlock();
 
125
 
 
126
    emit valueChanged(mnTimer);
 
127
    emit timerReset();
 
128
}
 
129
 
 
130
/******************************************************************************
 
131
    setTimeout
 
132
******************************************************************************/
 
133
void
 
134
StopWatch::setTimeout(
 
135
    int nTimeout)
 
136
{
 
137
    mMutex.lock();
 
138
    int curTime = mnTimer;
 
139
    mMutex.unlock();
 
140
 
 
141
    Q_ASSERT( nTimeout > curTime );
 
142
 
 
143
    mnTimeout = nTimeout;
 
144
 
 
145
    emit timeoutChanged(mnTimeout);
 
146
}
 
147
 
 
148
 
 
149
/******************************************************************************
 
150
    ThreadMain
 
151
******************************************************************************/
 
152
void
 
153
StopWatch::run()
 
154
{
 
155
    bool bStopped = false;
 
156
    mLastTime = QDateTime::currentDateTime();
 
157
 
 
158
    int totalMs = 0;
 
159
 
 
160
    do
 
161
    {
 
162
        int nSleepInterval = 250;
 
163
        msleep(nSleepInterval);
 
164
 
 
165
        mMutex.lock();
 
166
 
 
167
        // Poll for stop every nSleepInterval
 
168
        bStopped = mState == STOPPED;
 
169
 
 
170
        QDateTime currentTime = QDateTime::currentDateTime();
 
171
        int msSpentSleeping = mLastTime.time().msecsTo( currentTime.time() );
 
172
        if ( msSpentSleeping >= 1000 )
 
173
        {
 
174
            mLastTime = currentTime;
 
175
 
 
176
            totalMs += msSpentSleeping;
 
177
            mnTimer = totalMs / (int)1000;
 
178
 
 
179
            /*
 
180
            LOGL( 3, "msSpent: " << msSpentSleeping );
 
181
            LOGL( 3, "ms: " << totalMs );
 
182
            LOGL( 3, "s: " << mnTimer );
 
183
            */
 
184
 
 
185
            // Poll for timeout every second
 
186
            if (!mbTimedOut && mnTimer >= mnTimeout)
 
187
            {
 
188
                emit timeoutReached();
 
189
                mbTimedOut = true;
 
190
            }
 
191
 
 
192
            emit valueChanged(mnTimer);
 
193
        }
 
194
 
 
195
        mMutex.unlock();
 
196
 
 
197
    } while( !bStopped );
 
198
}