forked from ZoneMinder/zoneminder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzm_thread.cpp
More file actions
339 lines (304 loc) · 9.17 KB
/
Copy pathzm_thread.cpp
File metadata and controls
339 lines (304 loc) · 9.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//
// ZoneMinder Thread Class Implementation, $Date$, $Revision$
// Copyright (C) 2001-2008 Philip Coombes
//
// 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
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
#include "zm_thread.h"
#include "zm_logger.h"
#include "zm_utils.h"
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/time.h>
struct timespec getTimeout( int secs )
{
struct timespec timeout;
struct timeval temp_timeout;
gettimeofday( &temp_timeout, 0 );
timeout.tv_sec = temp_timeout.tv_sec + secs;
timeout.tv_nsec = temp_timeout.tv_usec*1000;
return( timeout );
}
struct timespec getTimeout( double secs )
{
struct timespec timeout;
struct timeval temp_timeout;
gettimeofday( &temp_timeout, 0 );
timeout.tv_sec = temp_timeout.tv_sec + int(secs);
timeout.tv_nsec = temp_timeout.tv_usec += (long int)(1000000000.0*(secs-int(secs)));
if ( timeout.tv_nsec > 1000000000 )
{
timeout.tv_sec += 1;
timeout.tv_nsec -= 1000000000;
}
return( timeout );
}
Mutex::Mutex()
{
if ( pthread_mutex_init( &mMutex, NULL ) < 0 )
throw ThreadException( stringtf( "Unable to create pthread mutex: %s", strerror(errno) ) );
}
Mutex::~Mutex()
{
if ( locked() )
Warning( "Destroying mutex when locked" );
if ( pthread_mutex_destroy( &mMutex ) < 0 )
throw ThreadException( stringtf( "Unable to destroy pthread mutex: %s", strerror(errno) ) );
}
void Mutex::lock()
{
if ( pthread_mutex_lock( &mMutex ) < 0 )
throw ThreadException( stringtf( "Unable to lock pthread mutex: %s", strerror(errno) ) );
}
void Mutex::lock( int secs )
{
struct timespec timeout = getTimeout( secs );
if ( pthread_mutex_timedlock( &mMutex, &timeout ) < 0 )
throw ThreadException( stringtf( "Unable to timedlock pthread mutex: %s", strerror(errno) ) );
}
void Mutex::lock( double secs )
{
struct timespec timeout = getTimeout( secs );
if ( pthread_mutex_timedlock( &mMutex, &timeout ) < 0 )
throw ThreadException( stringtf( "Unable to timedlock pthread mutex: %s", strerror(errno) ) );
}
void Mutex::unlock()
{
if ( pthread_mutex_unlock( &mMutex ) < 0 )
throw ThreadException( stringtf( "Unable to unlock pthread mutex: %s", strerror(errno) ) );
}
bool Mutex::locked()
{
int state = pthread_mutex_trylock( &mMutex );
if ( state != 0 && state != EBUSY )
throw ThreadException( stringtf( "Unable to trylock pthread mutex: %s", strerror(errno) ) );
if ( state != EBUSY )
unlock();
return( state == EBUSY );
}
Condition::Condition( Mutex &mutex ) : mMutex( mutex )
{
if ( pthread_cond_init( &mCondition, NULL ) < 0 )
throw ThreadException( stringtf( "Unable to create pthread condition: %s", strerror(errno) ) );
}
Condition::~Condition()
{
if ( pthread_cond_destroy( &mCondition ) < 0 )
throw ThreadException( stringtf( "Unable to destroy pthread condition: %s", strerror(errno) ) );
}
void Condition::wait()
{
// Locking done outside of this function
if ( pthread_cond_wait( &mCondition, mMutex.getMutex() ) < 0 )
throw ThreadException( stringtf( "Unable to wait pthread condition: %s", strerror(errno) ) );
}
bool Condition::wait( int secs )
{
// Locking done outside of this function
Debug( 8, "Waiting for %d seconds", secs );
struct timespec timeout = getTimeout( secs );
if ( pthread_cond_timedwait( &mCondition, mMutex.getMutex(), &timeout ) < 0 && errno != ETIMEDOUT )
throw ThreadException( stringtf( "Unable to timedwait pthread condition: %s", strerror(errno) ) );
return( errno != ETIMEDOUT );
}
bool Condition::wait( double secs )
{
// Locking done outside of this function
struct timespec timeout = getTimeout( secs );
if ( pthread_cond_timedwait( &mCondition, mMutex.getMutex(), &timeout ) < 0 && errno != ETIMEDOUT )
throw ThreadException( stringtf( "Unable to timedwait pthread condition: %s", strerror(errno) ) );
return( errno != ETIMEDOUT );
}
void Condition::signal()
{
if ( pthread_cond_signal( &mCondition ) < 0 )
throw ThreadException( stringtf( "Unable to signal pthread condition: %s", strerror(errno) ) );
}
void Condition::broadcast()
{
if ( pthread_cond_broadcast( &mCondition ) < 0 )
throw ThreadException( stringtf( "Unable to broadcast pthread condition: %s", strerror(errno) ) );
}
template <class T> const T ThreadData<T>::getValue() const
{
mMutex.lock();
const T valueCopy = mValue;
mMutex.unlock();
return( valueCopy );
}
template <class T> T ThreadData<T>::setValue( const T value )
{
mMutex.lock();
const T valueCopy = mValue = value;
mMutex.unlock();
return( valueCopy );
}
template <class T> const T ThreadData<T>::getUpdatedValue() const
{
Debug( 8, "Waiting for value update, %p", this );
mMutex.lock();
mChanged = false;
//do {
mCondition.wait();
//} while ( !mChanged );
const T valueCopy = mValue;
mMutex.unlock();
Debug( 9, "Got value update, %p", this );
return( valueCopy );
}
template <class T> const T ThreadData<T>::getUpdatedValue( double secs ) const
{
Debug( 8, "Waiting for value update, %.2f secs, %p", secs, this );
mMutex.lock();
mChanged = false;
//do {
mCondition.wait( secs );
//} while ( !mChanged );
const T valueCopy = mValue;
mMutex.unlock();
Debug( 9, "Got value update, %p", this );
return( valueCopy );
}
template <class T> const T ThreadData<T>::getUpdatedValue( int secs ) const
{
Debug( 8, "Waiting for value update, %d secs, %p", secs, this );
mMutex.lock();
mChanged = false;
//do {
mCondition.wait( secs );
//} while ( !mChanged );
const T valueCopy = mValue;
mMutex.unlock();
Debug( 9, "Got value update, %p", this );
return( valueCopy );
}
template <class T> void ThreadData<T>::updateValueSignal( const T value )
{
Debug( 8, "Updating value with signal, %p", this );
mMutex.lock();
mValue = value;
mChanged = true;
mCondition.signal();
mMutex.unlock();
Debug( 9, "Updated value, %p", this );
}
template <class T> void ThreadData<T>::updateValueBroadcast( const T value )
{
Debug( 8, "Updating value with broadcast, %p", this );
mMutex.lock();
mValue = value;
mChanged = true;
mCondition.broadcast();
mMutex.unlock();
Debug( 9, "Updated value, %p", this );
}
Thread::Thread() :
mThreadCondition( mThreadMutex ),
mPid( -1 ),
mStarted( false ),
mRunning( false )
{
Debug( 1, "Creating thread" );
}
Thread::~Thread()
{
Debug( 1, "Destroying thread %d", mPid );
if ( mStarted )
join();
}
void *Thread::mThreadFunc( void *arg )
{
Debug( 2, "Invoking thread" );
Thread *thisPtr = (Thread *)arg;
thisPtr->status = 0;
try
{
thisPtr->mThreadMutex.lock();
thisPtr->mPid = thisPtr->id();
thisPtr->mThreadCondition.signal();
thisPtr->mThreadMutex.unlock();
thisPtr->mRunning = true;
thisPtr->status = thisPtr->run();
thisPtr->mRunning = false;
Debug( 2, "Exiting thread, status %p", (void *)&(thisPtr->status) );
return (void *)&(thisPtr->status);
}
catch ( const ThreadException &e )
{
Error( "%s", e.getMessage().c_str() );
thisPtr->mRunning = false;
Debug( 2, "Exiting thread after exception, status %p", (void *)-1 );
return (void *)-1;
}
}
void Thread::start()
{
Debug( 1, "Starting thread" );
if ( isThread() )
throw ThreadException( "Can't self start thread" );
mThreadMutex.lock();
if ( !mStarted )
{
pthread_attr_t threadAttrs;
pthread_attr_init( &threadAttrs );
pthread_attr_setscope( &threadAttrs, PTHREAD_SCOPE_SYSTEM );
mStarted = true;
if ( pthread_create( &mThread, &threadAttrs, mThreadFunc, this ) < 0 )
throw ThreadException( stringtf( "Can't create thread: %s", strerror(errno) ) );
pthread_attr_destroy( &threadAttrs );
}
else
{
Error( "Attempt to start already running thread %d", mPid );
}
mThreadCondition.wait();
mThreadMutex.unlock();
Debug( 1, "Started thread %d", mPid );
}
void Thread::join()
{
Debug( 1, "Joining thread %d", mPid );
if ( isThread() )
throw ThreadException( "Can't self join thread" );
mThreadMutex.lock();
if ( mPid >= 0 )
{
if ( mStarted )
{
void *threadStatus = 0;
if ( pthread_join( mThread, &threadStatus ) < 0 )
throw ThreadException( stringtf( "Can't join sender thread: %s", strerror(errno) ) );
mStarted = false;
Debug( 1, "Thread %d exited, status %p", mPid, threadStatus );
}
else
{
Warning( "Attempt to join already finished thread %d", mPid );
}
}
else
{
Warning( "Attempt to join non-started thread %d", mPid );
}
mThreadMutex.unlock();
Debug( 1, "Joined thread %d", mPid );
}
void Thread::kill( int signal )
{
pthread_kill( mThread, signal );
}
// Some explicit template instantiations
#include "zm_threaddata.cpp"