Frobby 0.9.5
TaskEngine.cpp
Go to the documentation of this file.
1/* Frobby: Software for monomial ideal computations.
2 Copyright (C) 2009 University of Aarhus
3 Contact Bjarke Hammersholt Roune for license information (www.broune.com)
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see http://www.gnu.org/licenses/.
17*/
18#include "stdinc.h"
19#include "TaskEngine.h"
20
21#include "Task.h"
22#include "display.h"
23
25 _totalTasksEver(0) {
26}
27
29 while (!_tasks.empty()) {
30 dispose(_tasks.back());
31 _tasks.pop_back();
32 }
33}
34
36 ASSERT(task != 0);
37
38 try {
39 _tasks.push_back(task);
40 } catch (...) {
41 // We should only get an exception if insertion failed.
42 ASSERT(_tasks.empty() || _tasks.back() != task);
43 dispose(task);
44 throw;
45 }
46
48}
49
51 if (_tasks.empty())
52 return false;
53
54 Task* task = _tasks.back();
55 _tasks.pop_back();
56 task->run(*this);
57
58 return true;
59}
60
62 while (runNextTask())
63 ;
64}
65
67 return _totalTasksEver;
68}
69
71 ASSERT(task != 0);
72
73 try {
74 task->dispose();
75 } catch (...) {
76 displayInternalError("Task::dispose() threw an exception.");
77 ASSERT(false);
78 throw; // Lesser evil compared to ignoring the exception.
79 }
80}
void dispose(Task *task)
Definition: TaskEngine.cpp:70
void addTask(Task *task)
Add a task at the head of the list of pending tasks.
Definition: TaskEngine.cpp:35
size_t getTotalTasksEver()
Returns the number of times addTask has been successfully called.
Definition: TaskEngine.cpp:66
bool runNextTask()
Run the most recently added task that has not been run yet.
Definition: TaskEngine.cpp:50
vector< Task * > _tasks
Definition: TaskEngine.h:80
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
void runTasks()
Runs all pending tasks.
Definition: TaskEngine.cpp:61
A Task object represents a unit of work that is performed when the method run() is called.
Definition: Task.h:27
virtual void run(TaskEngine &engine)=0
Does whatever work this task represents.
virtual void dispose()=0
Called when the task is no longer used but run has not and will not be called.
void displayInternalError(const string &msg)
Display msg to standard in a way that indicates that it is an internal error.
Definition: display.cpp:143
This file contains functions for printing strings to standard error.
#define ASSERT(X)
Definition: stdinc.h:86