TaskEngine.cpp
Go to the documentation of this file.
/* Frobby: Software for monomial ideal computations.
Copyright (C) 2009 University of Aarhus
Contact Bjarke Hammersholt Roune for license information (www.broune.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
*/
#include "stdinc.h"
#include "TaskEngine.h"
#include "Task.h"
#include "display.h"
TaskEngine::TaskEngine():
_totalTasksEver(0) {
}
TaskEngine::~TaskEngine() {
while (!_tasks.empty()) {
dispose(_tasks.back());
_tasks.pop_back();
}
}
void TaskEngine::addTask(Task* task) {
ASSERT(task != 0);
try {
_tasks.push_back(task);
} catch (...) {
// We should only get an exception if insertion failed.
ASSERT(_tasks.empty() || _tasks.back() != task);
dispose(task);
throw;
}
++_totalTasksEver;
}
bool TaskEngine::runNextTask() {
if (_tasks.empty())
return false;
Task* task = _tasks.back();
_tasks.pop_back();
task->run(*this);
return true;
}
void TaskEngine::runTasks() {
while (runNextTask())
;
}
size_t TaskEngine::getTotalTasksEver() {
return _totalTasksEver;
}
void TaskEngine::dispose(Task* task) {
ASSERT(task != 0);
try {
task->dispose();
} catch (...) {
displayInternalError("Task::dispose() threw an exception.");
ASSERT(false);
throw; // Lesser evil compared to ignoring the exception.
}
}
TaskEngine.h
Task.h
TaskEngine::dispose
void dispose(Task *task)
Definition TaskEngine.cpp:70
TaskEngine::addTask
void addTask(Task *task)
Add a task at the head of the list of pending tasks.
Definition TaskEngine.cpp:35
TaskEngine::getTotalTasksEver
size_t getTotalTasksEver()
Returns the number of times addTask has been successfully called.
Definition TaskEngine.cpp:66
TaskEngine::TaskEngine
TaskEngine()
Definition TaskEngine.cpp:24
TaskEngine::runNextTask
bool runNextTask()
Run the most recently added task that has not been run yet.
Definition TaskEngine.cpp:50
TaskEngine::~TaskEngine
~TaskEngine()
Definition TaskEngine.cpp:28
TaskEngine::_tasks
vector< Task * > _tasks
Definition TaskEngine.h:80
TaskEngine::_totalTasksEver
size_t _totalTasksEver
This is used for statistics so that it is not a disaster if this overflows for very long-running comp...
Definition TaskEngine.h:78
TaskEngine::runTasks
void runTasks()
Runs all pending tasks.
Definition TaskEngine.cpp:61
Task
A Task object represents a unit of work that is performed when the method run() is called.
Definition Task.h:27
Task::run
virtual void run(TaskEngine &engine)=0
Does whatever work this task represents.
Task::dispose
virtual void dispose()=0
Called when the task is no longer used but run has not and will not be called.
displayInternalError
void displayInternalError(const string &msg)
Display msg to standard in a way that indicates that it is an internal error.
Definition display.cpp:143
display.h
This file contains functions for printing strings to standard error.
stdinc.h
This header file includes common definitions and is included as the first line of code in every imple...
ASSERT
#define ASSERT(X)
Definition stdinc.h:86