23 AtomicAdjust::Integer AsyncTask::_next_task_id;
31 AsyncTask(
const string &name) :
32 _chain_name(
"default"),
39 _servicing_thread(nullptr),
51 AtomicAdjust::Integer current_id = _next_task_id;
53 current_id = _next_task_id;
56 _task_id = current_id;
64 nassertv(_state == S_inactive && _manager ==
nullptr && _chain ==
nullptr);
75 if (manager !=
nullptr) {
76 nassertr(_chain->_manager == manager,
false);
77 if (task_cat.is_debug()) {
79 <<
"Removing " << *
this <<
"\n";
82 if (_chain->do_remove(
this,
true)) {
85 if (task_cat.is_debug()) {
87 <<
" (unable to remove " << *
this <<
")\n";
105 if (_manager !=
nullptr) {
107 if (_state == S_sleeping) {
127 if (_manager !=
nullptr) {
129 if (_state == S_sleeping) {
130 double now = _manager->_clock->get_frame_time();
131 _wake_time = now + _delay;
132 _start_time = _wake_time;
134 make_heap(_chain->_sleeping.begin(), _chain->_sleeping.end(),
135 AsyncTaskChain::AsyncTaskSortWakeTime());
148 nassertr(_state != S_inactive, 0.0);
149 nassertr(_manager !=
nullptr, 0.0);
150 return _manager->_clock->get_frame_time() - _start_time;
161 nassertr(_state != S_inactive, 0);
162 nassertr(_manager !=
nullptr, 0);
163 return _manager->_clock->get_frame_count() - _start_frame;
170 set_name(
const string &name) {
171 if (_manager !=
nullptr) {
173 if (Namable::get_name() != name) {
177 _manager->remove_task_by_name(
this);
178 Namable::set_name(name);
179 _manager->add_task_by_name(
this);
183 Namable::set_name(name);
189 size_t end = name.size();
190 size_t colon = name.find(
':');
191 if (colon != string::npos) {
192 end = std::min(end, colon);
199 size_t trimmed = end;
202 while (p > 0 && isdigit(name[p - 1])) {
205 if (p > 0 && (name[p - 1] ==
'-' || name[p - 1] ==
'_')) {
213 PStatCollector parent(_show_code_pcollector, name.substr(0, trimmed));
216 _task_pcollector = parent;
226 string name = get_name();
227 size_t trimmed = name.size();
230 while (p > 0 && isdigit(name[p - 1])) {
233 if (p > 0 && (name[p - 1] ==
'-' || name[p - 1] ==
'_')) {
242 return name.substr(0, trimmed);
251 if (chain_name != _chain_name) {
252 if (_manager !=
nullptr) {
254 if (_state == S_active) {
260 AsyncTaskChain *chain_a = manager->do_find_task_chain(_chain_name);
261 nassertv(chain_a !=
nullptr);
262 chain_a->do_remove(
this);
263 _chain_name = chain_name;
265 jump_to_task_chain(manager);
270 _chain_name = chain_name;
275 _chain_name = chain_name;
295 if (_manager !=
nullptr) {
297 if (_state == S_active && _sort >= _chain->_current_sort) {
301 AsyncTaskChain *chain = _manager->do_find_task_chain(_chain_name);
302 nassertv(chain !=
nullptr);
303 chain->do_remove(
this);
339 if (priority != _priority) {
340 if (_manager !=
nullptr) {
342 if (_state == S_active && _sort >= _chain->_current_sort) {
346 AsyncTaskChain *chain = _manager->do_find_task_chain(_chain_name);
347 nassertv(chain !=
nullptr);
348 chain->do_remove(
this);
349 _priority = priority;
355 _priority = priority;
360 _priority = priority;
369 output(std::ostream &out)
const {
372 out <<
" " << get_name();
382 AsyncTaskChain *chain_b = manager->do_find_task_chain(_chain_name);
383 if (chain_b ==
nullptr) {
385 <<
"Creating implicit AsyncTaskChain " << _chain_name
386 <<
" for " << manager->get_type() <<
" " 387 << manager->get_name() <<
"\n";
388 chain_b = manager->do_make_task_chain(_chain_name);
390 chain_b->do_add(
this);
397 AsyncTask::DoneStatus AsyncTask::
398 unlock_and_do_task() {
399 nassertr(_manager !=
nullptr, DS_done);
404 nassertr(current_thread->_current_task ==
nullptr, DS_interrupt);
407 __attribute__((unused))
418 nassertr(ptr ==
nullptr, DS_interrupt);
419 nassertr(current_thread->_current_task ==
this, DS_interrupt);
423 _manager->_lock.unlock();
425 double start = clock->get_real_time();
426 _task_pcollector.start();
427 DoneStatus status = do_task();
428 _task_pcollector.stop();
429 double end = clock->get_real_time();
432 _manager->_lock.lock();
435 _max_dt = std::max(_dt, _max_dt);
438 _chain->_time_in_frame += _dt;
441 nassertr(current_thread->_current_task ==
this, status);
452 nassertr(ptr ==
this, DS_interrupt);
453 nassertr(current_thread->_current_task ==
nullptr, DS_interrupt);
465 nassertr(
done(),
false);
512 AsyncTask::DoneStatus AsyncTask::
526 string add_name = manager->get_name() +
"-addTask";
527 PT_Event
event =
new Event(add_name);
548 if (manager !=
nullptr) {
549 string remove_name = manager->get_name() +
"-removeTask";
550 PT_Event
event =
new Event(remove_name);
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
An optional parameter associated with an event.
bool remove()
Removes the task from its active manager, if any, and makes the state S_inactive (or possible S_servi...
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...
A class to manage a loose queue of isolated tasks, which can be performed either synchronously (in th...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
set_sort
Specifies a sort value for this task.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
set_priority
Specifies a priority value for this task.
A base class for things which need to inherit from both TypedObject and from ReferenceCount.
A lightweight C++ object whose constructor calls acquire() and whose destructor calls release() on a ...
int get_elapsed_frames() const
Returns the number of frames that have elapsed since the task was started, according to the task mana...
std::string get_name_prefix() const
Returns the initial part of the name, up to but not including any trailing digits following a hyphen ...
double get_elapsed_time() const
Returns the amount of time that has elapsed since the task was started, according to the task manager...
A lightweight class that represents a single element that may be timed and/or counted via stats.
get_current_thread
Returns a pointer to the currently-executing Thread object.
bool done() const
Returns true if the future is done or has been cancelled.
set_task_chain
Specifies the AsyncTaskChain on which this task will be running.
A ClockObject keeps track of elapsed real time and discrete time.
The AsyncTaskChain is a subset of the AsyncTaskManager.
This class represents a concrete task performed by an AsyncManager.
A thread; that is, a lightweight process.
A named event, possibly with parameters.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool has_name() const
Returns true if the Namable has a nonempty name set, false if the name is empty.
void recalc_wake_time()
If the task is currently sleeping on a task chain, this resets its wake time to the current time + ge...
TypeHandle is the identifier used to differentiate C++ class types.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static Pointer compare_and_exchange_ptr(Pointer &mem, Pointer old_value, Pointer new_value)
Atomic compare and exchange.
static Integer compare_and_exchange(Integer &mem, Integer old_value, Integer new_value)
Atomic compare and exchange.