Ticket #5604: DeviceReadBuffer-polltimeout.2.patch
File DeviceReadBuffer-polltimeout.2.patch, 5.4 KB (added by , 16 years ago) |
---|
-
libs/libmythtv/DeviceReadBuffer.h
59 59 void IncrReadPointer(uint len); 60 60 61 61 bool HandlePausing(void); 62 bool Poll(void) const;62 bool Poll(void); 63 63 uint WaitForUnused(uint bytes_needed) const; 64 64 uint WaitForUsed (uint bytes_needed) const; 65 65 -
libs/libmythtv/DeviceReadBuffer.cpp
90 90 91 91 void DeviceReadBuffer::Start(void) 92 92 { 93 lock.lock(); 94 bool was_running = running; 95 lock.unlock(); 93 bool was_running; 94 95 { 96 QMutexLocker locker(&lock); 97 was_running = running; 98 error = false; 99 } 100 96 101 if (was_running) 97 102 { 98 103 VERBOSE(VB_IMPORTANT, LOC_ERR + "Start(): Already running."); … … 104 109 { 105 110 VERBOSE(VB_IMPORTANT, 106 111 LOC_ERR + QString("Start(): pthread_create failed.") + ENO); 112 113 QMutexLocker locker(&lock); 107 114 error = true; 108 115 } 109 116 } … … 118 125 used = 0; 119 126 readPtr = buffer; 120 127 writePtr = buffer; 128 129 error = false; 121 130 } 122 131 123 132 void DeviceReadBuffer::Stop(void) 124 133 { 125 134 bool was_running = IsRunning(); 126 lock.lock();127 run = false;128 lock.unlock();129 135 130 136 if (!was_running) 131 137 { 132 VERBOSE(VB_IMPORTANT, LOC _ERR+ "Stop(): Not running.");138 VERBOSE(VB_IMPORTANT, LOC + "Stop(): Not running."); 133 139 return; 134 140 } 135 141 142 { 143 QMutexLocker locker(&lock); 144 run = false; 145 } 146 136 147 pthread_join(thread, NULL); 137 148 } 138 149 … … 229 240 { 230 241 uint errcnt = 0; 231 242 232 lock.lock(); 233 run = true; 234 running = true; 235 lock.unlock(); 243 { 244 QMutexLocker locker(&lock); 245 run = true; 246 running = true; 247 } 236 248 237 249 while (run) 238 250 { … … 248 260 if (using_poll && !Poll()) 249 261 continue; 250 262 263 { 264 QMutexLocker locker(&lock); 265 if (error) 266 { 267 VERBOSE(VB_RECORD, LOC + "fill_ringbuffer: error state"); 268 break; 269 } 270 } 271 251 272 // Limit read size for faster return from read 252 273 size_t read_size = 253 274 min(dev_read_size, (size_t) WaitForUnused(TSPacket::SIZE)); … … 268 289 } 269 290 } 270 291 271 lock.lock(); 272 running = false; 273 lock.unlock(); 292 { 293 QMutexLocker locker(&lock); 294 running = false; 295 } 274 296 } 275 297 276 298 bool DeviceReadBuffer::HandlePausing(void) … … 293 315 return true; 294 316 } 295 317 296 bool DeviceReadBuffer::Poll(void) const318 bool DeviceReadBuffer::Poll(void) 297 319 { 298 320 #ifdef USING_MINGW 299 321 #warning mingw DeviceReadBuffer::Poll … … 302 324 return false; 303 325 #else 304 326 bool retval = true; 305 while (true) 327 uint timeout_cnt = 0; 328 329 for (;;) 306 330 { 307 331 struct pollfd polls; 308 332 polls.fd = _stream_fd; 309 333 polls.events = POLLIN; 310 334 polls.revents = 0; 311 335 312 int ret = poll(&polls, 1 /*number of polls*/, 10 /*msec*/); 313 if (IsPauseRequested() || !IsOpen() || !run) 336 int ret = poll(&polls, 1 /*number of polls*/, 250 /*msec*/); 337 338 if (polls.revents & (POLLERR | POLLHUP | POLLNVAL)) 314 339 { 340 VERBOSE(VB_IMPORTANT, LOC + "poll error"); 341 error = true; 342 return true; 343 } 344 345 if (!run || !IsOpen() || IsPauseRequested()) 346 { 315 347 retval = false; 316 348 break; // are we supposed to pause, stop, etc. 317 349 } 318 350 319 351 if (ret > 0) 320 352 break; // we have data to read :) 321 if ((-1 == ret) && (EOVERFLOW == errno)) 322 break; // we have an error to handle 323 324 if ((-1 == ret) && ((EAGAIN == errno) || (EINTR == errno))) 325 continue; // errors that tell you to try again 326 if (ret == 0) 327 continue; // timed out, try again 328 329 usleep(2500); 353 if (ret < 0) 354 { 355 if ((EOVERFLOW == errno)) 356 break; // we have an error to handle 357 if ((EAGAIN == errno) || (EINTR == errno)) 358 continue; // errors that tell you to try again 359 usleep(2500); 360 } 361 else // ret == 0 362 { 363 if (++timeout_cnt > 9) 364 { 365 VERBOSE(VB_RECORD, LOC_ERR + "Poll giving up"); 366 QMutexLocker locker(&lock); 367 error = true; 368 return true; 369 } 370 if (timeout_cnt % 2) 371 VERBOSE(VB_RECORD, LOC_ERR + QString("Poll timeout (%1)") 372 .arg(timeout_cnt)); 373 } 330 374 } 331 375 return retval; 332 376 #endif //!USING_MINGW … … 360 404 361 405 if (++errcnt > 5) 362 406 { 363 lock.lock(); 407 QMutexLocker locker(&lock); 408 VERBOSE(VB_RECORD, LOC + "Too many errors."); 364 409 error = true; 365 lock.unlock();366 410 return false; 367 411 } 368 412 … … 376 420 VERBOSE(VB_IMPORTANT, LOC + 377 421 QString("End-Of-File? fd(%1)").arg(_stream_fd)); 378 422 379 lock.lock();423 QMutexLocker locker(&lock); 380 424 eof = true; 381 lock.unlock();382 383 425 return false; 384 426 } 385 427 usleep(500);