epwing:replace qthreadpool with qtconcurrent

This commit is contained in:
Xiao YiFang 2023-04-08 12:13:41 +08:00 committed by xiaoyifang
parent f69f248c00
commit 4ffff72698

View file

@ -862,69 +862,37 @@ sptr< Dictionary::DataRequest > EpwingDictionary::getArticle( wstring const & wo
//// EpwingDictionary::getResource()
class EpwingResourceRequest;
class EpwingResourceRequestRunnable: public QRunnable
{
EpwingResourceRequest & r;
QSemaphore & hasExited;
public:
EpwingResourceRequestRunnable( EpwingResourceRequest & r_,
QSemaphore & hasExited_ ): r( r_ ),
hasExited( hasExited_ )
{}
~EpwingResourceRequestRunnable()
{
hasExited.release();
}
void run() override;
};
class EpwingResourceRequest: public Dictionary::DataRequest
{
friend class EpwingResourceRequestRunnable;
EpwingDictionary & dict;
string resourceName;
QAtomicInt isCancelled;
QSemaphore hasExited;
QFuture< void > f;
public:
EpwingResourceRequest( EpwingDictionary & dict_,
string const & resourceName_ ):
EpwingResourceRequest( EpwingDictionary & dict_, string const & resourceName_ ):
dict( dict_ ),
resourceName( resourceName_ )
{
QThreadPool::globalInstance()->start(
new EpwingResourceRequestRunnable( *this, hasExited ) );
f = QtConcurrent::run( [ this ]() {
this->run();
} );
}
void run(); // Run from another thread by EpwingResourceRequestRunnable
void cancel() override
{
isCancelled.ref();
}
void cancel() override { isCancelled.ref(); }
~EpwingResourceRequest()
{
isCancelled.ref();
hasExited.acquire();
f.waitForFinished();
}
};
void EpwingResourceRequestRunnable::run()
{
r.run();
}
void EpwingResourceRequest::run()
{
// Some runnables linger enough that they are cancelled before they start