Diffstat (limited to 'noncore/multimedia/opieplayer2/threadutil.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/multimedia/opieplayer2/threadutil.cpp | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/noncore/multimedia/opieplayer2/threadutil.cpp b/noncore/multimedia/opieplayer2/threadutil.cpp index fb951b4..d8b8abe 100644 --- a/noncore/multimedia/opieplayer2/threadutil.cpp +++ b/noncore/multimedia/opieplayer2/threadutil.cpp | |||
@@ -94,290 +94,290 @@ struct WaitCondition::Data | |||
94 | WaitCondition::WaitCondition() | 94 | WaitCondition::WaitCondition() |
95 | : d( new Data ) | 95 | : d( new Data ) |
96 | { | 96 | { |
97 | } | 97 | } |
98 | 98 | ||
99 | WaitCondition::~WaitCondition() | 99 | WaitCondition::~WaitCondition() |
100 | { | 100 | { |
101 | delete d; | 101 | delete d; |
102 | } | 102 | } |
103 | 103 | ||
104 | bool WaitCondition::wait() | 104 | bool WaitCondition::wait() |
105 | { | 105 | { |
106 | Mutex m; | 106 | Mutex m; |
107 | m.lock(); | 107 | m.lock(); |
108 | return wait( m ); | 108 | return wait( m ); |
109 | } | 109 | } |
110 | 110 | ||
111 | bool WaitCondition::wait( Mutex &mutex ) | 111 | bool WaitCondition::wait( Mutex &mutex ) |
112 | { | 112 | { |
113 | return pthread_cond_wait( &d->waitCondition, &mutex.d->mutex ); | 113 | return pthread_cond_wait( &d->waitCondition, &mutex.d->mutex ); |
114 | } | 114 | } |
115 | 115 | ||
116 | void WaitCondition::wakeOne() | 116 | void WaitCondition::wakeOne() |
117 | { | 117 | { |
118 | pthread_cond_signal( &d->waitCondition ); | 118 | pthread_cond_signal( &d->waitCondition ); |
119 | } | 119 | } |
120 | 120 | ||
121 | void WaitCondition::wakeAll() | 121 | void WaitCondition::wakeAll() |
122 | { | 122 | { |
123 | pthread_cond_broadcast( &d->waitCondition ); | 123 | pthread_cond_broadcast( &d->waitCondition ); |
124 | } | 124 | } |
125 | 125 | ||
126 | struct Thread::Data | 126 | struct Thread::Data |
127 | { | 127 | { |
128 | Data() : isRunning( false ) | 128 | Data() : isRunning( false ) |
129 | {} | 129 | {} |
130 | 130 | ||
131 | pthread_t self; | 131 | pthread_t self; |
132 | Mutex guard; | 132 | Mutex guard; |
133 | bool isRunning; | 133 | bool isRunning; |
134 | 134 | ||
135 | WaitCondition finishCondition; | 135 | WaitCondition finishCondition; |
136 | 136 | ||
137 | Thread *thr; | 137 | Thread *thr; |
138 | 138 | ||
139 | void run() { thr->run(); } | 139 | void run() { thr->run(); } |
140 | }; | 140 | }; |
141 | 141 | ||
142 | extern "C" | 142 | extern "C" |
143 | { | 143 | { |
144 | 144 | ||
145 | static void terminate_thread( void *arg ) | 145 | static void terminate_thread( void *arg ) |
146 | { | 146 | { |
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 | odebug << "ThreadUtil::Thread::start() called for running thread." << oendl; |
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 | odebug << "ThreadUtil::Thread::start() : can't create thread: " << strerror( err ) << "" << oendl; |
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 | odebug << "ChannelMessage::reply() - can't reply oneway message!" << oendl; |
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 | } |
340 | else | 340 | else |
341 | m_notifier.notify(); | 341 | m_notifier.notify(); |
342 | //QThread::postEvent( this, new QCustomEvent( QEvent::User, envelope ) ); | 342 | //QThread::postEvent( this, new QCustomEvent( QEvent::User, envelope ) ); |
343 | 343 | ||
344 | if ( type == WaitForReply ) | 344 | if ( type == WaitForReply ) |
345 | { | 345 | { |
346 | message->m_condition.wait( message->m_guard ); | 346 | message->m_condition.wait( message->m_guard ); |
347 | message->m_guard.unlock(); | 347 | message->m_guard.unlock(); |
348 | } | 348 | } |
349 | } | 349 | } |
350 | 350 | ||
351 | void Channel::deliver() | 351 | void Channel::deliver() |
352 | { | 352 | { |
353 | AutoLock lock( m_pendingMessagesGuard ); | 353 | AutoLock lock( m_pendingMessagesGuard ); |
354 | 354 | ||
355 | while ( !m_pendingMessages.isEmpty() ) { | 355 | while ( !m_pendingMessages.isEmpty() ) { |
356 | MsgEnvelope envelope = m_pendingMessages.first(); | 356 | MsgEnvelope envelope = m_pendingMessages.first(); |
357 | 357 | ||
358 | m_pendingMessages.remove( m_pendingMessages.begin() ); | 358 | m_pendingMessages.remove( m_pendingMessages.begin() ); |
359 | 359 | ||
360 | m_pendingMessagesGuard.unlock(); | 360 | m_pendingMessagesGuard.unlock(); |
361 | deliverOne( envelope ); | 361 | deliverOne( envelope ); |
362 | m_pendingMessagesGuard.lock(); | 362 | m_pendingMessagesGuard.lock(); |
363 | } | 363 | } |
364 | } | 364 | } |
365 | 365 | ||
366 | void Channel::deliverOne( const MsgEnvelope &envelope ) | 366 | void Channel::deliverOne( const MsgEnvelope &envelope ) |
367 | { | 367 | { |
368 | ChannelMessage *msg = envelope.msg; | 368 | ChannelMessage *msg = envelope.msg; |
369 | 369 | ||
370 | assert( msg ); | 370 | assert( msg ); |
371 | 371 | ||
372 | if ( envelope.type == WaitForReply ) | 372 | if ( envelope.type == WaitForReply ) |
373 | { | 373 | { |
374 | msg->m_guard.lock(); | 374 | msg->m_guard.lock(); |
375 | msg->m_inEventHandler = true; | 375 | msg->m_inEventHandler = true; |
376 | } | 376 | } |
377 | 377 | ||
378 | receiveMessage( msg, envelope.type ); | 378 | receiveMessage( msg, envelope.type ); |
379 | 379 | ||
380 | if ( envelope.type == WaitForReply ) | 380 | if ( envelope.type == WaitForReply ) |
381 | { | 381 | { |
382 | msg->m_inEventHandler = false; | 382 | msg->m_inEventHandler = false; |
383 | if ( msg->m_replied ) | 383 | if ( msg->m_replied ) |