Skip to content

Commit 9b4be7a

Browse files
committed
Simplify QgsTask::waitForFinished, hopefully fix bugs
1 parent 28fa839 commit 9b4be7a

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

python/core/auto_generated/qgstaskmanager.sip.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ be canceled if any of these layers are about to be removed.
178178
.. seealso:: :py:func:`setDependentLayers`
179179
%End
180180

181-
bool waitForFinished( unsigned long timeout = 30000 );
181+
bool waitForFinished( int timeout = 30000 );
182182
%Docstring
183183
Blocks the current thread until the task finishes or a maximum of ``timeout`` milliseconds.
184184
If ``timeout`` is ``0`` the thread will be blocked forever.

src/core/qgstaskmanager.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ QList<QgsMapLayer *> QgsTask::dependentLayers() const
144144
return _qgis_listQPointerToRaw( mDependentLayers );
145145
}
146146

147-
bool QgsTask::waitForFinished( unsigned long timeout )
147+
bool QgsTask::waitForFinished( int timeout )
148148
{
149149
bool rv = true;
150150
if ( mOverallStatus == Complete || mOverallStatus == Terminated )
@@ -154,8 +154,16 @@ bool QgsTask::waitForFinished( unsigned long timeout )
154154
else
155155
{
156156
if ( timeout == 0 )
157-
timeout = ULONG_MAX;
158-
rv = mTaskFinished.wait( &mNotFinishedMutex, timeout );
157+
timeout = std::numeric_limits< int >::max();
158+
if ( mNotFinishedMutex.tryLock( timeout ) )
159+
{
160+
mNotFinishedMutex.unlock();
161+
rv = true;
162+
}
163+
else
164+
{
165+
rv = false;
166+
}
159167
}
160168
return rv;
161169
}
@@ -253,9 +261,7 @@ void QgsTask::processSubTasksForCompletion()
253261
setProgress( 100.0 );
254262
emit statusChanged( Complete );
255263
emit taskCompleted();
256-
mTaskFinished.wakeAll();
257264
mNotFinishedMutex.unlock();
258-
mTaskFinished.wakeAll();
259265
}
260266
else if ( mStatus == Complete )
261267
{
@@ -282,9 +288,7 @@ void QgsTask::processSubTasksForTermination()
282288

283289
emit statusChanged( Terminated );
284290
emit taskTerminated();
285-
mTaskFinished.wakeAll();
286291
mNotFinishedMutex.unlock();
287-
mTaskFinished.wakeAll();
288292
}
289293
else if ( mStatus == Terminated && !subTasksTerminated )
290294
{

src/core/qgstaskmanager.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class CORE_EXPORT QgsTask : public QObject
204204
*
205205
* The result will be false if the wait timed out and true in any other case.
206206
*/
207-
bool waitForFinished( unsigned long timeout = 30000 );
207+
bool waitForFinished( int timeout = 30000 );
208208

209209
signals:
210210

@@ -312,8 +312,6 @@ class CORE_EXPORT QgsTask : public QObject
312312
bool mShouldTerminate = false;
313313
int mStartCount = 0;
314314

315-
QWaitCondition mTaskFinished;
316-
317315
struct SubTask
318316
{
319317
SubTask( QgsTask *task, const QgsTaskList &dependencies, SubTaskDependency dependency )

0 commit comments

Comments
 (0)