Ticket #3381: fifowriter.rev2.patch

File fifowriter.rev2.patch, 12.2 KB (added by russell@…, 17 years ago)
  • hdtvrecorder.h

     
    8585    // Data for managing the device ringbuffer
    8686    struct {
    8787        pthread_t        thread;
    88         mutable pthread_mutex_t lock;
    89         mutable pthread_mutex_t lock_stats;
     88        mutable QMutex lock;
     89        mutable QMutex lock_stats;
    9090
    9191        bool             run;
    9292        bool             eof;
  • hdtvrecorder.cpp

     
    128128
    129129    ringbuf.run = false;
    130130    ringbuf.buffer = 0;
    131     pthread_mutex_init(&ringbuf.lock, NULL);
    132     pthread_mutex_init(&ringbuf.lock_stats, NULL);
    133131    loop = random() % (report_loops / 2);
    134132}
    135133
     
    156154HDTVRecorder::~HDTVRecorder()
    157155{
    158156    TeardownAll();
    159     pthread_mutex_destroy(&ringbuf.lock);
    160     pthread_mutex_destroy(&ringbuf.lock_stats);
    161157}
    162158
    163159void HDTVRecorder::SetOptionsFromProfile(RecordingProfile *profile,
     
    296292    size_t    read_size;
    297293    bool      run, request_pause, paused;
    298294
    299     pthread_mutex_lock(&ringbuf.lock);
     295    ringbuf.lock.lock();
    300296    ringbuf.run = true;
    301     pthread_mutex_unlock(&ringbuf.lock);
     297    ringbuf.lock.unlock();
    302298
    303299    for (;;)
    304300    {
    305         pthread_mutex_lock(&ringbuf.lock);
     301        ringbuf.lock.lock();
    306302        run = ringbuf.run;
    307303        unused = ringbuf.size - ringbuf.used;
    308304        request_pause = ringbuf.request_pause;
    309305        paused = ringbuf.paused;
    310         pthread_mutex_unlock(&ringbuf.lock);
     306        ringbuf.lock.unlock();
    311307
    312308        if (!run)
    313309            break;
    314310
    315311        if (request_pause)
    316312        {
    317             pthread_mutex_lock(&ringbuf.lock);
     313            ringbuf.lock.lock();
    318314            ringbuf.paused = true;
    319             pthread_mutex_unlock(&ringbuf.lock);
     315            ringbuf.lock.unlock();
    320316
    321317            pauseWait.wakeAll();
    322318            if (tvrec)
     
    327323        }
    328324        else if (paused)
    329325        {
    330             pthread_mutex_lock(&ringbuf.lock);
     326            QMutexLocker locker(&ringbuf.lock);
    331327            ringbuf.writePtr = ringbuf.readPtr = ringbuf.buffer;
    332328            ringbuf.used = 0;
    333329            ringbuf.paused = false;
    334             pthread_mutex_unlock(&ringbuf.lock);
    335330        }
    336331
    337332        contiguous = ringbuf.endPtr - ringbuf.writePtr;
     
    339334        while (unused < TSPacket::SIZE && contiguous > TSPacket::SIZE)
    340335        {
    341336            usleep(500);
     337           
     338            QMutexLocker locker(&ringbuf.lock);
    342339
    343             pthread_mutex_lock(&ringbuf.lock);
    344340            unused = ringbuf.size - ringbuf.used;
    345341            request_pause = ringbuf.request_pause;
    346             pthread_mutex_unlock(&ringbuf.lock);
    347342
    348343            if (request_pause)
    349344                break;
     
    367362            perror("read");
    368363            if (++errcnt > 5)
    369364            {
    370                 pthread_mutex_lock(&ringbuf.lock);
     365                QMutexLocker locker(&ringbuf.lock);
    371366                ringbuf.error = true;
    372                 pthread_mutex_unlock(&ringbuf.lock);
    373 
    374367                break;
    375368            }
    376369
     
    383376            {
    384377                VERBOSE(VB_IMPORTANT, QString("HD8 %1 end of file found.")
    385378                        .arg(videodevice));
    386 
    387                 pthread_mutex_lock(&ringbuf.lock);
     379                QMutexLocker locker(&ringbuf.lock);
    388380                ringbuf.eof = true;
    389                 pthread_mutex_unlock(&ringbuf.lock);
    390 
    391381                break;
    392382            }
    393383            usleep(500);
     
    396386
    397387        errcnt = 0;
    398388
    399         pthread_mutex_lock(&ringbuf.lock);
     389        ringbuf.lock.lock();
    400390        ringbuf.used += len;
    401391        used = ringbuf.used;
    402392        ringbuf.writePtr += len;
    403         pthread_mutex_unlock(&ringbuf.lock);
     393        ringbuf.lock.unlock();
    404394
    405395#ifdef REPORT_RING_STATS
    406         pthread_mutex_lock(&ringbuf.lock_stats);
     396        ringbuf.lock_stats.lock();
    407397
    408398        if (ringbuf.max_used < used)
    409399            ringbuf.max_used = used;
    410400
    411401        ringbuf.avg_used = ((ringbuf.avg_used * ringbuf.avg_cnt) + used)
    412402                           / ++ringbuf.avg_cnt;
    413         pthread_mutex_unlock(&ringbuf.lock_stats);
     403   
     404        ringbuf.lock_stats.unlock();
    414405#endif
    415406
    416407        if (ringbuf.writePtr == ringbuf.endPtr)
     
    432423    bool            dev_error = false;
    433424    bool            dev_eof = false;
    434425
    435     pthread_mutex_lock(&ringbuf.lock);
     426    ringbuf.lock.lock();
    436427    avail = ringbuf.used;
    437     pthread_mutex_unlock(&ringbuf.lock);
     428    ringbuf.lock.unlock();
    438429
    439430    min_read = cnt < ringbuf.min_read ? cnt : ringbuf.min_read;
    440431
     
    445436        if (request_pause || dev_error || dev_eof)
    446437            return 0;
    447438
    448         pthread_mutex_lock(&ringbuf.lock);
     439        QMutexLocker locker(&ringbuf.lock);
     440
    449441        dev_error = ringbuf.error;
    450442        dev_eof = ringbuf.eof;
    451443        avail = ringbuf.used;
    452         pthread_mutex_unlock(&ringbuf.lock);
    453444    }
    454445    if (cnt > avail)
    455446        cnt = avail;
     
    475466        ringbuf.readPtr += cnt;
    476467    }
    477468
    478     pthread_mutex_lock(&ringbuf.lock);
     469    ringbuf.lock.lock();
    479470    ringbuf.used -= cnt;
    480     pthread_mutex_unlock(&ringbuf.lock);
     471    ringbuf.lock.unlock();
    481472
    482473    if (ringbuf.readPtr == ringbuf.endPtr)
    483474        ringbuf.readPtr = ringbuf.buffer;
     
    488479
    489480        if (++loop == report_loops)
    490481        {
     482            QMutexLocker locker(&ringbuf.lock_stats);
     483
    491484            loop = 0;
    492             pthread_mutex_lock(&ringbuf.lock_stats);
    493485            avg = ringbuf.avg_used;
    494486            samples = ringbuf.avg_cnt;
    495487            max = ringbuf.max_used;
    496488            ringbuf.avg_used = 0;
    497489            ringbuf.avg_cnt = 0;
    498490            ringbuf.max_used = 0;
    499             pthread_mutex_unlock(&ringbuf.lock_stats);
    500491
    501492            VERBOSE(VB_IMPORTANT, QString("%1 ringbuf avg %2% max %3%"
    502493                                          " samples %4")
     
    582573    // TRANSFER DATA
    583574    while (_request_recording)
    584575    {
    585         pthread_mutex_lock(&ringbuf.lock);
     576        ringbuf.lock.lock();
    586577        dev_error = ringbuf.error;
    587578        dev_eof = ringbuf.eof;
    588579        pause = ringbuf.paused;
    589         pthread_mutex_unlock(&ringbuf.lock);
     580        ringbuf.lock.unlock();
    590581
    591582        if (request_pause)
    592583        {
    593             pthread_mutex_lock(&ringbuf.lock);
     584            ringbuf.lock.lock();
    594585            ringbuf.request_pause = true;
    595             pthread_mutex_unlock(&ringbuf.lock);
     586            ringbuf.lock.unlock();
    596587
    597588            usleep(1000);
    598589            continue;
    599590        }
    600591        else if (pause)
    601592        {
    602             pthread_mutex_lock(&ringbuf.lock);
     593            ringbuf.lock.lock();
    603594            ringbuf.request_pause = false;
    604             pthread_mutex_unlock(&ringbuf.lock);
     595            ringbuf.lock.unlock();
    605596
    606597            usleep(1500);
    607598            continue;
     
    646637
    647638    _request_recording = false;
    648639
    649     pthread_mutex_lock(&ringbuf.lock);
     640    ringbuf.lock.lock();
    650641    bool run = ringbuf.run;
    651642    ringbuf.run = false;
    652     pthread_mutex_unlock(&ringbuf.lock);
     643    ringbuf.lock.unlock();
    653644
    654645    if (run)
    655646        pthread_join(ringbuf.thread, NULL);
     
    669660
    670661void HDTVRecorder::Pause(bool /*clear*/)
    671662{
    672     pthread_mutex_lock(&ringbuf.lock);
     663    ringbuf.lock.lock();
    673664    ringbuf.paused = false;
    674     pthread_mutex_unlock(&ringbuf.lock);
     665    ringbuf.lock.unlock();
    675666    request_pause = true;
    676667}
    677668
    678669bool HDTVRecorder::IsPaused(void) const
    679670{
    680     pthread_mutex_lock(&ringbuf.lock);
     671    ringbuf.lock.lock();   
    681672    bool paused = ringbuf.paused;
    682     pthread_mutex_unlock(&ringbuf.lock);
     673    ringbuf.lock.unlock();
    683674
    684675    return paused;
    685676}
     
    900891        }
    901892        else
    902893        {
    903             pthread_mutex_lock(&ringbuf.lock);
     894            QMutexLocker locker(&ringbuf.lock);
    904895            ringbuf.used = 0;
    905896            ringbuf.max_used = 0;
    906897            ringbuf.readPtr = ringbuf.writePtr = ringbuf.buffer;
    907             pthread_mutex_unlock(&ringbuf.lock);
    908898        }
    909899        Unpause();
    910900    }
  • fifowriter.cpp

     
    3232    fb_inptr = new struct fifo_buf *[count];
    3333    fb_outptr = new struct fifo_buf *[count];
    3434    fifothrds = new pthread_t[count];
    35     fifo_lock = new pthread_mutex_t [count];
    36     empty_cond = new pthread_cond_t[count];
    37     full_cond = new pthread_cond_t[count];
    38     for (int i = 0; i < count; i++)
    39     {
    40       pthread_cond_init(&empty_cond[i], NULL);
    41       pthread_cond_init(&full_cond[i], NULL);
    42     }
     35    fifo_lock = new QMutex[count];
     36    empty_cond = new QWaitCondition[count];
     37    full_cond = new QWaitCondition[count];
    4338    filename = new QString [count];
    4439    fbdesc = new QString [count];
    4540}
     
    4944    for (int i = 0; i <num_fifos; i++)
    5045    {
    5146        killwr[i] = 1;
    52         pthread_cond_signal(&empty_cond[i]);
     47
     48        fifo_lock[i].lock();
     49        empty_cond[i].wakeOne();
     50        fifo_lock[i].unlock();
     51
    5352        pthread_join(fifothrds[i], NULL);
    5453    }
    5554    delete [] maxblksize;
     
    5958    delete [] fifothrds;
    6059    delete [] full_cond;
    6160    delete [] empty_cond;
     61    delete [] fifo_lock;
    6262    delete [] filename;
    6363    delete [] fbdesc;
    6464    delete [] killwr;
     
    9595    }
    9696    fb_inptr[id]  = fifo_buf[id];
    9797    fb_outptr[id] = fifo_buf[id];
    98     pthread_mutex_init(&fifo_lock[id], NULL);
    9998
    10099    cur_id = id;
    101100
     
    120119{
    121120    int id = cur_id;
    122121    int fd = -1;
    123     pthread_mutex_lock(&fifo_lock[id]);
     122    fifo_lock[id].lock();
    124123    cur_id = -1;
    125124    while (1)
    126125    {
    127126        if (fb_inptr[id] == fb_outptr[id])
    128             pthread_cond_wait(&empty_cond[id],&fifo_lock[id]);
    129         pthread_mutex_unlock(&fifo_lock[id]);
     127            empty_cond[id].wait(&fifo_lock[id]);
     128        fifo_lock[id].unlock();
    130129        if (killwr[id])
    131130            break;
    132131        if (fd == -1)
    133132            fd = open(filename[id].ascii(), O_WRONLY| O_SYNC);
    134133        write(fd, fb_outptr[id]->data, fb_outptr[id]->blksize);
    135         pthread_mutex_lock(&fifo_lock[id]);
     134        fifo_lock[id].lock();
    136135        fb_outptr[id] = fb_outptr[id]->next;
    137         pthread_cond_signal(&full_cond[id]);
     136        full_cond[id].wakeOne();
    138137    }
    139138
    140139    if (fd != -1)
     
    155154
    156155void FIFOWriter::FIFOWrite(int id, void *buffer, long blksize)
    157156{
    158     pthread_mutex_lock(&fifo_lock[id]);
     157    fifo_lock[id].lock();
    159158    while (fb_inptr[id]->next == fb_outptr[id])
    160159    {
    161160        bool blocking = false;
     
    183182        }
    184183        else
    185184        {
    186             struct timespec timeout;
    187             struct timeval now;
    188             gettimeofday(&now, NULL);
    189             timeout.tv_sec = now.tv_sec + 1;
    190             timeout.tv_nsec = now.tv_usec * 1000;
    191             pthread_cond_timedwait(&full_cond[id], &fifo_lock[id], &timeout);
     185            full_cond[id].wait(&fifo_lock[id], 1000);
    192186        }
    193187    }
    194188    if (blksize > maxblksize[id])
     
    199193    memcpy(fb_inptr[id]->data,buffer,blksize);
    200194    fb_inptr[id]->blksize = blksize;
    201195    fb_inptr[id] = fb_inptr[id]->next;
    202     pthread_cond_signal(&empty_cond[id]);
    203     pthread_mutex_unlock(&fifo_lock[id]);
     196    empty_cond[id].wakeOne();
     197    fifo_lock[id].unlock();
    204198}
    205199
    206200void FIFOWriter::FIFODrain(void)
     
    214208            if (fb_inptr[i] == fb_outptr[i])
    215209            {
    216210                killwr[i] = 1;
    217                 pthread_cond_signal(&empty_cond[i]);
     211                fifo_lock[i].lock();
     212                empty_cond[i].wakeOne();
     213                fifo_lock[i].unlock();
    218214                count++;
    219215            }
    220216        }
  • fifowriter.h

     
    44#include <vector>
    55#include <qstring.h>
    66#include <qmutex.h>
     7#include <qwaitcondition.h>
    78#include <qptrqueue.h>
    89
    910#include "mythexp.h"
     
    3233     } **fifo_buf, **fb_inptr, **fb_outptr;
    3334
    3435     pthread_t *fifothrds;
    35      pthread_mutex_t *fifo_lock;
    36      pthread_cond_t *full_cond, *empty_cond;
     36     QMutex *fifo_lock;
     37     QWaitCondition *full_cond, *empty_cond;
    3738
    3839     QString *filename, *fbdesc;
    3940