ClickHouse/base/poco/Foundation/src/TaskManager.cpp
Robert Schulze b79ead9c84
Move poco to base/poco/ (#46075)
* Replicate poco into base/poco/

* De-register poco submodule

* Build poco from ClickHouse

* Exclude poco from stylecheck

* Exclude poco from whitespace check

* Exclude poco from typo check

* Remove x bit from sources/headers (the style check complained)

* Exclude poco from duplicate include check

* Fix fasttest

* Remove contrib/poco-cmake/*

* Simplify poco build descriptions

* Remove poco stuff not used by ClickHouse

* Glob poco sources

* Exclude poco from clang-tidy
2023-02-08 12:04:11 +01:00

157 lines
2.7 KiB
C++

//
// TaskManager.cpp
//
// Library: Foundation
// Package: Tasks
// Module: Tasks
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/TaskManager.h"
#include "Poco/TaskNotification.h"
#include "Poco/ThreadPool.h"
namespace Poco {
const int TaskManager::MIN_PROGRESS_NOTIFICATION_INTERVAL = 100000; // 100 milliseconds
TaskManager::TaskManager():
_threadPool(ThreadPool::defaultPool())
{
}
TaskManager::TaskManager(ThreadPool& pool):
_threadPool(pool)
{
}
TaskManager::~TaskManager()
{
}
void TaskManager::start(Task* pTask)
{
TaskPtr pAutoTask(pTask); // take ownership immediately
FastMutex::ScopedLock lock(_mutex);
pAutoTask->setOwner(this);
pAutoTask->setState(Task::TASK_STARTING);
_taskList.push_back(pAutoTask);
try
{
_threadPool.start(*pAutoTask, pAutoTask->name());
}
catch (...)
{
// Make sure that we don't act like we own the task since
// we never started it. If we leave the task on our task
// list, the size of the list is incorrect.
_taskList.pop_back();
throw;
}
}
void TaskManager::cancelAll()
{
FastMutex::ScopedLock lock(_mutex);
for (TaskList::iterator it = _taskList.begin(); it != _taskList.end(); ++it)
{
(*it)->cancel();
}
}
void TaskManager::joinAll()
{
_threadPool.joinAll();
}
TaskManager::TaskList TaskManager::taskList() const
{
FastMutex::ScopedLock lock(_mutex);
return _taskList;
}
void TaskManager::addObserver(const AbstractObserver& observer)
{
_nc.addObserver(observer);
}
void TaskManager::removeObserver(const AbstractObserver& observer)
{
_nc.removeObserver(observer);
}
void TaskManager::postNotification(const Notification::Ptr& pNf)
{
_nc.postNotification(pNf);
}
void TaskManager::taskStarted(Task* pTask)
{
_nc.postNotification(new TaskStartedNotification(pTask));
}
void TaskManager::taskProgress(Task* pTask, float progress)
{
ScopedLockWithUnlock<FastMutex> lock(_mutex);
if (_lastProgressNotification.isElapsed(MIN_PROGRESS_NOTIFICATION_INTERVAL))
{
_lastProgressNotification.update();
lock.unlock();
_nc.postNotification(new TaskProgressNotification(pTask, progress));
}
}
void TaskManager::taskCancelled(Task* pTask)
{
_nc.postNotification(new TaskCancelledNotification(pTask));
}
void TaskManager::taskFinished(Task* pTask)
{
_nc.postNotification(new TaskFinishedNotification(pTask));
FastMutex::ScopedLock lock(_mutex);
for (TaskList::iterator it = _taskList.begin(); it != _taskList.end(); ++it)
{
if (*it == pTask)
{
_taskList.erase(it);
break;
}
}
}
void TaskManager::taskFailed(Task* pTask, const Exception& exc)
{
_nc.postNotification(new TaskFailedNotification(pTask, exc));
}
} // namespace Poco