void
GenericWatcher::stopWatching()
{
    if ( !isRunning() ) {
        return;
    }
    {
        QMutexLocker k(&_imp->tasksMutex);
        _imp->tasks.clear();
    }

    {
        QMutexLocker quitLocker(&_imp->mustQuitMutex);
        _imp->mustQuit = true;


        // Post a stub request to wake up the thread
        {
            QMutexLocker k(&_imp->startRequestsMutex);
            ++_imp->startRequests;
            _imp->startRequestsCond.wakeOne();
        }
        while (_imp->mustQuit) {
            _imp->mustQuitCond.wait(&_imp->mustQuitMutex);
        }
    }

    wait();
}
void
GenericWatcher::scheduleBlockingTask(int taskID,
                                     const boost::shared_ptr<GenericWatcherCallerArgs>& args)
{
    {
        QMutexLocker quitLocker(&_imp->mustQuitMutex);
        if (_imp->mustQuit) {
            return;
        }
    }

    {
        QMutexLocker(&_imp->tasksMutex);
        GenericWatcherPrivate::Task t;
        t.id = taskID;
        t.args = args;
        _imp->tasks.push_back(t);
    }

    if ( !isRunning() ) {
        start();
    } else {
        ///Wake up the thread with a start request
        QMutexLocker locker(&_imp->startRequestsMutex);
        if (_imp->startRequests <= 0) {
            ++_imp->startRequests;
        }
        _imp->startRequestsCond.wakeOne();
    }
}
bool
GenericSchedulerThread::startTask(const ThreadStartArgsPtr& inArgs)
{
    {
        QMutexLocker quitLocker(&_imp->mustQuitMutex);
        if (!_imp->startingThreadAllowed || _imp->mustQuit) {
            return false;
        }
    }

    {
        QMutexLocker k1(&_imp->enqueuedTasksMutex);
        QMutexLocker k2(&_imp->abortRequestedMutex);
#ifdef TRACE_GENERIC_SCHEDULER_THREAD
        qDebug() << QThread::currentThread() << ": Requesting task on" <<  getThreadName().c_str() << ", is thread aborted?" << (bool)(_imp->abortRequested > 0);
#endif
        if (_imp->abortRequested > 0) {
            _imp->queuedTaskWhileProcessingAbort.push_back(inArgs);
        } else {
            _imp->enqueuedTasks.push_back(inArgs);
        }
    }

    if ( !isRunning() ) {
        start();
    } else {
        ///Wake up the thread with a start request
        QMutexLocker locker(&_imp->startRequestsMutex);
        ++_imp->startRequests;

        _imp->startRequestsCond.wakeOne();
    }

    return true;
}
void
GenericWatcher::run()
{
    for (;; ) {
        {
            QMutexLocker quitLocker(&_imp->mustQuitMutex);
            if (_imp->mustQuit) {
                _imp->mustQuit = false;
                _imp->mustQuitCond.wakeAll();

                return;
            }
        }
        int taskID = -1;
        boost::shared_ptr<GenericWatcherCallerArgs> inArgs;
        {
            QMutexLocker k(&_imp->tasksMutex);
            if ( !_imp->tasks.empty() ) {
                const GenericWatcherPrivate::Task& t = _imp->tasks.front();
                taskID = t.id;
                inArgs = t.args;
                _imp->tasks.pop_front();
            }
        }
        if (taskID != -1) {
            handleBlockingTask(taskID);
            Q_EMIT taskFinished(taskID, inArgs);
        }

        {
            QMutexLocker l(&_imp->startRequestsMutex);
            while (_imp->startRequests <= 0) {
                _imp->startRequestsCond.wait(&_imp->startRequestsMutex);
            }
            ///We got the request, reset it back to 0
            _imp->startRequests = 0;
        }
    } // for(;;)
}
示例#5
0
void
HistogramCPU::computeHistogram(int mode,      //< corresponds to the enum Histogram::DisplayMode
                               const boost::shared_ptr<Natron::Image> & image,
                               const RectI & rect,
                               int binsCount,
                               double vmin,
                               double vmax,
                               int smoothingKernelSize)
{
    /*Starting or waking-up the thread*/
    QMutexLocker quitLocker(&_imp->mustQuitMutex);
    QMutexLocker locker(&_imp->requestMutex);

    _imp->requests.push_back( HistogramRequest(binsCount,mode,image,rect,vmin,vmax,smoothingKernelSize) );
    if (!isRunning() && !_imp->mustQuit) {
        quitLocker.unlock();
        start(HighestPriority);
    } else {
        quitLocker.unlock();
        _imp->requestCond.wakeOne();
    }
}