-rw-r--r-- | noncore/multimedia/opieplayer2/threadutil.cpp | 6 | ||||
-rw-r--r-- | noncore/multimedia/opieplayer2/threadutil.h | 2 |
2 files changed, 8 insertions, 0 deletions
diff --git a/noncore/multimedia/opieplayer2/threadutil.cpp b/noncore/multimedia/opieplayer2/threadutil.cpp index 5687f42..a5cc92d 100644 --- a/noncore/multimedia/opieplayer2/threadutil.cpp +++ b/noncore/multimedia/opieplayer2/threadutil.cpp | |||
@@ -171,96 +171,102 @@ Thread::Thread() | |||
171 | { | 171 | { |
172 | } | 172 | } |
173 | 173 | ||
174 | Thread::~Thread() | 174 | Thread::~Thread() |
175 | { | 175 | { |
176 | assert( d->isRunning == false ); | 176 | assert( d->isRunning == false ); |
177 | delete d; | 177 | delete d; |
178 | } | 178 | } |
179 | 179 | ||
180 | void Thread::start() | 180 | void Thread::start() |
181 | { | 181 | { |
182 | AutoLock lock( d->guard ); | 182 | AutoLock lock( d->guard ); |
183 | 183 | ||
184 | if ( d->isRunning ) { | 184 | if ( d->isRunning ) { |
185 | qDebug( "ThreadUtil::Thread::start() called for running thread." ); | 185 | qDebug( "ThreadUtil::Thread::start() called for running thread." ); |
186 | return; | 186 | return; |
187 | } | 187 | } |
188 | 188 | ||
189 | pthread_attr_t attributes; | 189 | pthread_attr_t attributes; |
190 | pthread_attr_init( &attributes ); | 190 | pthread_attr_init( &attributes ); |
191 | pthread_attr_setscope( &attributes, PTHREAD_SCOPE_SYSTEM ); | 191 | pthread_attr_setscope( &attributes, PTHREAD_SCOPE_SYSTEM ); |
192 | int err = pthread_create( &d->self, &attributes, _threadutil_start_thread, ( void* )this ); | 192 | int err = pthread_create( &d->self, &attributes, _threadutil_start_thread, ( void* )this ); |
193 | if ( err != 0 ) { | 193 | if ( err != 0 ) { |
194 | qDebug( "ThreadUtil::Thread::start() : can't create thread: %s", strerror( err ) ); | 194 | qDebug( "ThreadUtil::Thread::start() : can't create thread: %s", strerror( err ) ); |
195 | pthread_attr_destroy( &attributes ); | 195 | pthread_attr_destroy( &attributes ); |
196 | return; | 196 | return; |
197 | } | 197 | } |
198 | pthread_attr_destroy( &attributes ); | 198 | pthread_attr_destroy( &attributes ); |
199 | } | 199 | } |
200 | 200 | ||
201 | void Thread::terminate() | 201 | void Thread::terminate() |
202 | { | 202 | { |
203 | AutoLock lock( d->guard ); | 203 | AutoLock lock( d->guard ); |
204 | if ( !d->isRunning ) | 204 | if ( !d->isRunning ) |
205 | return; | 205 | return; |
206 | 206 | ||
207 | pthread_cancel( d->self ); | 207 | pthread_cancel( d->self ); |
208 | } | 208 | } |
209 | 209 | ||
210 | bool Thread::wait() | 210 | bool Thread::wait() |
211 | { | 211 | { |
212 | AutoLock lock( d->guard ); | 212 | AutoLock lock( d->guard ); |
213 | if ( !d->isRunning ) | 213 | if ( !d->isRunning ) |
214 | return true; | 214 | return true; |
215 | 215 | ||
216 | return d->finishCondition.wait( d->guard ); | 216 | return d->finishCondition.wait( d->guard ); |
217 | } | 217 | } |
218 | 218 | ||
219 | bool Thread::isRunning() const | ||
220 | { | ||
221 | AutoLock lock( d->guard ); | ||
222 | return d->isRunning; | ||
223 | } | ||
224 | |||
219 | void Thread::exit() | 225 | void Thread::exit() |
220 | { | 226 | { |
221 | pthread_exit( 0 ); | 227 | pthread_exit( 0 ); |
222 | } | 228 | } |
223 | 229 | ||
224 | OnewayNotifier::OnewayNotifier() | 230 | OnewayNotifier::OnewayNotifier() |
225 | { | 231 | { |
226 | int fds[ 2 ]; | 232 | int fds[ 2 ]; |
227 | pipe( fds ); | 233 | pipe( fds ); |
228 | m_readFd = fds[ 0 ]; | 234 | m_readFd = fds[ 0 ]; |
229 | m_writeFd = fds[ 1 ]; | 235 | m_writeFd = fds[ 1 ]; |
230 | 236 | ||
231 | m_notifier = new QSocketNotifier( m_readFd, QSocketNotifier::Read ); | 237 | m_notifier = new QSocketNotifier( m_readFd, QSocketNotifier::Read ); |
232 | connect( m_notifier, SIGNAL( activated( int ) ), | 238 | connect( m_notifier, SIGNAL( activated( int ) ), |
233 | this, SLOT( wakeUp() ) ); | 239 | this, SLOT( wakeUp() ) ); |
234 | } | 240 | } |
235 | 241 | ||
236 | OnewayNotifier::~OnewayNotifier() | 242 | OnewayNotifier::~OnewayNotifier() |
237 | { | 243 | { |
238 | delete m_notifier; | 244 | delete m_notifier; |
239 | 245 | ||
240 | ::close( m_readFd ); | 246 | ::close( m_readFd ); |
241 | ::close( m_writeFd ); | 247 | ::close( m_writeFd ); |
242 | } | 248 | } |
243 | 249 | ||
244 | void OnewayNotifier::notify() | 250 | void OnewayNotifier::notify() |
245 | { | 251 | { |
246 | const char c = 42; | 252 | const char c = 42; |
247 | ::write( m_writeFd, &c, 1 ); | 253 | ::write( m_writeFd, &c, 1 ); |
248 | } | 254 | } |
249 | 255 | ||
250 | void OnewayNotifier::wakeUp() | 256 | void OnewayNotifier::wakeUp() |
251 | { | 257 | { |
252 | char c = 0; | 258 | char c = 0; |
253 | 259 | ||
254 | if ( ::read( m_readFd, &c, 1 ) != 1 ) | 260 | if ( ::read( m_readFd, &c, 1 ) != 1 ) |
255 | return; | 261 | return; |
256 | 262 | ||
257 | emit awake(); | 263 | emit awake(); |
258 | } | 264 | } |
259 | 265 | ||
260 | ChannelMessage::ChannelMessage( int type ) | 266 | ChannelMessage::ChannelMessage( int type ) |
261 | : m_type( type ), m_isCall( false ), m_replied( false ), | 267 | : m_type( type ), m_isCall( false ), m_replied( false ), |
262 | m_inEventHandler( false ) | 268 | m_inEventHandler( false ) |
263 | { | 269 | { |
264 | } | 270 | } |
265 | 271 | ||
266 | ChannelMessage::~ChannelMessage() | 272 | ChannelMessage::~ChannelMessage() |
diff --git a/noncore/multimedia/opieplayer2/threadutil.h b/noncore/multimedia/opieplayer2/threadutil.h index b537cc1..b67b61d 100644 --- a/noncore/multimedia/opieplayer2/threadutil.h +++ b/noncore/multimedia/opieplayer2/threadutil.h | |||
@@ -55,96 +55,98 @@ namespace ThreadUtil | |||
55 | Mutex &operator=( const Mutex & ); | 55 | Mutex &operator=( const Mutex & ); |
56 | }; | 56 | }; |
57 | 57 | ||
58 | class AutoLock | 58 | class AutoLock |
59 | { | 59 | { |
60 | public: | 60 | public: |
61 | AutoLock( Mutex &mutex ) : m_mutex( mutex ) { m_mutex.lock(); } | 61 | AutoLock( Mutex &mutex ) : m_mutex( mutex ) { m_mutex.lock(); } |
62 | ~AutoLock() { m_mutex.unlock(); } | 62 | ~AutoLock() { m_mutex.unlock(); } |
63 | 63 | ||
64 | Mutex *operator &() const { return &m_mutex; } | 64 | Mutex *operator &() const { return &m_mutex; } |
65 | 65 | ||
66 | private: | 66 | private: |
67 | Mutex &m_mutex; | 67 | Mutex &m_mutex; |
68 | }; | 68 | }; |
69 | 69 | ||
70 | class WaitCondition | 70 | class WaitCondition |
71 | { | 71 | { |
72 | public: | 72 | public: |
73 | WaitCondition(); | 73 | WaitCondition(); |
74 | ~WaitCondition(); | 74 | ~WaitCondition(); |
75 | 75 | ||
76 | bool wait(); | 76 | bool wait(); |
77 | bool wait( Mutex &mutex ); | 77 | bool wait( Mutex &mutex ); |
78 | 78 | ||
79 | void wakeOne(); | 79 | void wakeOne(); |
80 | void wakeAll(); | 80 | void wakeAll(); |
81 | 81 | ||
82 | private: | 82 | private: |
83 | struct Data; | 83 | struct Data; |
84 | Data *d; | 84 | Data *d; |
85 | 85 | ||
86 | WaitCondition( const WaitCondition & ); | 86 | WaitCondition( const WaitCondition & ); |
87 | WaitCondition &operator=( const WaitCondition & ); | 87 | WaitCondition &operator=( const WaitCondition & ); |
88 | }; | 88 | }; |
89 | 89 | ||
90 | class Thread | 90 | class Thread |
91 | { | 91 | { |
92 | friend void *::_threadutil_start_thread( void* ); | 92 | friend void *::_threadutil_start_thread( void* ); |
93 | friend void ::_threadutil_terminate_thread( void* ); | 93 | friend void ::_threadutil_terminate_thread( void* ); |
94 | public: | 94 | public: |
95 | Thread(); | 95 | Thread(); |
96 | virtual ~Thread(); | 96 | virtual ~Thread(); |
97 | 97 | ||
98 | void start(); | 98 | void start(); |
99 | void terminate(); | 99 | void terminate(); |
100 | 100 | ||
101 | bool wait(); | 101 | bool wait(); |
102 | 102 | ||
103 | bool isRunning() const; | ||
104 | |||
103 | static void exit(); | 105 | static void exit(); |
104 | protected: | 106 | protected: |
105 | virtual void run() = 0; | 107 | virtual void run() = 0; |
106 | 108 | ||
107 | private: | 109 | private: |
108 | struct Data; | 110 | struct Data; |
109 | Data *d; | 111 | Data *d; |
110 | }; | 112 | }; |
111 | 113 | ||
112 | class OnewayNotifier : public QObject | 114 | class OnewayNotifier : public QObject |
113 | { | 115 | { |
114 | Q_OBJECT | 116 | Q_OBJECT |
115 | public: | 117 | public: |
116 | OnewayNotifier(); | 118 | OnewayNotifier(); |
117 | ~OnewayNotifier(); | 119 | ~OnewayNotifier(); |
118 | 120 | ||
119 | void notify(); | 121 | void notify(); |
120 | 122 | ||
121 | signals: | 123 | signals: |
122 | void awake(); | 124 | void awake(); |
123 | 125 | ||
124 | private slots: | 126 | private slots: |
125 | void wakeUp(); | 127 | void wakeUp(); |
126 | 128 | ||
127 | private: | 129 | private: |
128 | int m_readFd; | 130 | int m_readFd; |
129 | int m_writeFd; | 131 | int m_writeFd; |
130 | QSocketNotifier *m_notifier; | 132 | QSocketNotifier *m_notifier; |
131 | }; | 133 | }; |
132 | 134 | ||
133 | 135 | ||
134 | class Channel; | 136 | class Channel; |
135 | 137 | ||
136 | class ChannelMessage | 138 | class ChannelMessage |
137 | { | 139 | { |
138 | friend class Channel; | 140 | friend class Channel; |
139 | public: | 141 | public: |
140 | ChannelMessage( int type = -1 ); | 142 | ChannelMessage( int type = -1 ); |
141 | virtual ~ChannelMessage(); | 143 | virtual ~ChannelMessage(); |
142 | 144 | ||
143 | int type() const { return m_type; } | 145 | int type() const { return m_type; } |
144 | 146 | ||
145 | void reply(); | 147 | void reply(); |
146 | 148 | ||
147 | private: | 149 | private: |
148 | ChannelMessage( const ChannelMessage & ); | 150 | ChannelMessage( const ChannelMessage & ); |
149 | ChannelMessage &operator=( const ChannelMessage ); | 151 | ChannelMessage &operator=( const ChannelMessage ); |
150 | 152 | ||