Diffstat (limited to 'noncore/multimedia/opieplayer2/threadutil.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/multimedia/opieplayer2/threadutil.cpp | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/noncore/multimedia/opieplayer2/threadutil.cpp b/noncore/multimedia/opieplayer2/threadutil.cpp index ff38b1e..fb951b4 100644 --- a/noncore/multimedia/opieplayer2/threadutil.cpp +++ b/noncore/multimedia/opieplayer2/threadutil.cpp | |||
@@ -147,193 +147,193 @@ static void terminate_thread( void *arg ) | |||
147 | Thread::Data *data = ( Thread::Data* )arg; | 147 | Thread::Data *data = ( Thread::Data* )arg; |
148 | 148 | ||
149 | assert( data ); | 149 | assert( data ); |
150 | 150 | ||
151 | AutoLock locker( data->guard ); | 151 | AutoLock locker( data->guard ); |
152 | data->isRunning = false; | 152 | data->isRunning = false; |
153 | data->finishCondition.wakeAll(); | 153 | data->finishCondition.wakeAll(); |
154 | } | 154 | } |
155 | 155 | ||
156 | static void *start_thread( void *arg ) | 156 | static void *start_thread( void *arg ) |
157 | { | 157 | { |
158 | Thread::Data *data = ( Thread::Data* )arg; | 158 | Thread::Data *data = ( Thread::Data* )arg; |
159 | 159 | ||
160 | pthread_cleanup_push( terminate_thread, data ); | 160 | pthread_cleanup_push( terminate_thread, data ); |
161 | 161 | ||
162 | data->isRunning = true; | 162 | data->isRunning = true; |
163 | data->run(); | 163 | data->run(); |
164 | 164 | ||
165 | pthread_cleanup_pop( true ); | 165 | pthread_cleanup_pop( true ); |
166 | 166 | ||
167 | Thread::exit(); | 167 | Thread::exit(); |
168 | return 0; // never reached | 168 | return 0; // never reached |
169 | } | 169 | } |
170 | 170 | ||
171 | } | 171 | } |
172 | 172 | ||
173 | Thread::Thread() | 173 | Thread::Thread() |
174 | : d( new Data ) | 174 | : d( new Data ) |
175 | { | 175 | { |
176 | d->thr = this; | 176 | d->thr = this; |
177 | } | 177 | } |
178 | 178 | ||
179 | Thread::~Thread() | 179 | Thread::~Thread() |
180 | { | 180 | { |
181 | assert( d->isRunning == false ); | 181 | assert( d->isRunning == false ); |
182 | delete d; | 182 | delete d; |
183 | } | 183 | } |
184 | 184 | ||
185 | void Thread::start() | 185 | void Thread::start() |
186 | { | 186 | { |
187 | AutoLock lock( d->guard ); | 187 | AutoLock lock( d->guard ); |
188 | 188 | ||
189 | if ( d->isRunning ) { | 189 | if ( d->isRunning ) { |
190 | qDebug( "ThreadUtil::Thread::start() called for running thread." ); | 190 | qDebug( "ThreadUtil::Thread::start() called for running thread." ); |
191 | return; | 191 | return; |
192 | } | 192 | } |
193 | 193 | ||
194 | pthread_attr_t attributes; | 194 | pthread_attr_t attributes; |
195 | pthread_attr_init( &attributes ); | 195 | pthread_attr_init( &attributes ); |
196 | pthread_attr_setscope( &attributes, PTHREAD_SCOPE_SYSTEM ); | 196 | pthread_attr_setscope( &attributes, PTHREAD_SCOPE_SYSTEM ); |
197 | int err = pthread_create( &d->self, &attributes, start_thread, ( void* )d ); | 197 | int err = pthread_create( &d->self, &attributes, start_thread, ( void* )d ); |
198 | if ( err != 0 ) { | 198 | if ( err != 0 ) { |
199 | qDebug( "ThreadUtil::Thread::start() : can't create thread: %s", strerror( err ) ); | 199 | qDebug( "ThreadUtil::Thread::start() : can't create thread: %s", strerror( err ) ); |
200 | pthread_attr_destroy( &attributes ); | 200 | pthread_attr_destroy( &attributes ); |
201 | return; | 201 | return; |
202 | } | 202 | } |
203 | pthread_attr_destroy( &attributes ); | 203 | pthread_attr_destroy( &attributes ); |
204 | } | 204 | } |
205 | 205 | ||
206 | void Thread::terminate() | 206 | void Thread::terminate() |
207 | { | 207 | { |
208 | AutoLock lock( d->guard ); | 208 | AutoLock lock( d->guard ); |
209 | if ( !d->isRunning ) | 209 | if ( !d->isRunning ) |
210 | return; | 210 | return; |
211 | 211 | ||
212 | pthread_cancel( d->self ); | 212 | pthread_cancel( d->self ); |
213 | } | 213 | } |
214 | 214 | ||
215 | bool Thread::wait() | 215 | bool Thread::wait() |
216 | { | 216 | { |
217 | AutoLock lock( d->guard ); | 217 | AutoLock lock( d->guard ); |
218 | if ( !d->isRunning ) | 218 | if ( !d->isRunning ) |
219 | return true; | 219 | return true; |
220 | 220 | ||
221 | return d->finishCondition.wait( d->guard ); | 221 | return d->finishCondition.wait( d->guard ); |
222 | } | 222 | } |
223 | 223 | ||
224 | bool Thread::isRunning() const | 224 | bool Thread::isRunning() const |
225 | { | 225 | { |
226 | AutoLock lock( d->guard ); | 226 | AutoLock lock( d->guard ); |
227 | return d->isRunning; | 227 | return d->isRunning; |
228 | } | 228 | } |
229 | 229 | ||
230 | void Thread::exit() | 230 | void Thread::exit() |
231 | { | 231 | { |
232 | pthread_exit( 0 ); | 232 | pthread_exit( 0 ); |
233 | } | 233 | } |
234 | 234 | ||
235 | OnewayNotifier::OnewayNotifier() | 235 | OnewayNotifier::OnewayNotifier() |
236 | { | 236 | { |
237 | int fds[ 2 ]; | 237 | int fds[ 2 ]; |
238 | pipe( fds ); | 238 | pipe( fds ); |
239 | m_readFd = fds[ 0 ]; | 239 | m_readFd = fds[ 0 ]; |
240 | m_writeFd = fds[ 1 ]; | 240 | m_writeFd = fds[ 1 ]; |
241 | 241 | ||
242 | m_notifier = new QSocketNotifier( m_readFd, QSocketNotifier::Read ); | 242 | m_notifier = new QSocketNotifier( m_readFd, QSocketNotifier::Read ); |
243 | connect( m_notifier, SIGNAL( activated( int ) ), | 243 | connect( m_notifier, SIGNAL( activated(int) ), |
244 | this, SLOT( wakeUp() ) ); | 244 | this, SLOT( wakeUp() ) ); |
245 | } | 245 | } |
246 | 246 | ||
247 | OnewayNotifier::~OnewayNotifier() | 247 | OnewayNotifier::~OnewayNotifier() |
248 | { | 248 | { |
249 | delete m_notifier; | 249 | delete m_notifier; |
250 | 250 | ||
251 | ::close( m_readFd ); | 251 | ::close( m_readFd ); |
252 | ::close( m_writeFd ); | 252 | ::close( m_writeFd ); |
253 | } | 253 | } |
254 | 254 | ||
255 | void OnewayNotifier::notify() | 255 | void OnewayNotifier::notify() |
256 | { | 256 | { |
257 | const char c = 42; | 257 | const char c = 42; |
258 | ::write( m_writeFd, &c, 1 ); | 258 | ::write( m_writeFd, &c, 1 ); |
259 | } | 259 | } |
260 | 260 | ||
261 | void OnewayNotifier::wakeUp() | 261 | void OnewayNotifier::wakeUp() |
262 | { | 262 | { |
263 | char c = 0; | 263 | char c = 0; |
264 | 264 | ||
265 | if ( ::read( m_readFd, &c, 1 ) != 1 ) | 265 | if ( ::read( m_readFd, &c, 1 ) != 1 ) |
266 | return; | 266 | return; |
267 | 267 | ||
268 | emit awake(); | 268 | emit awake(); |
269 | } | 269 | } |
270 | 270 | ||
271 | ChannelMessage::ChannelMessage( int type ) | 271 | ChannelMessage::ChannelMessage( int type ) |
272 | : m_type( type ), m_isCall( false ), m_replied( false ), | 272 | : m_type( type ), m_isCall( false ), m_replied( false ), |
273 | m_inEventHandler( false ) | 273 | m_inEventHandler( false ) |
274 | { | 274 | { |
275 | } | 275 | } |
276 | 276 | ||
277 | ChannelMessage::~ChannelMessage() | 277 | ChannelMessage::~ChannelMessage() |
278 | { | 278 | { |
279 | if ( m_guard.isLocked() ) | 279 | if ( m_guard.isLocked() ) |
280 | m_guard.unlock(); | 280 | m_guard.unlock(); |
281 | } | 281 | } |
282 | 282 | ||
283 | void ChannelMessage::reply() | 283 | void ChannelMessage::reply() |
284 | { | 284 | { |
285 | if ( !m_isCall ) | 285 | if ( !m_isCall ) |
286 | { | 286 | { |
287 | qDebug( "ChannelMessage::reply() - can't reply oneway message!" ); | 287 | qDebug( "ChannelMessage::reply() - can't reply oneway message!" ); |
288 | return; | 288 | return; |
289 | } | 289 | } |
290 | 290 | ||
291 | if ( m_inEventHandler ) | 291 | if ( m_inEventHandler ) |
292 | { | 292 | { |
293 | m_replied = true; | 293 | m_replied = true; |
294 | return; | 294 | return; |
295 | } | 295 | } |
296 | 296 | ||
297 | m_condition.wakeOne(); | 297 | m_condition.wakeOne(); |
298 | m_guard.unlock(); | 298 | m_guard.unlock(); |
299 | } | 299 | } |
300 | 300 | ||
301 | struct Channel::Private | 301 | struct Channel::Private |
302 | { | 302 | { |
303 | Private() | 303 | Private() |
304 | { | 304 | { |
305 | ownerThread = pthread_self(); | 305 | ownerThread = pthread_self(); |
306 | } | 306 | } |
307 | 307 | ||
308 | pthread_t ownerThread; | 308 | pthread_t ownerThread; |
309 | }; | 309 | }; |
310 | 310 | ||
311 | Channel::Channel( QObject *parent, const char *name ) | 311 | Channel::Channel( QObject *parent, const char *name ) |
312 | : QObject( parent, name ), d( new Private ) | 312 | : QObject( parent, name ), d( new Private ) |
313 | { | 313 | { |
314 | connect( &m_notifier, SIGNAL( awake() ), | 314 | connect( &m_notifier, SIGNAL( awake() ), |
315 | this, SLOT( deliver() ) ); | 315 | this, SLOT( deliver() ) ); |
316 | } | 316 | } |
317 | 317 | ||
318 | Channel::~Channel() | 318 | Channel::~Channel() |
319 | { | 319 | { |
320 | delete d; | 320 | delete d; |
321 | } | 321 | } |
322 | 322 | ||
323 | void Channel::send( ChannelMessage *message, SendType type ) | 323 | void Channel::send( ChannelMessage *message, SendType type ) |
324 | { | 324 | { |
325 | if ( type == WaitForReply ) | 325 | if ( type == WaitForReply ) |
326 | { | 326 | { |
327 | message->m_guard.lock(); | 327 | message->m_guard.lock(); |
328 | message->m_isCall = true; | 328 | message->m_isCall = true; |
329 | } | 329 | } |
330 | 330 | ||
331 | m_pendingMessagesGuard.lock(); | 331 | m_pendingMessagesGuard.lock(); |
332 | m_pendingMessages << MsgEnvelope( type, message ); | 332 | m_pendingMessages << MsgEnvelope( type, message ); |
333 | m_pendingMessagesGuard.unlock(); | 333 | m_pendingMessagesGuard.unlock(); |
334 | 334 | ||
335 | if ( d->ownerThread == pthread_self() ) { | 335 | if ( d->ownerThread == pthread_self() ) { |
336 | assert( type != WaitForReply ); | 336 | assert( type != WaitForReply ); |
337 | 337 | ||
338 | deliver(); | 338 | deliver(); |
339 | } | 339 | } |