Panda3D

asyncTask.I
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.I
10 * @author drose
11 * @date 2006-08-23
12 */
13
14 /**
15 * Returns the current state of the task.
16 */
17 INLINE AsyncTask::State AsyncTask::
18 get_state() const {
19 return _state;
20 }
21
22 /**
23 * Returns true if the task is currently active or sleeping on some task
24 * chain, meaning that it will be executed in its turn, or false if it is not
25 * active. If the task has recently been removed while it is in the middle of
26 * execution, this will return false, because the task will not run again once
27 * it finishes.
28 */
29 INLINE bool AsyncTask::
30 is_alive() const {
31 switch (_state) {
32 case S_active:
33 case S_servicing:
34 case S_sleeping:
35 case S_active_nested:
36 case S_awaiting:
37 return true;
38
39 case S_inactive:
40 case S_servicing_removed:
41 return false;
42 }
43
44 // Shouldn't get here.
45 return false;
46 }
47
48 /**
49 * Returns the AsyncTaskManager that this task is active on. This will be
50 * NULL if the state is S_inactive.
51 */
52 INLINE AsyncTaskManager *AsyncTask::
53 get_manager() const {
54 return _manager;
55 }
56
57 /**
58 * Specifies the amount of time, in seconds, by which this task will be
59 * delayed after it has been added to the AsyncTaskManager. At least the
60 * specified amount of time (and possibly more) will elapse before the task
61 * begins.
62 *
63 * You may specify a delay of 0.0 to guarantee that the task will run in the
64 * next epoch following the one in which it is added.
65 *
66 * Setting this value after the task has already been added will not affect
67 * the task's wake time; it will only affect the task if it is re-added to the
68 * queue in the future, for instance if the task returns DS_again. However,
69 * see recalc_wake_time() if you wish to apply the delay effect immediately.
70 */
71 INLINE void AsyncTask::
72 set_delay(double delay) {
73 _delay = delay;
74 _has_delay = true;
75 }
76
77 /**
78 * Removes any delay specified for the task. The next time the task is added
79 * to the queue, it will run immediately. This does not affect the task's
80 * wake time if it has already been added to the queue.
81 */
82 INLINE void AsyncTask::
83 clear_delay() {
84 _delay = 0.0;
85 _has_delay = false;
86 }
87
88 /**
89 * Returns true if a delay has been set for this task via set_delay(), or
90 * false otherwise.
91 */
92 INLINE bool AsyncTask::
93 has_delay() const {
94 return _has_delay;
95 }
96
97 /**
98 * Returns the delay value that has been set via set_delay, if any.
99 */
100 INLINE double AsyncTask::
101 get_delay() const {
102 return _delay;
103 }
104
105 /**
106 * Returns the time at which the task was started, according to the task
107 * manager's clock.
108 *
109 * It is only valid to call this if the task's status is not S_inactive.
110 */
111 INLINE double AsyncTask::
112 get_start_time() const {
113 nassertr(_state != S_inactive, 0.0);
114 return _start_time;
115 }
116
117 /**
118 * Returns the frame number at which the task was started, according to the
119 * task manager's clock.
120 *
121 * It is only valid to call this if the task's status is not S_inactive.
122 */
123 INLINE int AsyncTask::
124 get_start_frame() const {
125 nassertr(_state != S_inactive, 0);
126 return _start_frame;
127 }
128
129 /**
130 * Resets the task's name to empty.
131 */
132 INLINE void AsyncTask::
133 clear_name() {
134 set_name(std::string());
135 }
136
137 /**
138 * Returns a number guaranteed to be unique for each different AsyncTask
139 * object in the universe.
140 */
141 INLINE AtomicAdjust::Integer AsyncTask::
142 get_task_id() const {
143 return _task_id;
144 }
145
146 /**
147 * Returns the AsyncTaskChain on which this task will be running. Each task
148 * chain runs tasks independently of the others.
149 */
150 INLINE const std::string &AsyncTask::
151 get_task_chain() const {
152 return _chain_name;
153 }
154
155
156 /**
157 * Returns the task's current sort value. See set_sort().
158 */
159 INLINE int AsyncTask::
160 get_sort() const {
161 return _sort;
162 }
163
164 /**
165 * Returns the task's current priority value. See set_priority().
166 */
167 INLINE int AsyncTask::
168 get_priority() const {
169 return _priority;
170 }
171
172 /**
173 * Sets the event name that will be triggered when the task finishes. This
174 * should only be called before the task has been started, or after it has
175 * finished and before it is about to be restarted (i.e. when get_state()
176 * returns S_inactive).
177 */
178 INLINE void AsyncTask::
179 set_done_event(const std::string &done_event) {
180 nassertv(_state == S_inactive);
181 _done_event = done_event;
182 }
183
184 /**
185 * Returns the amount of time elapsed during the task's previous run cycle, in
186 * seconds.
187 */
188 INLINE double AsyncTask::
189 get_dt() const {
190 return _dt;
191 }
192
193 /**
194 * Returns the maximum amount of time elapsed during any one of the task's
195 * previous run cycles, in seconds.
196 */
197 INLINE double AsyncTask::
198 get_max_dt() const {
199 return _max_dt;
200 }
201
202 /**
203 * Returns the average amount of time elapsed during each of the task's
204 * previous run cycles, in seconds.
205 */
206 INLINE double AsyncTask::
207 get_average_dt() const {
208 if (_num_frames == 0) {
209 return 0.0;
210 } else {
211 return _total_dt / _num_frames;
212 }
213 }
AsyncTaskManager
A class to manage a loose queue of isolated tasks, which can be performed either synchronously (in th...
Definition: asyncTaskManager.h:48
AsyncTask::set_done_event
set_done_event
Sets the event name that will be triggered when the task finishes.
Definition: asyncTask.h:117
AsyncTask::get_state
get_state
Returns the current state of the task.
Definition: asyncTask.h:103
AsyncTask::clear_name
void clear_name()
Resets the task's name to empty.
Definition: asyncTask.I:133
AsyncTask::get_task_chain
get_task_chain
Returns the AsyncTaskChain on which this task will be running.
Definition: asyncTask.h:114
AsyncTask::get_max_dt
get_max_dt
Returns the maximum amount of time elapsed during any one of the task's previous run cycles,...
Definition: asyncTask.h:120
AsyncTask::get_priority
get_priority
Returns the task's current priority value.
Definition: asyncTask.h:116
AsyncTask::get_average_dt
get_average_dt
Returns the average amount of time elapsed during each of the task's previous run cycles,...
Definition: asyncTask.h:121
AsyncTask::has_delay
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
AsyncTask::get_task_id
get_task_id
Returns a number guaranteed to be unique for each different AsyncTask object in the universe.
Definition: asyncTask.h:112
AsyncTask::get_sort
get_sort
Returns the task's current sort value.
Definition: asyncTask.h:115
AsyncTask::get_dt
get_dt
Returns the amount of time elapsed during the task's previous run cycle, in seconds.
Definition: asyncTask.h:119
AsyncTask::is_alive
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
AsyncTask::clear_delay
void clear_delay()
Removes any delay specified for the task.
Definition: asyncTask.I:83
AsyncTask::get_start_time
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
AsyncTask::set_delay
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
AsyncTask::get_start_frame
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
AsyncTask::get_delay
double get_delay() const
Returns the delay value that has been set via set_delay, if any.
Definition: asyncTask.I:101
AsyncTask::get_manager
get_manager
Returns the AsyncTaskManager that this task is active on.
Definition: asyncTask.h:105