~ubuntu-branches/ubuntu/trusty/zoneminder/trusty

« back to all changes in this revision

Viewing changes to .pc/Fix-FTBFS-with-gcc-4.7/src/zm_thread.h

  • Committer: Package Import Robot
  • Author(s): gregor herrmann
  • Date: 2012-05-13 17:02:21 UTC
  • mfrom: (15.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20120513170221-1ywdwo2eavhda0cn
Tags: 1.25.0-1.1
* Non-maintainer upload.
* Fix "ftbfs with GCC-4.7": add patch Fix-FTBFS-with-gcc-4.7 from Cyril
  Brulebois: fix missing <unistd.h> includes.
  (Closes: #667428)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
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
 
4
// 
 
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.
 
9
// 
 
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.
 
14
// 
 
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.
 
18
// 
 
19
 
 
20
#ifndef ZM_THREAD_H
 
21
#define ZM_THREAD_H
 
22
 
 
23
#include <pthread.h>
 
24
#include "zm_exception.h"
 
25
#include "zm_utils.h"
 
26
 
 
27
class ThreadException : public Exception
 
28
{
 
29
public:
 
30
    ThreadException( const std::string &message ) : Exception( stringtf( "(%d) "+message, (long int)syscall(224) ) )
 
31
    {
 
32
    }
 
33
};
 
34
 
 
35
class Mutex
 
36
{
 
37
friend class Condition;
 
38
 
 
39
private:
 
40
    pthread_mutex_t mMutex;
 
41
 
 
42
public:
 
43
    Mutex();
 
44
    ~Mutex();
 
45
 
 
46
private:
 
47
    pthread_mutex_t *getMutex()
 
48
    {
 
49
        return( &mMutex );
 
50
    }
 
51
 
 
52
public:
 
53
    void lock();
 
54
    void lock( int secs );
 
55
    void lock( double secs );
 
56
    void unlock();
 
57
    bool locked();
 
58
};
 
59
 
 
60
class ScopedMutex
 
61
{
 
62
private:
 
63
    Mutex &mMutex;
 
64
 
 
65
public:
 
66
    ScopedMutex( Mutex &mutex ) : mMutex( mutex )
 
67
    {
 
68
        mMutex.lock();
 
69
    }
 
70
    ~ScopedMutex()
 
71
    {
 
72
        mMutex.unlock();
 
73
    }
 
74
 
 
75
private:
 
76
    ScopedMutex( const ScopedMutex & );
 
77
};
 
78
 
 
79
class Condition
 
80
{
 
81
private:
 
82
    Mutex &mMutex;
 
83
    pthread_cond_t mCondition;
 
84
 
 
85
public:
 
86
    Condition( Mutex &mutex );
 
87
    ~Condition();
 
88
 
 
89
    void wait();
 
90
    bool wait( int secs );
 
91
    bool wait( double secs );
 
92
    void signal();
 
93
    void broadcast();
 
94
};
 
95
 
 
96
class Semaphore : public Condition
 
97
{
 
98
private:
 
99
    Mutex mMutex;
 
100
 
 
101
public:
 
102
    Semaphore() : Condition( mMutex )
 
103
    {
 
104
    }
 
105
 
 
106
    void wait()
 
107
    {
 
108
        mMutex.lock();
 
109
        Condition::wait();
 
110
        mMutex.unlock();
 
111
    }
 
112
    bool wait( int secs )
 
113
    {
 
114
        mMutex.lock();
 
115
        bool result = Condition::wait( secs );
 
116
        mMutex.unlock();
 
117
        return( result );
 
118
    }
 
119
    bool wait( double secs )
 
120
    {
 
121
        mMutex.lock();
 
122
        bool result = Condition::wait( secs );
 
123
        mMutex.unlock();
 
124
        return( result );
 
125
    }
 
126
    void signal()
 
127
    {
 
128
        mMutex.lock();
 
129
        Condition::signal();
 
130
        mMutex.unlock();
 
131
    }
 
132
    void broadcast()
 
133
    {
 
134
        mMutex.lock();
 
135
        Condition::broadcast();
 
136
        mMutex.unlock();
 
137
    }
 
138
};
 
139
 
 
140
template <class T> class ThreadData
 
141
{
 
142
private:
 
143
    T mValue;
 
144
    mutable bool mChanged;
 
145
    mutable Mutex mMutex;
 
146
    mutable Condition mCondition;
 
147
 
 
148
public:
 
149
    ThreadData() : mCondition( mMutex )
 
150
    {
 
151
    }
 
152
    ThreadData( T value ) : mValue( value ), mCondition( mMutex )
 
153
    {
 
154
    }
 
155
    //~ThreadData() {}
 
156
 
 
157
    operator T() const
 
158
    {
 
159
        return( getValue() );
 
160
    }
 
161
    const T operator=( const T value )
 
162
    {
 
163
        return( setValue( value ) );
 
164
    }
 
165
 
 
166
    const T getValueImmediate() const
 
167
    {
 
168
        return( mValue );
 
169
    }
 
170
    T setValueImmediate( const T value )
 
171
    {
 
172
        return( mValue = value );
 
173
    }
 
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 );
 
181
};
 
182
 
 
183
class Thread
 
184
{
 
185
public:
 
186
    typedef void *(*ThreadFunc)( void * );
 
187
 
 
188
protected:
 
189
    pthread_t mThread;
 
190
 
 
191
    Mutex mThreadMutex;
 
192
    Condition mThreadCondition;
 
193
    pid_t mPid;
 
194
    bool  mStarted;
 
195
    bool  mRunning;
 
196
 
 
197
protected:
 
198
    Thread();
 
199
    virtual ~Thread();
 
200
 
 
201
    pid_t id() const
 
202
    {
 
203
        return( (pid_t)syscall(224) );
 
204
    }
 
205
    void exit( int status = 0 )
 
206
    {
 
207
        //INFO( "Exiting" );
 
208
        pthread_exit( (void *)status );
 
209
    }
 
210
    static void *mThreadFunc( void *arg );
 
211
 
 
212
public:
 
213
    virtual int run() = 0;
 
214
 
 
215
    void start();
 
216
    void join();
 
217
    void kill( int signal );
 
218
    bool isThread()
 
219
    {
 
220
        return( mPid > -1 && pthread_equal( pthread_self(), mThread ) );
 
221
    }
 
222
    bool isStarted() const { return( mStarted ); }
 
223
    bool isRunning() const { return( mRunning ); }
 
224
};
 
225
 
 
226
#endif // ZM_THREAD_H