src/network/kernel/qhostinfo.cpp
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
child 37 758a864f9613
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
    42 #include "qhostinfo.h"
    42 #include "qhostinfo.h"
    43 #include "qhostinfo_p.h"
    43 #include "qhostinfo_p.h"
    44 
    44 
    45 #include "QtCore/qscopedpointer.h"
    45 #include "QtCore/qscopedpointer.h"
    46 #include <qabstracteventdispatcher.h>
    46 #include <qabstracteventdispatcher.h>
    47 #include <private/qunicodetables_p.h>
       
    48 #include <qcoreapplication.h>
    47 #include <qcoreapplication.h>
    49 #include <qmetaobject.h>
    48 #include <qmetaobject.h>
    50 #include <qregexp.h>
       
    51 #include <private/qnativesocketengine_p.h>
       
    52 #include <qstringlist.h>
    49 #include <qstringlist.h>
    53 #include <qthread.h>
    50 #include <qthread.h>
    54 #include <qtimer.h>
       
    55 #include <qurl.h>
    51 #include <qurl.h>
    56 
    52 
    57 #ifdef Q_OS_UNIX
    53 #ifdef Q_OS_UNIX
    58 #  include <unistd.h>
    54 #  include <unistd.h>
    59 #endif
    55 #endif
   170     int id = theIdCounter.fetchAndAddRelaxed(1); // generate unique ID
   166     int id = theIdCounter.fetchAndAddRelaxed(1); // generate unique ID
   171 
   167 
   172     if (name.isEmpty()) {
   168     if (name.isEmpty()) {
   173         QHostInfo hostInfo(id);
   169         QHostInfo hostInfo(id);
   174         hostInfo.setError(QHostInfo::HostNotFound);
   170         hostInfo.setError(QHostInfo::HostNotFound);
   175         hostInfo.setErrorString(QObject::tr("No host name given"));
   171         hostInfo.setErrorString(QCoreApplication::translate("QHostInfo", "No host name given"));
   176         QScopedPointer<QHostInfoResult> result(new QHostInfoResult);
   172         QScopedPointer<QHostInfoResult> result(new QHostInfoResult);
   177         QObject::connect(result.data(), SIGNAL(resultsReady(QHostInfo)),
   173         QObject::connect(result.data(), SIGNAL(resultsReady(QHostInfo)),
   178                          receiver, member, Qt::QueuedConnection);
   174                          receiver, member, Qt::QueuedConnection);
   179         result.data()->emitResultsReady(hostInfo);
   175         result.data()->emitResultsReady(hostInfo);
   180         return id;
   176         return id;
   469 
   465 
   470     // signal emission
   466     // signal emission
   471     hostInfo.setLookupId(id);
   467     hostInfo.setLookupId(id);
   472     resultEmitter.emitResultsReady(hostInfo);
   468     resultEmitter.emitResultsReady(hostInfo);
   473 
   469 
       
   470     // now also iterate through the postponed ones
       
   471     QMutableListIterator<QHostInfoRunnable*> iterator(manager->postponedLookups);
       
   472     while (iterator.hasNext()) {
       
   473         QHostInfoRunnable* postponed = iterator.next();
       
   474         if (toBeLookedUp == postponed->toBeLookedUp) {
       
   475             // we can now emit
       
   476             iterator.remove();
       
   477             hostInfo.setLookupId(postponed->id);
       
   478             postponed->resultEmitter.emitResultsReady(hostInfo);
       
   479         }
       
   480     }
       
   481 
   474     manager->lookupFinished(this);
   482     manager->lookupFinished(this);
   475 
   483 
   476     // thread goes back to QThreadPool
   484     // thread goes back to QThreadPool
   477 }
   485 }
   478 
   486 
   486 QHostInfoLookupManager::~QHostInfoLookupManager()
   494 QHostInfoLookupManager::~QHostInfoLookupManager()
   487 {
   495 {
   488     wasDeleted = true;
   496     wasDeleted = true;
   489 
   497 
   490     // don't qDeleteAll currentLookups, the QThreadPool has ownership
   498     // don't qDeleteAll currentLookups, the QThreadPool has ownership
   491     qDeleteAll(postponedLookups);
   499     clear();
   492     qDeleteAll(scheduledLookups);
   500 }
   493     qDeleteAll(finishedLookups);
   501 
       
   502 void QHostInfoLookupManager::clear()
       
   503 {
       
   504     {
       
   505         QMutexLocker locker(&mutex);
       
   506         qDeleteAll(postponedLookups);
       
   507         qDeleteAll(scheduledLookups);
       
   508         qDeleteAll(finishedLookups);
       
   509         postponedLookups.clear();
       
   510         scheduledLookups.clear();
       
   511         finishedLookups.clear();
       
   512     }
       
   513 
       
   514     threadPool.waitForDone();
       
   515     cache.clear();
   494 }
   516 }
   495 
   517 
   496 void QHostInfoLookupManager::work()
   518 void QHostInfoLookupManager::work()
   497 {
   519 {
   498     if (wasDeleted)
   520     if (wasDeleted)
   549                     scheduled = 0;
   571                     scheduled = 0;
   550                     break;
   572                     break;
   551                 }
   573                 }
   552             }
   574             }
   553 
   575 
   554             if (scheduled && threadPool.tryStart(scheduled)) {
   576             if (scheduled && currentLookups.size() < threadPool.maxThreadCount()) {
   555                 // runnable now running in new thread, track this in currentLookups
   577                 // runnable now running in new thread, track this in currentLookups
       
   578                 threadPool.start(scheduled);
   556                 iterator.remove();
   579                 iterator.remove();
   557                 currentLookups.append(scheduled);
   580                 currentLookups.append(scheduled);
   558             } else if (scheduled) {
       
   559                 // wanted to start, but could not because thread pool is busy
       
   560                 break;
       
   561             } else {
   581             } else {
   562                 // was postponed, continue iterating
   582                 // was postponed, continue iterating
   563                 continue;
   583                 continue;
   564             }
   584             }
   565         };
   585         };
   582 {
   602 {
   583     if (wasDeleted)
   603     if (wasDeleted)
   584         return;
   604         return;
   585 
   605 
   586     QMutexLocker locker(&this->mutex);
   606     QMutexLocker locker(&this->mutex);
       
   607 
       
   608     // is postponed? delete and return
       
   609     for (int i = 0; i < postponedLookups.length(); i++) {
       
   610         if (postponedLookups.at(i)->id == id) {
       
   611             delete postponedLookups.takeAt(i);
       
   612             return;
       
   613         }
       
   614     }
       
   615 
       
   616     // is scheduled? delete and return
       
   617     for (int i = 0; i < scheduledLookups.length(); i++) {
       
   618         if (scheduledLookups.at(i)->id == id) {
       
   619             delete scheduledLookups.takeAt(i);
       
   620             return;
       
   621         }
       
   622     }
       
   623 
   587     if (!abortedLookups.contains(id))
   624     if (!abortedLookups.contains(id))
   588         abortedLookups.append(id);
   625         abortedLookups.append(id);
   589 }
   626 }
   590 
   627 
   591 // called from QHostInfoRunnable
   628 // called from QHostInfoRunnable
   634 
   671 
   635 void qt_qhostinfo_clear_cache()
   672 void qt_qhostinfo_clear_cache()
   636 {
   673 {
   637     QHostInfoLookupManager* manager = theHostInfoLookupManager();
   674     QHostInfoLookupManager* manager = theHostInfoLookupManager();
   638     if (manager) {
   675     if (manager) {
   639         manager->cache.clear();
   676         manager->clear();
   640     }
   677     }
   641 }
   678 }
   642 
   679 
   643 void Q_AUTOTEST_EXPORT qt_qhostinfo_enable_cache(bool e)
   680 void Q_AUTOTEST_EXPORT qt_qhostinfo_enable_cache(bool e)
   644 {
   681 {