2008-07-16 16:35:59 +08:00
|
|
|
//
|
2008-07-25 17:08:15 +08:00
|
|
|
// ZoneMinder Thread 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_THREAD_H
|
|
|
|
#define ZM_THREAD_H
|
|
|
|
|
2018-11-24 01:46:10 +08:00
|
|
|
class RecursiveMutex;
|
|
|
|
|
|
|
|
|
|
|
|
#include "zm_config.h"
|
2012-07-17 17:23:13 +08:00
|
|
|
#include <unistd.h>
|
2008-07-16 16:35:59 +08:00
|
|
|
#include <pthread.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_exception.h"
|
|
|
|
#include "zm_utils.h"
|
2015-02-24 22:22:08 +08:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
#include <sys/thr.h>
|
|
|
|
#endif
|
2008-07-16 16:35:59 +08:00
|
|
|
|
2017-11-19 05:00:10 +08:00
|
|
|
class ThreadException : public Exception {
|
2015-02-24 22:22:08 +08:00
|
|
|
private:
|
2015-05-18 08:18:54 +08:00
|
|
|
#ifndef SOLARIS
|
2016-04-04 22:11:48 +08:00
|
|
|
pid_t pid() {
|
2015-04-21 00:53:02 +08:00
|
|
|
pid_t tid;
|
2015-02-24 22:22:08 +08:00
|
|
|
#ifdef __FreeBSD__
|
2015-04-21 00:53:02 +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
|
2015-04-21 00:53:02 +08:00
|
|
|
return tid;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
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
|
2008-07-16 16:35:59 +08:00
|
|
|
public:
|
2020-05-08 01:44:32 +08:00
|
|
|
explicit ThreadException(const std::string &message) :
|
|
|
|
Exception(stringtf("(%d) ", (long int)pid())+message)
|
|
|
|
{
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2008-07-16 16:35:59 +08:00
|
|
|
};
|
|
|
|
|
2017-11-19 05:00:10 +08:00
|
|
|
class Mutex {
|
2018-06-01 23:27:35 +08:00
|
|
|
friend class Condition;
|
2008-07-16 16:35:59 +08:00
|
|
|
|
2018-06-01 23:27:35 +08:00
|
|
|
private:
|
|
|
|
pthread_mutex_t mMutex;
|
2008-07-16 16:35:59 +08:00
|
|
|
|
2018-06-01 23:27:35 +08:00
|
|
|
public:
|
|
|
|
Mutex();
|
|
|
|
~Mutex();
|
2008-07-16 16:35:59 +08:00
|
|
|
|
2018-06-01 23:27:35 +08:00
|
|
|
private:
|
|
|
|
pthread_mutex_t *getMutex() {
|
|
|
|
return &mMutex;
|
|
|
|
}
|
2008-07-16 16:35:59 +08:00
|
|
|
|
2018-06-01 23:27:35 +08:00
|
|
|
public:
|
|
|
|
int trylock();
|
|
|
|
void lock();
|
|
|
|
void lock( int secs );
|
|
|
|
void lock( double secs );
|
|
|
|
void unlock();
|
|
|
|
bool locked();
|
|
|
|
};
|
|
|
|
|
|
|
|
class RecursiveMutex : public Mutex {
|
|
|
|
private:
|
|
|
|
pthread_mutex_t mMutex;
|
|
|
|
public:
|
|
|
|
RecursiveMutex();
|
2008-07-16 16:35:59 +08:00
|
|
|
};
|
|
|
|
|
2017-11-19 05:00:10 +08:00
|
|
|
class ScopedMutex {
|
2008-07-16 16:35:59 +08:00
|
|
|
private:
|
2016-04-04 22:11:48 +08:00
|
|
|
Mutex &mMutex;
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
public:
|
2017-11-19 05:00:10 +08:00
|
|
|
explicit ScopedMutex( Mutex &mutex ) : mMutex( mutex ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
mMutex.lock();
|
|
|
|
}
|
2017-11-19 05:00:10 +08:00
|
|
|
~ScopedMutex() {
|
2016-04-04 22:11:48 +08:00
|
|
|
mMutex.unlock();
|
|
|
|
}
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
private:
|
2016-04-04 22:11:48 +08:00
|
|
|
ScopedMutex( const ScopedMutex & );
|
2008-07-16 16:35:59 +08:00
|
|
|
};
|
|
|
|
|
2018-04-12 23:29:35 +08:00
|
|
|
class Condition {
|
2008-07-16 16:35:59 +08:00
|
|
|
private:
|
2016-04-04 22:11:48 +08:00
|
|
|
Mutex &mMutex;
|
|
|
|
pthread_cond_t mCondition;
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
public:
|
2018-04-12 23:29:35 +08:00
|
|
|
explicit Condition(Mutex &mutex);
|
2016-04-04 22:11:48 +08:00
|
|
|
~Condition();
|
2008-07-16 16:35:59 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
void wait();
|
2018-04-12 23:29:35 +08:00
|
|
|
bool wait(int secs);
|
|
|
|
bool wait(double secs);
|
2016-04-04 22:11:48 +08:00
|
|
|
void signal();
|
|
|
|
void broadcast();
|
2008-07-16 16:35:59 +08:00
|
|
|
};
|
|
|
|
|
2018-04-12 23:29:35 +08:00
|
|
|
class Semaphore : public Condition {
|
2008-07-16 16:35:59 +08:00
|
|
|
private:
|
2016-04-04 22:11:48 +08:00
|
|
|
Mutex mMutex;
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
public:
|
2018-04-12 23:29:35 +08:00
|
|
|
Semaphore() : Condition(mMutex) {
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2008-07-16 16:35:59 +08:00
|
|
|
|
2018-04-12 23:29:35 +08:00
|
|
|
void wait() {
|
2016-04-04 22:11:48 +08:00
|
|
|
mMutex.lock();
|
|
|
|
Condition::wait();
|
|
|
|
mMutex.unlock();
|
|
|
|
}
|
2018-04-12 23:29:35 +08:00
|
|
|
bool wait(int secs) {
|
2016-04-04 22:11:48 +08:00
|
|
|
mMutex.lock();
|
2018-04-12 23:29:35 +08:00
|
|
|
bool result = Condition::wait(secs);
|
2016-04-04 22:11:48 +08:00
|
|
|
mMutex.unlock();
|
2018-04-12 23:29:35 +08:00
|
|
|
return result;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2018-04-12 23:29:35 +08:00
|
|
|
bool wait(double secs) {
|
2016-04-04 22:11:48 +08:00
|
|
|
mMutex.lock();
|
2018-04-12 23:29:35 +08:00
|
|
|
bool result = Condition::wait(secs);
|
2016-04-04 22:11:48 +08:00
|
|
|
mMutex.unlock();
|
2018-04-12 23:29:35 +08:00
|
|
|
return result;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2018-04-12 23:29:35 +08:00
|
|
|
void signal() {
|
2016-04-04 22:11:48 +08:00
|
|
|
mMutex.lock();
|
|
|
|
Condition::signal();
|
|
|
|
mMutex.unlock();
|
|
|
|
}
|
2018-04-12 23:29:35 +08:00
|
|
|
void broadcast() {
|
2016-04-04 22:11:48 +08:00
|
|
|
mMutex.lock();
|
|
|
|
Condition::broadcast();
|
|
|
|
mMutex.unlock();
|
|
|
|
}
|
2008-07-16 16:35:59 +08:00
|
|
|
};
|
|
|
|
|
2017-11-19 05:00:10 +08:00
|
|
|
template <class T> class ThreadData {
|
2008-07-16 16:35:59 +08:00
|
|
|
private:
|
2016-04-04 22:11:48 +08:00
|
|
|
T mValue;
|
|
|
|
mutable bool mChanged;
|
|
|
|
mutable Mutex mMutex;
|
|
|
|
mutable Condition mCondition;
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
public:
|
2020-05-08 01:44:32 +08:00
|
|
|
__attribute__((used)) ThreadData() :
|
|
|
|
mValue(0), mCondition(mMutex)
|
|
|
|
{
|
2017-11-19 05:00:10 +08:00
|
|
|
mChanged = false;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2020-05-08 01:44:32 +08:00
|
|
|
explicit __attribute__((used)) ThreadData(T value) :
|
|
|
|
mValue(value), mCondition(mMutex) {
|
2017-11-19 05:00:10 +08:00
|
|
|
mChanged = false;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2008-07-16 16:35:59 +08:00
|
|
|
|
2020-05-08 01:44:32 +08:00
|
|
|
__attribute__((used)) operator T() const {
|
|
|
|
return getValue();
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2020-05-08 01:44:32 +08:00
|
|
|
__attribute__((used)) const T operator=( const T value ) {
|
|
|
|
return setValue(value);
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2008-07-16 16:35:59 +08:00
|
|
|
|
2020-05-08 01:44:32 +08:00
|
|
|
__attribute__((used)) const T getValueImmediate() const {
|
|
|
|
return mValue;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2020-05-08 01:44:32 +08:00
|
|
|
__attribute__((used)) T setValueImmediate( const T value ) {
|
|
|
|
return mValue = value;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
|
|
|
__attribute__((used)) const T getValue() const;
|
|
|
|
__attribute__((used)) T setValue( const T value );
|
|
|
|
__attribute__((used)) const T getUpdatedValue() const;
|
2020-05-08 01:44:32 +08:00
|
|
|
__attribute__((used)) const T getUpdatedValue(double secs) const;
|
|
|
|
__attribute__((used)) const T getUpdatedValue(int secs) const;
|
|
|
|
__attribute__((used)) void updateValueSignal(const T value);
|
|
|
|
__attribute__((used)) void updateValueBroadcast(const T value);
|
2008-07-16 16:35:59 +08:00
|
|
|
};
|
|
|
|
|
2018-04-12 23:29:35 +08:00
|
|
|
class Thread {
|
2008-07-16 16:35:59 +08:00
|
|
|
public:
|
2016-04-04 22:11:48 +08:00
|
|
|
typedef void *(*ThreadFunc)( void * );
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
protected:
|
2016-04-04 22:11:48 +08:00
|
|
|
pthread_t mThread;
|
2008-07-16 16:35:59 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
Mutex mThreadMutex;
|
|
|
|
Condition mThreadCondition;
|
2015-05-18 08:18:54 +08:00
|
|
|
#ifndef SOLARIS
|
2016-04-04 22:11:48 +08:00
|
|
|
pid_t mPid;
|
2015-05-18 08:18:54 +08:00
|
|
|
#else
|
2016-04-04 22:11:48 +08:00
|
|
|
pthread_t mPid;
|
2015-05-18 08:18:54 +08:00
|
|
|
#endif
|
2016-04-04 22:11:48 +08:00
|
|
|
bool mStarted;
|
|
|
|
bool mRunning;
|
2016-12-29 17:31:05 +08:00
|
|
|
int status; // Used in various functions to get around return a local variable
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
protected:
|
2016-04-04 22:11:48 +08:00
|
|
|
Thread();
|
|
|
|
virtual ~Thread();
|
2008-07-16 16:35:59 +08:00
|
|
|
|
2015-05-18 08:18:54 +08:00
|
|
|
#ifndef SOLARIS
|
2018-04-12 23:29:35 +08:00
|
|
|
pid_t id() const {
|
2016-04-04 22:11:48 +08:00
|
|
|
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
|
2015-04-21 00:53:02 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
#else
|
|
|
|
tid=syscall(SYS_gettid);
|
|
|
|
#endif
|
2015-02-24 22:22:08 +08:00
|
|
|
#endif
|
2018-04-12 23:29:35 +08:00
|
|
|
return tid;
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2015-05-18 08:18:54 +08:00
|
|
|
#else
|
2018-04-12 23:29:35 +08:00
|
|
|
pthread_t id() const {
|
|
|
|
return pthread_self();
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2015-05-18 08:18:54 +08:00
|
|
|
#endif
|
2018-04-12 23:29:35 +08:00
|
|
|
void exit( int p_status = 0 ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
//INFO( "Exiting" );
|
2016-04-28 21:20:56 +08:00
|
|
|
pthread_exit( (void *)&p_status );
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
|
|
|
static void *mThreadFunc( void *arg );
|
2008-07-16 16:35:59 +08:00
|
|
|
|
|
|
|
public:
|
2016-04-04 22:11:48 +08:00
|
|
|
virtual int run() = 0;
|
2008-07-16 16:35:59 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
void start();
|
|
|
|
void join();
|
|
|
|
void kill( int signal );
|
2018-04-12 23:29:35 +08:00
|
|
|
bool isThread() {
|
2016-04-04 22:11:48 +08:00
|
|
|
return( mPid > -1 && pthread_equal( pthread_self(), mThread ) );
|
|
|
|
}
|
2018-04-12 23:29:35 +08:00
|
|
|
bool isStarted() const { return mStarted; }
|
|
|
|
bool isRunning() const { return mRunning; }
|
2008-07-16 16:35:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // ZM_THREAD_H
|