1 /* Declarations to support tasks.
2    Copyright 2002 Paul Twohey.
3 
4 This file is part of VMIPS.
5 
6 VMIPS is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2 of the License, or (at your
9 option) any later version.
10 
11 VMIPS is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15 
16 You should have received a copy of the GNU General Public License along
17 with VMIPS; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19 
20 #ifndef _TASK_H_
21 #define _TASK_H_
22 
23 /* A Task is the unit of deferred action used by Clock. See clock.h */
24 class Task
25 {
26 public:
Task()27 	Task() { }
~Task()28 	virtual ~Task() { }
29 
30 	/* The task to be performed. */
31 	virtual void task() = 0;
32 };
33 
34 /* A CancelableTask is a task that may not be needed when it comes due. This
35    is useful when a task refers to another object that may be delete'd when
36    task() is called. */
37 class CancelableTask : public Task
38 {
39 public:
CancelableTask()40 	CancelableTask() : needed(true) { }
~CancelableTask()41 	virtual ~CancelableTask() { }
42 
43 	/* Mark this task is no longer needed. When task() is called it will
44 	   not call real_task() */
cancel()45 	virtual void cancel() { needed = false; }
46 
47 	/* Perform real_task() if it is still needed, otherwise return. */
task()48 	virtual void task() { if (needed) real_task(); }
49 
50 protected:
51 	/* The real task to be performed. */
52 	virtual void real_task() = 0;
53 
54 protected:
55 	bool	needed;		// true iff the task is needed
56 };
57 
58 #endif /* _TASK_H_ */
59