Panda3D
genericAsyncTask.cxx
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 genericAsyncTask.cxx
10  * @author drose
11  * @date 2008-10-03
12  */
13 
14 #include "genericAsyncTask.h"
15 #include "pnotify.h"
16 
17 TypeHandle GenericAsyncTask::_type_handle;
18 
19 /**
20  *
21  */
22 GenericAsyncTask::
23 GenericAsyncTask(const std::string &name) :
24  AsyncTask(name)
25 {
26  _function = nullptr;
27  _upon_birth = nullptr;
28  _upon_death = nullptr;
29  _user_data = nullptr;
30 }
31 
32 /**
33  *
34  */
35 GenericAsyncTask::
36 GenericAsyncTask(const std::string &name, GenericAsyncTask::TaskFunc *function, void *user_data) :
37  AsyncTask(name),
38  _function(function),
39  _user_data(user_data)
40 {
41  _upon_birth = nullptr;
42  _upon_death = nullptr;
43 }
44 
45 /**
46  * Override this function to return true if the task can be successfully
47  * executed, false if it cannot. Mainly intended as a sanity check when
48  * attempting to add the task to a task manager.
49  *
50  * This function is called with the lock held.
51  */
52 bool GenericAsyncTask::
53 is_runnable() {
54  return _function != nullptr;
55 }
56 
57 /**
58  * Override this function to do something useful for the task.
59  *
60  * This function is called with the lock *not* held.
61  */
62 AsyncTask::DoneStatus GenericAsyncTask::
63 do_task() {
64  nassertr(_function != nullptr, DS_interrupt);
65  return (*_function)(this, _user_data);
66 }
67 
68 /**
69  * Override this function to do something useful when the task has been added
70  * to the active queue.
71  *
72  * This function is called with the lock *not* held.
73  */
74 void GenericAsyncTask::
75 upon_birth(AsyncTaskManager *manager) {
76  AsyncTask::upon_birth(manager);
77 
78  if (_upon_birth != nullptr) {
79  (*_upon_birth)(this, _user_data);
80  }
81 }
82 
83 /**
84  * Override this function to do something useful when the task has been
85  * removed from the active queue. The parameter clean_exit is true if the
86  * task has been removed because it exited normally (returning DS_done), or
87  * false if it was removed for some other reason (e.g.
88  * AsyncTaskManager::remove()). By the time this method is called, _manager
89  * has been cleared, so the parameter manager indicates the original
90  * AsyncTaskManager that owned this task.
91  *
92  * The normal behavior is to throw the done_event only if clean_exit is true.
93  *
94  * This function is called with the lock *not* held.
95  */
96 void GenericAsyncTask::
97 upon_death(AsyncTaskManager *manager, bool clean_exit) {
98  AsyncTask::upon_death(manager, clean_exit);
99 
100  if (_upon_death != nullptr) {
101  (*_upon_death)(this, clean_exit, _user_data);
102  }
103 }
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This class represents a concrete task performed by an AsyncManager.
Definition: asyncTask.h:32
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81