Panda3D
Loading...
Searching...
No Matches
asyncTask.h
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file asyncTask.h
10 * @author drose
11 * @date 2006-08-23
12 */
13
14#ifndef ASYNCTASK_H
15#define ASYNCTASK_H
16
17#include "pandabase.h"
18#include "asyncFuture.h"
19#include "namable.h"
20#include "pmutex.h"
21#include "conditionVar.h"
22#include "pStatCollector.h"
23
25class AsyncTaskChain;
26
27/**
28 * This class represents a concrete task performed by an AsyncManager.
29 * Normally, you would subclass from this class, and override do_task(), to
30 * define the functionality you wish to have the task perform.
31 */
32class EXPCL_PANDA_EVENT AsyncTask : public AsyncFuture, public Namable {
33public:
34 AsyncTask(const std::string &name = std::string());
35 ALLOC_DELETED_CHAIN(AsyncTask);
36
37PUBLISHED:
38 virtual ~AsyncTask();
39
40 enum DoneStatus {
41 DS_done, // normal task completion
42 DS_cont, // run task again next epoch
43 DS_again, // start the task over from the beginning
44 DS_pickup, // run task again this frame, if frame budget allows
45 DS_exit, // stop the enclosing sequence
46 DS_pause, // pause, then exit (useful within a sequence)
47 DS_interrupt, // interrupt the task manager, but run task again
48 DS_await, // await a different task's completion
49 };
50
51 enum State {
52 S_inactive,
53 S_active,
54 S_servicing,
55 S_servicing_removed, // Still servicing, but wants removal from manager.
56 S_sleeping,
57 S_active_nested, // active within a sequence.
58 S_awaiting, // Waiting for a dependent task to complete
59 };
60
61 INLINE State get_state() const;
62 INLINE bool is_alive() const;
63 INLINE AsyncTaskManager *get_manager() const;
64
65 bool remove();
66
67 INLINE void set_delay(double delay);
68 INLINE void clear_delay();
69 INLINE bool has_delay() const;
70 INLINE double get_delay() const;
71 double get_wake_time() const;
72 void recalc_wake_time();
73
74 INLINE double get_start_time() const;
75 double get_elapsed_time() const;
76 INLINE int get_start_frame() const;
77 int get_elapsed_frames() const;
78
79 void set_name(const std::string &name);
80 INLINE void clear_name();
81 std::string get_name_prefix() const;
82
83 INLINE AtomicAdjust::Integer get_task_id() const;
84
85 void set_task_chain(const std::string &chain_name);
86 INLINE const std::string &get_task_chain() const;
87
88 void set_sort(int sort);
89 INLINE int get_sort() const;
90
91 void set_priority(int priority);
92 INLINE int get_priority() const;
93
94 INLINE void set_done_event(const std::string &done_event);
95
96 INLINE double get_dt() const;
97 INLINE double get_max_dt() const;
98 INLINE double get_average_dt() const;
99
100 virtual void output(std::ostream &out) const;
101
102PUBLISHED:
103 MAKE_PROPERTY(state, get_state);
104 MAKE_PROPERTY(alive, is_alive);
105 MAKE_PROPERTY(manager, get_manager);
106
107 // The name of this task.
108 MAKE_PROPERTY(name, get_name, set_name);
109
110 // This is a number guaranteed to be unique for each different AsyncTask
111 // object in the universe.
112 MAKE_PROPERTY(id, get_task_id);
113
114 MAKE_PROPERTY(task_chain, get_task_chain, set_task_chain);
115 MAKE_PROPERTY(sort, get_sort, set_sort);
116 MAKE_PROPERTY(priority, get_priority, set_priority);
117 MAKE_PROPERTY(done_event, get_done_event, set_done_event);
118
119 MAKE_PROPERTY(dt, get_dt);
120 MAKE_PROPERTY(max_dt, get_max_dt);
121 MAKE_PROPERTY(average_dt, get_average_dt);
122
123protected:
124 void jump_to_task_chain(AsyncTaskManager *manager);
125 DoneStatus unlock_and_do_task();
126
127 virtual bool cancel() final;
128 virtual bool is_task() const final {return true;}
129
130 virtual bool is_runnable();
131 virtual DoneStatus do_task();
132 virtual void upon_birth(AsyncTaskManager *manager);
133 virtual void upon_death(AsyncTaskManager *manager, bool clean_exit);
134
135protected:
136 AtomicAdjust::Integer _task_id;
137 std::string _chain_name;
138 double _delay;
139 bool _has_delay;
140 double _wake_time;
141 int _sort;
142 int _priority;
143 unsigned int _implicit_sort;
144
145 State _state;
146 Thread *_servicing_thread;
147 AsyncTaskChain *_chain;
148
149 double _start_time;
150 int _start_frame;
151
152 double _dt;
153 double _max_dt;
154 double _total_dt;
155 int _num_frames;
156
157 static AtomicAdjust::Integer _next_task_id;
158
159 static PStatCollector _show_code_pcollector;
160 PStatCollector _task_pcollector;
161
162 friend class PythonTask;
163
164public:
165 static TypeHandle get_class_type() {
166 return _type_handle;
167 }
168 static void init_type() {
169 AsyncFuture::init_type();
170 register_type(_type_handle, "AsyncTask",
171 AsyncFuture::get_class_type());
172 }
173 virtual TypeHandle get_type() const {
174 return get_class_type();
175 }
176 virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
177
178private:
179 static TypeHandle _type_handle;
180
181 friend class AsyncFuture;
182 friend class AsyncTaskManager;
183 friend class AsyncTaskChain;
184 friend class AsyncTaskSequence;
185};
186
187INLINE std::ostream &operator << (std::ostream &out, const AsyncTask &task) {
188 task.output(out);
189 return out;
190};
191
192#include "asyncTask.I"
193
194#endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual bool cancel()
Cancels the future.
set_done_event
Sets the event name that will be triggered when the future finishes.
Definition asyncFuture.h:77
The AsyncTaskChain is a subset of the AsyncTaskManager.
A class to manage a loose queue of isolated tasks, which can be performed either synchronously (in th...
This class represents a concrete task performed by an AsyncManager.
Definition asyncTask.h:32
set_done_event
Sets the event name that will be triggered when the task finishes.
Definition asyncTask.h:117
get_state
Returns the current state of the task.
Definition asyncTask.h:103
int get_elapsed_frames() const
Returns the number of frames that have elapsed since the task was started, according to the task mana...
set_task_chain
Specifies the AsyncTaskChain on which this task will be running.
Definition asyncTask.h:114
get_task_chain
Returns the AsyncTaskChain on which this task will be running.
Definition asyncTask.h:114
get_max_dt
Returns the maximum amount of time elapsed during any one of the task's previous run cycles,...
Definition asyncTask.h:120
double get_elapsed_time() const
Returns the amount of time that has elapsed since the task was started, according to the task manager...
get_priority
Returns the task's current priority value.
Definition asyncTask.h:116
get_average_dt
Returns the average amount of time elapsed during each of the task's previous run cycles,...
Definition asyncTask.h:121
bool has_delay() const
Returns true if a delay has been set for this task via set_delay(), or false otherwise.
Definition asyncTask.I:93
get_task_id
Returns a number guaranteed to be unique for each different AsyncTask object in the universe.
Definition asyncTask.h:112
get_sort
Returns the task's current sort value.
Definition asyncTask.h:115
double get_wake_time() const
If this task has been added to an AsyncTaskManager with a delay in effect, this returns the time at w...
get_dt
Returns the amount of time elapsed during the task's previous run cycle, in seconds.
Definition asyncTask.h:119
bool remove()
Removes the task from its active manager, if any, and makes the state S_inactive (or possible S_servi...
Definition asyncTask.cxx:73
is_alive
Returns true if the task is currently active or sleeping on some task chain, meaning that it will be ...
Definition asyncTask.h:104
void clear_delay()
Removes any delay specified for the task.
Definition asyncTask.I:83
double get_start_time() const
Returns the time at which the task was started, according to the task manager's clock.
Definition asyncTask.I:112
void set_delay(double delay)
Specifies the amount of time, in seconds, by which this task will be delayed after it has been added ...
Definition asyncTask.I:72
int get_start_frame() const
Returns the frame number at which the task was started, according to the task manager's clock.
Definition asyncTask.I:124
double get_delay() const
Returns the delay value that has been set via set_delay, if any.
Definition asyncTask.I:101
set_priority
Specifies a priority value for this task.
Definition asyncTask.h:116
std::string get_name_prefix() const
Returns the initial part of the name, up to but not including any trailing digits following a hyphen ...
get_manager
Returns the AsyncTaskManager that this task is active on.
Definition asyncTask.h:105
void recalc_wake_time()
If the task is currently sleeping on a task chain, this resets its wake time to the current time + ge...
set_sort
Specifies a sort value for this task.
Definition asyncTask.h:115
void clear_name()
Resets the Namable's name to empty.
Definition namable.I:35
void output(std::ostream &out) const
Outputs the Namable.
Definition namable.I:61
A lightweight class that represents a single element that may be timed and/or counted via stats.
A thread; that is, a lightweight process.
Definition thread.h:46
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...