2
// ZoneMinder Thread Class Interface, $Date: 2009-06-03 09:10:02 +0100 (Wed, 03 Jun 2009) $, $Revision: 2906 $
3
// Copyright (C) 2001-2008 Philip Coombes
5
// This program is free software; you can redistribute it and/or
6
// modify it under the terms of the GNU General Public License
7
// as published by the Free Software Foundation; either version 2
8
// of the License, or (at your option) any later version.
10
// This program is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
// GNU General Public License for more details.
15
// You should have received a copy of the GNU General Public License
16
// along with this program; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
#include "zm_exception.h"
27
class ThreadException : public Exception
30
ThreadException( const std::string &message ) : Exception( stringtf( "(%d) "+message, (long int)syscall(224) ) )
37
friend class Condition;
40
pthread_mutex_t mMutex;
47
pthread_mutex_t *getMutex()
54
void lock( int secs );
55
void lock( double secs );
66
ScopedMutex( Mutex &mutex ) : mMutex( mutex )
76
ScopedMutex( const ScopedMutex & );
83
pthread_cond_t mCondition;
86
Condition( Mutex &mutex );
90
bool wait( int secs );
91
bool wait( double secs );
96
class Semaphore : public Condition
102
Semaphore() : Condition( mMutex )
112
bool wait( int secs )
115
bool result = Condition::wait( secs );
119
bool wait( double secs )
122
bool result = Condition::wait( secs );
135
Condition::broadcast();
140
template <class T> class ThreadData
144
mutable bool mChanged;
145
mutable Mutex mMutex;
146
mutable Condition mCondition;
149
ThreadData() : mCondition( mMutex )
152
ThreadData( T value ) : mValue( value ), mCondition( mMutex )
159
return( getValue() );
161
const T operator=( const T value )
163
return( setValue( value ) );
166
const T getValueImmediate() const
170
T setValueImmediate( const T value )
172
return( mValue = value );
174
const T getValue() const;
175
T setValue( const T value );
176
const T getUpdatedValue() const;
177
const T getUpdatedValue( double secs ) const;
178
const T getUpdatedValue( int secs ) const;
179
void updateValueSignal( const T value );
180
void updateValueBroadcast( const T value );
186
typedef void *(*ThreadFunc)( void * );
192
Condition mThreadCondition;
203
return( (pid_t)syscall(224) );
205
void exit( int status = 0 )
208
pthread_exit( (void *)status );
210
static void *mThreadFunc( void *arg );
213
virtual int run() = 0;
217
void kill( int signal );
220
return( mPid > -1 && pthread_equal( pthread_self(), mThread ) );
222
bool isStarted() const { return( mStarted ); }
223
bool isRunning() const { return( mRunning ); }
226
#endif // ZM_THREAD_H