Poco: fix endless execution of task that was cancelled before run() method

This commit is contained in:
Pavel Kartavyy 2016-08-18 19:43:39 +03:00
parent 9c8697655e
commit 50c28f08b4
2 changed files with 16 additions and 4 deletions

View File

@ -26,6 +26,8 @@
#include "Poco/Mutex.h"
#include "Poco/Event.h"
#include <atomic>
namespace Poco {
@ -140,7 +142,7 @@ private:
std::string _name;
TaskManager* _pOwner;
float _progress;
TaskState _state;
std::atomic<TaskState> _state;
Event _cancelEvent;
mutable FastMutex _mutex;

View File

@ -17,7 +17,8 @@
#include "Poco/Task.h"
#include "Poco/TaskManager.h"
#include "Poco/Exception.h"
#include <iostream>
#include <array>
namespace Poco {
@ -61,8 +62,17 @@ void Task::run()
pOwner->taskStarted(this);
try
{
_state = TASK_RUNNING;
runTask();
/** Task can be already cancelled.
* To prevent endless executing already cancelled task _state is assigned to TASK_RUNNING only if _state != TASK_CANCELLING
*/
std::array<TaskState, 3> allowed_states{TASK_IDLE, TASK_STARTING, TASK_FINISHED};
for (auto & expected : allowed_states)
if (_state.compare_exchange_strong(expected, TASK_RUNNING))
break;
if (_state == TASK_RUNNING)
runTask();
}
catch (Exception& exc)
{