Ticket #5604: DeviceReadBuffer-polltimeout.patch
File DeviceReadBuffer-polltimeout.patch, 5.5 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
89 89 90 90 void DeviceReadBuffer::Start(void) 91 91 { 92 lock.lock(); 93 bool was_running = running; 94 lock.unlock(); 92 bool was_running; 93 94 { 95 QMutexLocker locker(&lock); 96 was_running = running; 97 error = false; 98 } 99 95 100 if (was_running) 96 101 { 97 102 VERBOSE(VB_IMPORTANT, LOC_ERR + "Start(): Already running."); … … 103 108 { 104 109 VERBOSE(VB_IMPORTANT, 105 110 LOC_ERR + QString("Start(): pthread_create failed.") + ENO); 111 112 QMutexLocker locker(&lock); 106 113 error = true; 107 114 } 108 115 } … … 117 124 used = 0; 118 125 readPtr = buffer; 119 126 writePtr = buffer; 127 128 error = false; 120 129 } 121 130 122 131 void DeviceReadBuffer::Stop(void) 123 132 { 124 133 bool was_running = IsRunning(); 125 lock.lock();126 run = false;127 lock.unlock();128 134 135 { 136 QMutexLocker locker(&lock); 137 run = false; 138 } 139 129 140 if (!was_running) 130 141 { 131 142 VERBOSE(VB_IMPORTANT, LOC_ERR + "Stop(): Not running."); … … 143 154 144 155 void DeviceReadBuffer::SetPaused(bool val) 145 156 { 146 lock.lock(); 147 paused = val; 148 lock.unlock(); 157 { 158 QMutexLocker locker(&lock); 159 paused = val; 160 } 161 149 162 if (val) 150 163 pauseWait.wakeAll(); 151 164 else … … 233 246 { 234 247 uint errcnt = 0; 235 248 236 lock.lock(); 237 run = true; 238 running = true; 239 lock.unlock(); 249 { 250 QMutexLocker locker(&lock); 251 run = true; 252 running = true; 253 } 240 254 241 255 while (run) 242 256 { … … 252 266 if (using_poll && !Poll()) 253 267 continue; 254 268 269 { 270 QMutexLocker locker(&lock); 271 if (error) 272 { 273 VERBOSE(VB_RECORD, LOC + "fill_ringbuffer: error state"); 274 break; 275 } 276 } 277 255 278 // Limit read size for faster return from read 256 279 size_t read_size = 257 280 min(dev_read_size, (size_t) WaitForUnused(TSPacket::SIZE)); … … 272 295 } 273 296 } 274 297 275 lock.lock();298 QMutexLocker locker(&lock); 276 299 running = false; 277 lock.unlock();278 300 } 279 301 280 302 bool DeviceReadBuffer::HandlePausing(void) … … 297 319 return true; 298 320 } 299 321 300 bool DeviceReadBuffer::Poll(void) const322 bool DeviceReadBuffer::Poll(void) 301 323 { 302 324 #ifdef USING_MINGW 303 325 #warning mingw DeviceReadBuffer::Poll … … 306 328 return false; 307 329 #else 308 330 bool retval = true; 309 while (true) 331 uint timeout_cnt = 0; 332 333 for (;;) 310 334 { 311 335 struct pollfd polls; 312 336 polls.fd = _stream_fd; 313 337 polls.events = POLLIN; 314 338 polls.revents = 0; 315 339 316 int ret = poll(&polls, 1 /*number of polls*/, 10 /*msec*/); 317 if (IsPauseRequested() || !IsOpen() || !run) 340 int ret = poll(&polls, 1 /*number of polls*/, 250 /*msec*/); 341 342 if (polls.revents & (POLLERR | POLLHUP | POLLNVAL)) 318 343 { 344 VERBOSE(VB_IMPORTANT, LOC + "poll error"); 345 error = true; 346 return true; 347 } 348 349 if (!run || !IsOpen() || IsPauseRequested()) 350 { 319 351 retval = false; 320 352 break; // are we supposed to pause, stop, etc. 321 353 } 322 354 323 355 if (ret > 0) 324 356 break; // we have data to read :) 325 if ((-1 == ret) && (EOVERFLOW == errno)) 326 break; // we have an error to handle 327 328 if ((-1 == ret) && ((EAGAIN == errno) || (EINTR == errno))) 329 continue; // errors that tell you to try again 330 if (ret == 0) 331 continue; // timed out, try again 332 333 usleep(2500); 357 if (ret < 0) 358 { 359 if ((EOVERFLOW == errno)) 360 break; // we have an error to handle 361 if ((EAGAIN == errno) || (EINTR == errno)) 362 continue; // errors that tell you to try again 363 usleep(2500); 364 } 365 else // ret == 0 366 { 367 if (++timeout_cnt > 9) 368 { 369 VERBOSE(VB_RECORD, LOC_ERR + "Poll giving up"); 370 QMutexLocker locker(&lock); 371 error = true; 372 return true; 373 } 374 if (timeout_cnt % 2) 375 VERBOSE(VB_RECORD, LOC_ERR + QString("Poll timeout (%1)") 376 .arg(timeout_cnt)); 377 } 334 378 } 335 379 return retval; 336 380 #endif //!USING_MINGW … … 364 408 365 409 if (++errcnt > 5) 366 410 { 367 lock.lock(); 411 QMutexLocker locker(&lock); 412 VERBOSE(VB_RECORD, LOC + "Too many errors."); 368 413 error = true; 369 lock.unlock();370 414 return false; 371 415 } 372 416 … … 380 424 VERBOSE(VB_IMPORTANT, LOC + 381 425 QString("End-Of-File? fd(%1)").arg(_stream_fd)); 382 426 383 lock.lock();427 QMutexLocker locker(&lock); 384 428 eof = true; 385 lock.unlock();386 387 429 return false; 388 430 } 389 431 usleep(500);