author | erik <erik> | 2007-01-31 22:06:07 (UTC) |
---|---|---|
committer | erik <erik> | 2007-01-31 22:06:07 (UTC) |
commit | 9395cf2a65184e493714c699bb23b02ea31feef5 (patch) (unidiff) | |
tree | ea07c026298820559c13ad32e612d51048cee0a9 /noncore/multimedia/opieplayer2/threadutil.cpp | |
parent | 958e042c5a4d4e38fd1baae50b78a2febfd306ab (diff) | |
download | opie-9395cf2a65184e493714c699bb23b02ea31feef5.zip opie-9395cf2a65184e493714c699bb23b02ea31feef5.tar.gz opie-9395cf2a65184e493714c699bb23b02ea31feef5.tar.bz2 |
I expanded my audit to include any app I could get to compile in i386.
In that expansion a whole new crop of unchecked returns has sprung up.
This commit fixes those weeds or should I say potential bugs.
Diffstat (limited to 'noncore/multimedia/opieplayer2/threadutil.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/multimedia/opieplayer2/threadutil.cpp | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/noncore/multimedia/opieplayer2/threadutil.cpp b/noncore/multimedia/opieplayer2/threadutil.cpp index b5cac61..5fc8a0b 100644 --- a/noncore/multimedia/opieplayer2/threadutil.cpp +++ b/noncore/multimedia/opieplayer2/threadutil.cpp | |||
@@ -140,198 +140,203 @@ struct Thread::Data | |||
140 | void run() { thr->run(); } | 140 | void run() { thr->run(); } |
141 | }; | 141 | }; |
142 | 142 | ||
143 | extern "C" | 143 | extern "C" |
144 | { | 144 | { |
145 | 145 | ||
146 | static void terminate_thread( void *arg ) | 146 | static void terminate_thread( void *arg ) |
147 | { | 147 | { |
148 | Thread::Data *data = ( Thread::Data* )arg; | 148 | Thread::Data *data = ( Thread::Data* )arg; |
149 | 149 | ||
150 | assert( data ); | 150 | assert( data ); |
151 | 151 | ||
152 | AutoLock locker( data->guard ); | 152 | AutoLock locker( data->guard ); |
153 | data->isRunning = false; | 153 | data->isRunning = false; |
154 | data->finishCondition.wakeAll(); | 154 | data->finishCondition.wakeAll(); |
155 | } | 155 | } |
156 | 156 | ||
157 | static void *start_thread( void *arg ) | 157 | static void *start_thread( void *arg ) |
158 | { | 158 | { |
159 | Thread::Data *data = ( Thread::Data* )arg; | 159 | Thread::Data *data = ( Thread::Data* )arg; |
160 | 160 | ||
161 | pthread_cleanup_push( terminate_thread, data ); | 161 | pthread_cleanup_push( terminate_thread, data ); |
162 | 162 | ||
163 | data->isRunning = true; | 163 | data->isRunning = true; |
164 | data->run(); | 164 | data->run(); |
165 | 165 | ||
166 | pthread_cleanup_pop( true ); | 166 | pthread_cleanup_pop( true ); |
167 | 167 | ||
168 | Thread::exit(); | 168 | Thread::exit(); |
169 | return 0; // never reached | 169 | return 0; // never reached |
170 | } | 170 | } |
171 | 171 | ||
172 | } | 172 | } |
173 | 173 | ||
174 | Thread::Thread() | 174 | Thread::Thread() |
175 | : d( new Data ) | 175 | : d( new Data ) |
176 | { | 176 | { |
177 | d->thr = this; | 177 | d->thr = this; |
178 | } | 178 | } |
179 | 179 | ||
180 | Thread::~Thread() | 180 | Thread::~Thread() |
181 | { | 181 | { |
182 | assert( d->isRunning == false ); | 182 | assert( d->isRunning == false ); |
183 | delete d; | 183 | delete d; |
184 | } | 184 | } |
185 | 185 | ||
186 | void Thread::start() | 186 | void Thread::start() |
187 | { | 187 | { |
188 | AutoLock lock( d->guard ); | 188 | AutoLock lock( d->guard ); |
189 | 189 | ||
190 | if ( d->isRunning ) { | 190 | if ( d->isRunning ) { |
191 | odebug << "ThreadUtil::Thread::start() called for running thread." << oendl; | 191 | odebug << "ThreadUtil::Thread::start() called for running thread." << oendl; |
192 | return; | 192 | return; |
193 | } | 193 | } |
194 | 194 | ||
195 | pthread_attr_t attributes; | 195 | pthread_attr_t attributes; |
196 | pthread_attr_init( &attributes ); | 196 | pthread_attr_init( &attributes ); |
197 | pthread_attr_setscope( &attributes, PTHREAD_SCOPE_SYSTEM ); | 197 | pthread_attr_setscope( &attributes, PTHREAD_SCOPE_SYSTEM ); |
198 | int err = pthread_create( &d->self, &attributes, start_thread, ( void* )d ); | 198 | int err = pthread_create( &d->self, &attributes, start_thread, ( void* )d ); |
199 | if ( err != 0 ) { | 199 | if ( err != 0 ) { |
200 | odebug << "ThreadUtil::Thread::start() : can't create thread: " << strerror( err ) << "" << oendl; | 200 | odebug << "ThreadUtil::Thread::start() : can't create thread: " << strerror( err ) << "" << oendl; |
201 | pthread_attr_destroy( &attributes ); | 201 | pthread_attr_destroy( &attributes ); |
202 | return; | 202 | return; |
203 | } | 203 | } |
204 | pthread_attr_destroy( &attributes ); | 204 | pthread_attr_destroy( &attributes ); |
205 | } | 205 | } |
206 | 206 | ||
207 | void Thread::terminate() | 207 | void Thread::terminate() |
208 | { | 208 | { |
209 | AutoLock lock( d->guard ); | 209 | AutoLock lock( d->guard ); |
210 | if ( !d->isRunning ) | 210 | if ( !d->isRunning ) |
211 | return; | 211 | return; |
212 | 212 | ||
213 | pthread_cancel( d->self ); | 213 | pthread_cancel( d->self ); |
214 | } | 214 | } |
215 | 215 | ||
216 | bool Thread::wait() | 216 | bool Thread::wait() |
217 | { | 217 | { |
218 | AutoLock lock( d->guard ); | 218 | AutoLock lock( d->guard ); |
219 | if ( !d->isRunning ) | 219 | if ( !d->isRunning ) |
220 | return true; | 220 | return true; |
221 | 221 | ||
222 | return d->finishCondition.wait( d->guard ); | 222 | return d->finishCondition.wait( d->guard ); |
223 | } | 223 | } |
224 | 224 | ||
225 | bool Thread::isRunning() const | 225 | bool Thread::isRunning() const |
226 | { | 226 | { |
227 | AutoLock lock( d->guard ); | 227 | AutoLock lock( d->guard ); |
228 | return d->isRunning; | 228 | return d->isRunning; |
229 | } | 229 | } |
230 | 230 | ||
231 | void Thread::exit() | 231 | void Thread::exit() |
232 | { | 232 | { |
233 | pthread_exit( 0 ); | 233 | pthread_exit( 0 ); |
234 | } | 234 | } |
235 | 235 | ||
236 | OnewayNotifier::OnewayNotifier() | 236 | OnewayNotifier::OnewayNotifier() : |
237 | { | 237 | m_readFd(-1), |
238 | int fds[ 2 ]; | 238 | m_writeFd(-1) |
239 | pipe( fds ); | 239 | { |
240 | m_readFd = fds[ 0 ]; | 240 | int fds[ 2 ] = { -1, -1 }; |
241 | m_writeFd = fds[ 1 ]; | 241 | if (pipe( fds ) == 0) { |
242 | m_readFd = fds[ 0 ]; | ||
243 | m_writeFd = fds[ 1 ]; | ||
244 | } else { | ||
245 | owarn << "Call to pipe() failed" << oendl; | ||
246 | } | ||
242 | 247 | ||
243 | m_notifier = new QSocketNotifier( m_readFd, QSocketNotifier::Read ); | 248 | m_notifier = new QSocketNotifier( m_readFd, QSocketNotifier::Read ); |
244 | connect( m_notifier, SIGNAL( activated(int) ), | 249 | connect( m_notifier, SIGNAL( activated(int) ), |
245 | this, SLOT( wakeUp() ) ); | 250 | this, SLOT( wakeUp() ) ); |
246 | } | 251 | } |
247 | 252 | ||
248 | OnewayNotifier::~OnewayNotifier() | 253 | OnewayNotifier::~OnewayNotifier() |
249 | { | 254 | { |
250 | delete m_notifier; | 255 | delete m_notifier; |
251 | 256 | ||
252 | ::close( m_readFd ); | 257 | ::close( m_readFd ); |
253 | ::close( m_writeFd ); | 258 | ::close( m_writeFd ); |
254 | } | 259 | } |
255 | 260 | ||
256 | void OnewayNotifier::notify() | 261 | void OnewayNotifier::notify() |
257 | { | 262 | { |
258 | const char c = 42; | 263 | const char c = 42; |
259 | ::write( m_writeFd, &c, 1 ); | 264 | ::write( m_writeFd, &c, 1 ); |
260 | } | 265 | } |
261 | 266 | ||
262 | void OnewayNotifier::wakeUp() | 267 | void OnewayNotifier::wakeUp() |
263 | { | 268 | { |
264 | char c = 0; | 269 | char c = 0; |
265 | 270 | ||
266 | if ( ::read( m_readFd, &c, 1 ) != 1 ) | 271 | if ( ::read( m_readFd, &c, 1 ) != 1 ) |
267 | return; | 272 | return; |
268 | 273 | ||
269 | emit awake(); | 274 | emit awake(); |
270 | } | 275 | } |
271 | 276 | ||
272 | ChannelMessage::ChannelMessage( int type, int data, const char* msg ) | 277 | ChannelMessage::ChannelMessage( int type, int data, const char* msg ) |
273 | : m_type( type ), m_data( data ), m_msg( msg ), | 278 | : m_type( type ), m_data( data ), m_msg( msg ), |
274 | m_isCall( false ), m_replied( false ), m_inEventHandler( false ) | 279 | m_isCall( false ), m_replied( false ), m_inEventHandler( false ) |
275 | {} | 280 | {} |
276 | 281 | ||
277 | ChannelMessage::~ChannelMessage() | 282 | ChannelMessage::~ChannelMessage() |
278 | { | 283 | { |
279 | if ( m_guard.isLocked() ) | 284 | if ( m_guard.isLocked() ) |
280 | m_guard.unlock(); | 285 | m_guard.unlock(); |
281 | } | 286 | } |
282 | 287 | ||
283 | void ChannelMessage::reply() | 288 | void ChannelMessage::reply() |
284 | { | 289 | { |
285 | if ( !m_isCall ) | 290 | if ( !m_isCall ) |
286 | { | 291 | { |
287 | odebug << "ChannelMessage::reply() - can't reply oneway message!" << oendl; | 292 | odebug << "ChannelMessage::reply() - can't reply oneway message!" << oendl; |
288 | return; | 293 | return; |
289 | } | 294 | } |
290 | 295 | ||
291 | if ( m_inEventHandler ) | 296 | if ( m_inEventHandler ) |
292 | { | 297 | { |
293 | m_replied = true; | 298 | m_replied = true; |
294 | return; | 299 | return; |
295 | } | 300 | } |
296 | 301 | ||
297 | m_condition.wakeOne(); | 302 | m_condition.wakeOne(); |
298 | m_guard.unlock(); | 303 | m_guard.unlock(); |
299 | } | 304 | } |
300 | 305 | ||
301 | struct Channel::Private | 306 | struct Channel::Private |
302 | { | 307 | { |
303 | Private() | 308 | Private() |
304 | { | 309 | { |
305 | ownerThread = pthread_self(); | 310 | ownerThread = pthread_self(); |
306 | } | 311 | } |
307 | 312 | ||
308 | pthread_t ownerThread; | 313 | pthread_t ownerThread; |
309 | }; | 314 | }; |
310 | 315 | ||
311 | Channel::Channel( QObject *parent, const char *name ) | 316 | Channel::Channel( QObject *parent, const char *name ) |
312 | : QObject( parent, name ), d( new Private ) | 317 | : QObject( parent, name ), d( new Private ) |
313 | { | 318 | { |
314 | connect( &m_notifier, SIGNAL( awake() ), | 319 | connect( &m_notifier, SIGNAL( awake() ), |
315 | this, SLOT( deliver() ) ); | 320 | this, SLOT( deliver() ) ); |
316 | } | 321 | } |
317 | 322 | ||
318 | Channel::~Channel() | 323 | Channel::~Channel() |
319 | { | 324 | { |
320 | delete d; | 325 | delete d; |
321 | } | 326 | } |
322 | 327 | ||
323 | void Channel::send( ChannelMessage *message, SendType type ) | 328 | void Channel::send( ChannelMessage *message, SendType type ) |
324 | { | 329 | { |
325 | if ( type == WaitForReply ) | 330 | if ( type == WaitForReply ) |
326 | { | 331 | { |
327 | message->m_guard.lock(); | 332 | message->m_guard.lock(); |
328 | message->m_isCall = true; | 333 | message->m_isCall = true; |
329 | } | 334 | } |
330 | 335 | ||
331 | m_pendingMessagesGuard.lock(); | 336 | m_pendingMessagesGuard.lock(); |
332 | m_pendingMessages << MsgEnvelope( type, message ); | 337 | m_pendingMessages << MsgEnvelope( type, message ); |
333 | m_pendingMessagesGuard.unlock(); | 338 | m_pendingMessagesGuard.unlock(); |
334 | 339 | ||
335 | if ( d->ownerThread == pthread_self() ) { | 340 | if ( d->ownerThread == pthread_self() ) { |
336 | assert( type != WaitForReply ); | 341 | assert( type != WaitForReply ); |
337 | 342 | ||