Panda3D
genericAsyncTask.cxx
1 // Filename: genericAsyncTask.cxx
2 // Created by: drose (03Oct08)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "genericAsyncTask.h"
16 #include "pnotify.h"
17 
18 TypeHandle GenericAsyncTask::_type_handle;
19 
20 ////////////////////////////////////////////////////////////////////
21 // Function: GenericAsyncTask::Constructor
22 // Access: Published
23 // Description:
24 ////////////////////////////////////////////////////////////////////
25 GenericAsyncTask::
26 GenericAsyncTask(const string &name) :
27  AsyncTask(name)
28 {
29  _function = NULL;
30  _upon_birth = NULL;
31  _upon_death = NULL;
32  _user_data = NULL;
33 }
34 
35 ////////////////////////////////////////////////////////////////////
36 // Function: GenericAsyncTask::Constructor
37 // Access: Published
38 // Description:
39 ////////////////////////////////////////////////////////////////////
40 GenericAsyncTask::
41 GenericAsyncTask(const string &name, GenericAsyncTask::TaskFunc *function, void *user_data) :
42  AsyncTask(name),
43  _function(function),
44  _user_data(user_data)
45 {
46  _upon_birth = NULL;
47  _upon_death = NULL;
48 }
49 
50 ////////////////////////////////////////////////////////////////////
51 // Function: GenericAsyncTask::is_runnable
52 // Access: Protected, Virtual
53 // Description: Override this function to return true if the task can
54 // be successfully executed, false if it cannot. Mainly
55 // intended as a sanity check when attempting to add the
56 // task to a task manager.
57 //
58 // This function is called with the lock held.
59 ////////////////////////////////////////////////////////////////////
60 bool GenericAsyncTask::
61 is_runnable() {
62  return _function != NULL;
63 }
64 
65 ////////////////////////////////////////////////////////////////////
66 // Function: GenericAsyncTask::do_task
67 // Access: Protected, Virtual
68 // Description: Override this function to do something useful for the
69 // task.
70 //
71 // This function is called with the lock *not* held.
72 ////////////////////////////////////////////////////////////////////
73 AsyncTask::DoneStatus GenericAsyncTask::
74 do_task() {
75  nassertr(_function != NULL, DS_interrupt);
76  return (*_function)(this, _user_data);
77 }
78 
79 ////////////////////////////////////////////////////////////////////
80 // Function: GenericAsyncTask::upon_birth
81 // Access: Protected, Virtual
82 // Description: Override this function to do something useful when the
83 // task has been added to the active queue.
84 //
85 // This function is called with the lock *not* held.
86 ////////////////////////////////////////////////////////////////////
87 void GenericAsyncTask::
88 upon_birth(AsyncTaskManager *manager) {
89  AsyncTask::upon_birth(manager);
90 
91  if (_upon_birth != NULL) {
92  (*_upon_birth)(this, _user_data);
93  }
94 }
95 
96 ////////////////////////////////////////////////////////////////////
97 // Function: GenericAsyncTask::upon_death
98 // Access: Protected, Virtual
99 // Description: Override this function to do something useful when the
100 // task has been removed from the active queue. The
101 // parameter clean_exit is true if the task has been
102 // removed because it exited normally (returning
103 // DS_done), or false if it was removed for some other
104 // reason (e.g. AsyncTaskManager::remove()). By the
105 // time this method is called, _manager has been
106 // cleared, so the parameter manager indicates the
107 // original AsyncTaskManager that owned this task.
108 //
109 // The normal behavior is to throw the done_event only
110 // if clean_exit is true.
111 //
112 // This function is called with the lock *not* held.
113 ////////////////////////////////////////////////////////////////////
114 void GenericAsyncTask::
115 upon_death(AsyncTaskManager *manager, bool clean_exit) {
116  AsyncTask::upon_death(manager, clean_exit);
117 
118  if (_upon_death != NULL) {
119  (*_upon_death)(this, clean_exit, _user_data);
120  }
121 }
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:43
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85