15 #include "asyncTask.h"
16 #include "asyncTaskManager.h"
17 #include "config_event.h"
19 #include "throw_event.h"
20 #include "eventParameter.h"
22 AtomicAdjust::Integer AsyncTask::_next_task_id;
32 AsyncTask(
const string &name) :
33 _chain_name(
"default"),
40 _servicing_thread(NULL),
51 _python_object = NULL;
57 AtomicAdjust::Integer current_id = _next_task_id;
59 current_id = _next_task_id;
62 _task_id = current_id;
72 nassertv(_state == S_inactive && _manager == NULL && _chain == NULL);
74 set_python_object(NULL);
110 if (_state == S_sleeping) {
138 if (_state == S_sleeping) {
139 double now = _manager->_clock->get_frame_time();
140 _wake_time = now + _delay;
141 _start_time = _wake_time;
143 make_heap(_chain->_sleeping.begin(), _chain->_sleeping.end(),
144 AsyncTaskChain::AsyncTaskSortWakeTime());
161 nassertr(_state != S_inactive, 0.0);
163 return _manager->_clock->get_frame_time() - _start_time;
178 nassertr(_state != S_inactive, 0);
180 return _manager->_clock->get_frame_count() - _start_frame;
189 set_name(
const string &name) {
192 if (Namable::get_name() != name) {
196 _manager->remove_task_by_name(
this);
197 Namable::set_name(name);
198 _manager->add_task_by_name(
this);
203 Namable::set_name(name);
210 size_t end = name.size();
211 size_t colon = name.find(
':');
212 if (colon != string::npos) {
213 end = min(end, colon);
220 size_t trimmed = end;
223 while (p > 0 && isdigit(name[p - 1])) {
226 if (p > 0 && (name[p - 1] ==
'-' || name[p - 1] ==
'_')) {
234 PStatCollector parent(_show_code_pcollector, name.substr(0, trimmed));
237 _task_pcollector = parent;
250 string name = get_name();
251 size_t trimmed = name.size();
254 while (p > 0 && isdigit(name[p - 1])) {
257 if (p > 0 && (name[p - 1] ==
'-' || name[p - 1] ==
'_')) {
266 return name.substr(0, trimmed);
278 if (chain_name != _chain_name) {
281 if (_state == S_active) {
287 AsyncTaskChain *chain_a = manager->do_find_task_chain(_chain_name);
289 chain_a->do_remove(
this);
290 _chain_name = chain_name;
292 jump_to_task_chain(manager);
297 _chain_name = chain_name;
302 _chain_name = chain_name;
328 if (_state == S_active && _sort >= _chain->_current_sort) {
332 AsyncTaskChain *chain = _manager->do_find_task_chain(_chain_name);
334 chain->do_remove(
this);
377 if (priority != _priority) {
380 if (_state == S_active && _sort >= _chain->_current_sort) {
384 AsyncTaskChain *chain = _manager->do_find_task_chain(_chain_name);
386 chain->do_remove(
this);
387 _priority = priority;
393 _priority = priority;
398 _priority = priority;
409 output(ostream &out)
const {
412 out <<
" " << get_name();
424 AsyncTaskChain *chain_b = manager->do_find_task_chain(_chain_name);
427 <<
"Creating implicit AsyncTaskChain " << _chain_name
428 <<
" for " << manager->get_type() <<
" "
429 << manager->get_name() <<
"\n";
430 chain_b = manager->do_make_task_chain(_chain_name);
432 chain_b->do_add(
this);
441 AsyncTask::DoneStatus AsyncTask::
442 unlock_and_do_task() {
447 record_task(current_thread);
451 _manager->_lock.release();
453 double start = clock->get_real_time();
454 _task_pcollector.start();
455 DoneStatus status = do_task();
456 _task_pcollector.stop();
457 double end = clock->get_real_time();
460 _manager->_lock.acquire();
463 _max_dt = max(_dt, _max_dt);
466 _chain->_time_in_frame += _dt;
468 clear_task(current_thread);
527 AsyncTask::DoneStatus AsyncTask::
543 string add_name = manager->get_name() +
"-addTask";
544 PT_Event
event =
new Event(add_name);
569 if (clean_exit && !_done_event.empty()) {
570 PT_Event
event =
new Event(_done_event);
577 string remove_name = manager->get_name() +
"-removeTask";
578 PT_Event
event =
new Event(remove_name);
bool remove(AsyncTask *task)
Removes the indicated task from the active queue.
An optional parameter associated with an event.
A class to manage a loose queue of isolated tasks, which can be performed either synchronously (in th...
double get_elapsed_time() const
Returns the amount of time that has elapsed since the task was started, according to the task manager...
void set_sort(int sort)
Specifies a sort value for this task.
A lightweight C++ object whose constructor calls acquire() and whose destructor calls release() on a ...
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...
bool has_name() const
Returns true if the Namable has a nonempty name set, false if the name is empty.
A lightweight class that represents a single element that may be timed and/or counted via stats...
void remove()
Removes the task from its active manager, if any, and makes the state S_inactive (or possible S_servi...
A ClockObject keeps track of elapsed real time and discrete time.
The AsyncTaskChain is a subset of the AsyncTaskManager.
void set_task_chain(const string &chain_name)
Specifies the AsyncTaskChain on which this task will be running.
string get_name_prefix() const
Returns the initial part of the name, up to but not including any trailing digits following a hyphen ...
int get_elapsed_frames() const
Returns the number of frames that have elapsed since the task was started, according to the task mana...
This class represents a concrete task performed by an AsyncManager.
void set_priority(int priority)
Specifies a priority value for this task.
A thread; that is, a lightweight process.
A named event, possibly with parameters.
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.
static Integer compare_and_exchange(Integer &mem, Integer old_value, Integer new_value)
Atomic compare and exchange.