Changeset 198 for trunk/src


Ignore:
Timestamp:
Oct 4, 2009, 1:23:44 AM (16 years ago)
Author:
Dmitry A. Kuminov
Message:

corelib: QProcess: Replace the lock for byte counters with QAtomicInt.

Location:
trunk/src/corelib/io
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/corelib/io/qprocess_os2.cpp

    r197 r198  
    126126    static void removeProcess(USHORT procKey);
    127127
    128     static QMutex *mutex() { return &mtx; }
    129 
    130128private:
    131129
     
    162160
    163161    static QProcessManager *instance;
    164     static QMutex mtx;
     162    static QMutex mx;
    165163};
    166164
    167165// static
    168166QProcessManager *QProcessManager::instance = 0;
    169 QMutex QProcessManager::mtx;
     167QMutex QProcessManager::mx;
    170168
    171169// static
     
    202200void QProcessManager::addRef()
    203201{
    204     QMutexLocker locker(mutex());
     202    QMutexLocker locker();
    205203
    206204    if (instance == 0) {
     
    214212void QProcessManager::release()
    215213{
    216     QMutexLocker locker(mutex());
     214    QMutexLocker locker();
    217215
    218216    Q_ASSERT(instance);
     
    245243#endif
    246244
    247     QMutexLocker locker(mutex());
     245    QMutexLocker locker();
    248246    Q_ASSERT(instance);
    249247
     
    301299void QProcessManager::removeProcess(USHORT procKey)
    302300{
    303     QMutexLocker locker(mutex());
     301    QMutexLocker locker();
    304302    Q_ASSERT(instance);
    305303
     
    392390    // and all complex work is done here asynchronously.
    393391
    394     QMutexLocker locker(mutex());
     392    QMutexLocker locker();
    395393    APIRET arc;
    396394
     
    480478                // data to read, or if it fails to write to a (closed) pipe
    481479            } else {
    482                 d->pipeData[type].bytes = pipeStates[i].usAvail;
     480                // update the counter
     481                if (d->pipeData[type].newBytes.fetchAndStoreRelaxed(pipeStates[i].usAvail) != 0) {
     482                    // the object didn't process the previous notification yet;
     483                    // there is no point to send a new one
     484                    continue;
     485                }
    483486            }
    484 
    485             if (d->pipeData[type].isNew) {
    486                 // the object didn't process the previous notification yet;
    487                 // there is no point to send a new one
    488                 continue;
    489             }
    490 
    491             d->pipeData[type].isNew = true;
    492487
    493488            // signal the process object
     
    532527    waitSem = NULLHANDLE;
    533528
    534     // Note: after calling QProcessManager::addProcess(), pipeData member must
    535     // only be accessed from under QProcessManager::mutex()!
    536     memset(pipeData, sizeof(pipeData), 0);
     529    pipeData[InPipe].bytesLeft = 0;
     530    pipeData[OutPipe].bytesLeft = 0;
     531    pipeData[ErrPipe].bytesLeft = 0;
     532
    537533    procKey = QProcessManager::InvalidProcKey;
    538534
     
    992988qint64 QProcessPrivate::bytesAvailableFromStdout() const
    993989{
    994     qint64 available = 0;
    995     {
    996         QMutexLocker lock(QProcessManager::mutex());
    997         available = pipeData[OutPipe].bytes;
    998         // accept new messages from QProcessManager
    999         const_cast<QProcessPrivate*>(this)->pipeData[OutPipe].isNew = false;
    1000     }
    1001 #if defined (QPROCESS_DEBUG)
    1002     qDebug("QProcessPrivate::bytesAvailableFromStdout() == %lld", available);
    1003 #endif
    1004     return available;
     990    ;
     991
     992   
     993    ;
     994   
     995
     996#if defined (QPROCESS_DEBUG)
     997    qDebug("QProcessPrivate::bytesAvailableFromStdout() == %lld",
     998    );
     999#endif
     1000    return ;
    10051001}
    10061002
    10071003qint64 QProcessPrivate::bytesAvailableFromStderr() const
    10081004{
    1009     qint64 available = 0;
    1010     {
    1011         QMutexLocker lock(QProcessManager::mutex());
    1012         available = pipeData[ErrPipe].bytes;
    1013         // accept new messages from QProcessManager
    1014         const_cast<QProcessPrivate*>(this)->pipeData[ErrPipe].isNew = false;
    1015     }
    1016 #if defined (QPROCESS_DEBUG)
    1017     qDebug("QProcessPrivate::bytesAvailableFromStderr() == %lld", available);
    1018 #endif
    1019     return available;
     1005    ;
     1006
     1007   
     1008    ;
     1009   
     1010
     1011#if defined (QPROCESS_DEBUG)
     1012    qDebug("QProcessPrivate::bytesAvailableFromStderr() == %lld",
     1013    );
     1014#endif
     1015    return ;
    10201016}
    10211017
     
    10251021    APIRET arc = DosRead(stdoutChannel.pipe.server, data, maxlen, &actual);
    10261022
    1027     // decrement the counter to make sure bytesAvailableFromStdout() will return
    1028     // a correct number before QProcessManager::run() updates it
    1029     QMutexLocker lock(QProcessManager::mutex());
    1030     if (actual <= (ULONG) pipeData[OutPipe].bytes)
    1031         pipeData[OutPipe].bytes -= (USHORT) actual;
    1032     else
    1033         pipeData[OutPipe].bytes = 0;
    1034 
    1035     qint64 bytesRead = arc == NO_ERROR ? (qint64)actual : -1;
     1023    qint64 bytesRead = -1;
     1024    if (arc == NO_ERROR) {
     1025        bytesRead = (qint64)actual;
     1026        // update our counter
     1027        Q_ASSERT(pipeData[OutPipe].bytesLeft >= bytesRead);
     1028        pipeData[OutPipe].bytesLeft -= bytesRead;
     1029    }
    10361030
    10371031#if defined QPROCESS_DEBUG
     
    10471041    APIRET arc = DosRead(stderrChannel.pipe.server, data, maxlen, &actual);
    10481042
    1049     // decrement the counter to make sure bytesAvailableFromStderr() will return
    1050     // a correct number before QProcessManager::run() updates it
    1051     QMutexLocker lock(QProcessManager::mutex());
    1052     if (actual <= (ULONG) pipeData[ErrPipe].bytes)
    1053         pipeData[ErrPipe].bytes -= (USHORT) actual;
    1054     else
    1055         pipeData[ErrPipe].bytes = 0;
    1056 
    1057     qint64 bytesRead = arc == NO_ERROR ? (qint64)actual : -1;
     1043    qint64 bytesRead = -1;
     1044    if (arc == NO_ERROR) {
     1045        bytesRead = (qint64)actual;
     1046        // update our counter
     1047        Q_ASSERT(pipeData[ErrPipe].bytesLeft >= bytesRead);
     1048        pipeData[ErrPipe].bytesLeft -= bytesRead;
     1049    }
    10581050
    10591051#if defined QPROCESS_DEBUG
     
    10711063    qint64 written = arc == NO_ERROR ? (qint64)actual : -1;
    10721064
    1073     // accept new messages from QProcessManager
    1074     QMutexLocker lock(QProcessManager::mutex());
    1075     const_cast<QProcessPrivate*>(this)->pipeData[InPipe].isNew = false;
     1065    QProcessPrivate* that = const_cast<QProcessPrivate*>(this);
     1066
     1067    // reset the counter so that QProcessManager will inform us again if
     1068    // more space is available (note that we don't care about the actual number)
     1069    that->pipeData[InPipe].newBytes.fetchAndStoreRelaxed(0);
    10761070
    10771071#if defined QPROCESS_DEBUG
     
    11741168                failed = true;
    11751169            } else {
    1176                 QMutexLocker lock(QProcessManager::mutex());
    1177 
    11781170                bool readyReadEmitted = false;
    1179                 if (pipeData[OutPipe].bytes) {
     1171                if (pipeData[OutPipe].) {
    11801172                    bool canRead = _q_canReadStandardOutput();
    11811173                    if (processChannel == QProcess::StandardOutput && canRead)
    11821174                        readyReadEmitted = true;
    11831175                }
    1184                 if (pipeData[ErrPipe].bytes) {
     1176                if (pipeData[ErrPipe].) {
    11851177                    bool canRead = _q_canReadStandardError();
    11861178                    if (processChannel == QProcess::StandardError && canRead)
     
    11921184                }
    11931185
    1194                 if (pipeData[InPipe].bytes)
     1186                if (pipeData[InPipe].)
    11951187                    _q_canWrite();
    11961188
     
    12421234                failed = true;
    12431235            } else {
    1244                 QMutexLocker lock(QProcessManager::mutex());
    1245 
    1246                 if (pipeData[InPipe].bytes) {
     1236                if (pipeData[InPipe].newBytes.fetchAndAddRelaxed(0)) {
    12471237                    waitMode.fetchAndStoreRelaxed(Async);
    12481238                    return _q_canWrite();
    12491239                }
    12501240
    1251                 if (pipeData[OutPipe].bytes)
     1241                if (pipeData[OutPipe].)
    12521242                    _q_canReadStandardOutput();
    12531243
    1254                 if (pipeData[ErrPipe].bytes)
     1244                if (pipeData[ErrPipe].)
    12551245                    _q_canReadStandardError();
    12561246
     
    13031293                failed = true;
    13041294            } else {
    1305                 QMutexLocker lock(QProcessManager::mutex());
    1306 
    1307                 if (pipeData[InPipe].bytes)
     1295                if (pipeData[InPipe].newBytes.fetchAndAddRelaxed(0))
    13081296                    _q_canWrite();
    13091297
    1310                 if (pipeData[OutPipe].bytes)
     1298                if (pipeData[OutPipe].)
    13111299                    _q_canReadStandardOutput();
    13121300
    1313                 if (pipeData[ErrPipe].bytes)
     1301                if (pipeData[ErrPipe].)
    13141302                    _q_canReadStandardError();
    13151303
     
    13501338        qDosNI(arc = DosWaitEventSem(waitSem, (ULONG)msecs));
    13511339        if (arc == NO_ERROR) {
    1352             QMutexLocker lock(QProcessManager::mutex());
    1353             ret = pipeData[InPipe].bytes;
     1340            ret = pipeData[InPipe].newBytes.fetchAndAddRelaxed(0);
    13541341        }
    13551342    }
  • trunk/src/corelib/io/qprocess_p.h

    r197 r198  
    236236    HEV waitSem;
    237237    struct PipeData {
    238         qint64 bytes;
    239         bool isNew;
     238        ytes;
     239        ;
    240240    };
    241241    PipeData pipeData[3]; // size must match PipeType!
Note: See TracChangeset for help on using the changeset viewer.