2008-07-16 16:35:59 +08:00
|
|
|
//
|
2008-07-25 17:08:15 +08:00
|
|
|
// ZoneMinder Timer Class Interface, $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
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef ZM_TIMER_H
|
|
|
|
#define ZM_TIMER_H
|
|
|
|
|
2013-09-25 12:11:59 +08:00
|
|
|
#ifdef HAVE_SYS_SYSCALL_H
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#endif // HAVE_SYS_SYSCALL_H
|
2008-07-16 16:35:59 +08:00
|
|
|
#include "zm_thread.h"
|
|
|
|
|
|
|
|
#include "zm_exception.h"
|
|
|
|
|
|
|
|
class Timer
|
|
|
|
{
|
|
|
|
private:
|
2016-04-04 22:11:48 +08:00
|
|
|
class TimerException : public Exception
|
|
|
|
{
|
|
|
|
private:
|
2015-05-18 08:18:54 +08:00
|
|
|
#ifndef SOLARIS
|
2016-04-04 22:11:48 +08:00
|
|
|
pid_t pid() {
|
|
|
|
pid_t tid;
|
2015-02-24 22:22:08 +08:00
|
|
|
#ifdef __FreeBSD__
|
2016-04-04 22:11:48 +08:00
|
|
|
long lwpid;
|
|
|
|
thr_self(&lwpid);
|
|
|
|
tid = lwpid;
|
2015-02-24 22:22:08 +08:00
|
|
|
#else
|
2016-04-04 22:11:48 +08:00
|
|
|
#ifdef __FreeBSD_kernel__
|
|
|
|
if ( (syscall(SYS_thr_self, &tid)) < 0 ) // Thread/Process id
|
|
|
|
#else
|
|
|
|
tid=syscall(SYS_gettid);
|
|
|
|
#endif
|
2015-02-24 22:22:08 +08:00
|
|
|
#endif
|
2016-04-04 22:11:48 +08:00
|
|
|
return tid;
|
|
|
|
}
|
2015-05-18 08:18:54 +08:00
|
|
|
#else
|
2016-04-04 22:11:48 +08:00
|
|
|
pthread_t pid() { return( pthread_self() ); }
|
2015-05-18 08:18:54 +08:00
|
|
|
#endif
|
2016-04-04 22:11:48 +08:00
|
|
|
public:
|
2018-04-12 23:29:35 +08:00
|
|
|
explicit TimerException( const std::string &message ) : Exception( stringtf("(%d) ", (long int)pid())+message ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TimerThread : public Thread
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
typedef ThreadData<bool> ExpiryFlag;
|
|
|
|
|
|
|
|
private:
|
|
|
|
static int mNextTimerId;
|
|
|
|
|
|
|
|
private:
|
|
|
|
int mTimerId;
|
|
|
|
Timer &mTimer;
|
|
|
|
int mDuration;
|
|
|
|
int mRepeat;
|
|
|
|
int mReset;
|
|
|
|
ExpiryFlag mExpiryFlag;
|
|
|
|
Mutex mAccessMutex;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void quit()
|
2008-07-16 16:35:59 +08:00
|
|
|
{
|
2016-04-04 22:11:48 +08:00
|
|
|
cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
TimerThread( Timer &timer, int timeout, bool repeat );
|
|
|
|
~TimerThread();
|
|
|
|
|
|
|
|
void cancel();
|
|
|
|
void reset();
|
|
|
|
int run();
|
|
|
|
};
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
protected:
|
2016-04-04 22:11:48 +08:00
|
|
|
TimerThread mTimerThread;
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
protected:
|
2016-04-04 22:11:48 +08:00
|
|
|
Timer( int timeout, bool repeat=false );
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
public:
|
2016-04-04 22:11:48 +08:00
|
|
|
virtual ~Timer();
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
protected:
|
2016-04-04 22:11:48 +08:00
|
|
|
virtual void expire()=0;
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
public:
|
2016-04-04 22:11:48 +08:00
|
|
|
void cancel();
|
|
|
|
void reset();
|
2008-07-16 16:35:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // ZM_TIMER_H
|