2008-07-16 16:35:59 +08:00
|
|
|
//
|
2008-07-25 17:08:15 +08:00
|
|
|
// ZoneMinder Timer Class Implementation, $Date$, $Revision$
|
2008-07-25 17:33:23 +08:00
|
|
|
// Copyright (C) 2001-2008 Philip Coombes
|
2008-07-16 16:35:59 +08:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation; either version 2
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
2016-12-26 23:23:16 +08:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2008-07-16 16:35:59 +08:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "zm_timer.h"
|
|
|
|
|
2011-06-21 17:19:10 +08:00
|
|
|
#include "zm_logger.h"
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
int Timer::TimerThread::mNextTimerId = 0;
|
|
|
|
|
|
|
|
Timer::TimerThread::TimerThread( Timer &timer, int duration, bool repeat ) :
|
2016-04-04 22:11:48 +08:00
|
|
|
mTimerId( 0 ),
|
|
|
|
mTimer( timer ),
|
|
|
|
mDuration( duration ),
|
|
|
|
mRepeat( repeat ),
|
|
|
|
mReset( false ),
|
|
|
|
mExpiryFlag( true )
|
2008-07-16 16:35:59 +08:00
|
|
|
{
|
2016-04-04 22:11:48 +08:00
|
|
|
mAccessMutex.lock();
|
|
|
|
mTimerId = mNextTimerId++;
|
|
|
|
Debug( 5, "Creating timer %d for %d seconds%s", mTimerId, mDuration, mRepeat?", repeating":"" );
|
|
|
|
mAccessMutex.unlock();
|
2008-07-16 16:35:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Timer::TimerThread::~TimerThread()
|
|
|
|
{
|
2016-04-04 22:11:48 +08:00
|
|
|
cancel();
|
2008-07-16 16:35:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::TimerThread::cancel()
|
|
|
|
{
|
2016-04-04 22:11:48 +08:00
|
|
|
mAccessMutex.lock();
|
|
|
|
if ( mRunning )
|
|
|
|
{
|
|
|
|
Debug( 4, "Cancelling timer %d", mTimerId );
|
|
|
|
mRepeat = false;
|
|
|
|
mReset = false;
|
|
|
|
mExpiryFlag.updateValueSignal( false );
|
|
|
|
}
|
|
|
|
mAccessMutex.unlock();
|
2008-07-16 16:35:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::TimerThread::reset()
|
|
|
|
{
|
2016-04-04 22:11:48 +08:00
|
|
|
mAccessMutex.lock();
|
|
|
|
if ( mRunning )
|
|
|
|
{
|
|
|
|
Debug( 4, "Resetting timer" );
|
|
|
|
mReset = true;
|
|
|
|
mExpiryFlag.updateValueSignal( false );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Error( "Attempting to reset expired timer %d", mTimerId );
|
|
|
|
}
|
|
|
|
mAccessMutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Timer::TimerThread::run()
|
|
|
|
{
|
|
|
|
Debug( 4, "Starting timer %d for %d seconds", mTimerId, mDuration );
|
|
|
|
bool timerExpired = false;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
mAccessMutex.lock();
|
|
|
|
mReset = false;
|
|
|
|
mExpiryFlag.setValue( true );
|
|
|
|
mAccessMutex.unlock();
|
|
|
|
timerExpired = mExpiryFlag.getUpdatedValue( mDuration );
|
2008-07-16 16:35:59 +08:00
|
|
|
mAccessMutex.lock();
|
2016-04-04 22:11:48 +08:00
|
|
|
if ( timerExpired )
|
2008-07-16 16:35:59 +08:00
|
|
|
{
|
2016-04-04 22:11:48 +08:00
|
|
|
Debug( 4, "Timer %d expired", mTimerId );
|
|
|
|
mTimer.expire();
|
2008-07-16 16:35:59 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-04 22:11:48 +08:00
|
|
|
Debug( 4, "Timer %d %s", mTimerId, mReset?"reset":"cancelled" );
|
2008-07-16 16:35:59 +08:00
|
|
|
}
|
|
|
|
mAccessMutex.unlock();
|
2016-04-04 22:11:48 +08:00
|
|
|
} while ( mRepeat || (mReset && !timerExpired) );
|
|
|
|
return( timerExpired );
|
2008-07-16 16:35:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Timer::Timer( int timeout, bool repeat ) : mTimerThread( *this, timeout, repeat )
|
|
|
|
{
|
2016-04-04 22:11:48 +08:00
|
|
|
mTimerThread.start();
|
2008-07-16 16:35:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Timer::~Timer()
|
|
|
|
{
|
2016-04-04 22:11:48 +08:00
|
|
|
//cancel();
|
2008-07-16 16:35:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::Timer::cancel()
|
|
|
|
{
|
2016-04-04 22:11:48 +08:00
|
|
|
mTimerThread.cancel();
|
2008-07-16 16:35:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::Timer::reset()
|
|
|
|
{
|
2016-04-04 22:11:48 +08:00
|
|
|
mTimerThread.reset();
|
2008-07-16 16:35:59 +08:00
|
|
|
}
|
|
|
|
|