Panda3D
 All Classes Functions Variables Enumerations
asyncTask.h
1 // Filename: asyncTask.h
2 // Created by: drose (23Aug06)
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 #ifndef ASYNCTASK_H
16 #define ASYNCTASK_H
17 
18 #include "pandabase.h"
19 
20 #include "asyncTaskBase.h"
21 #include "pmutex.h"
22 #include "conditionVar.h"
23 #include "pStatCollector.h"
24 
25 #ifdef HAVE_PYTHON
26 
27 #undef HAVE_LONG_LONG // NSPR and Python both define this.
28 #undef _POSIX_C_SOURCE
29 #include <Python.h>
30 
31 #endif // HAVE_PYTHON
32 
33 class AsyncTaskManager;
34 class AsyncTaskChain;
35 
36 ////////////////////////////////////////////////////////////////////
37 // Class : AsyncTask
38 // Description : This class represents a concrete task performed by an
39 // AsyncManager. Normally, you would subclass from this
40 // class, and override do_task(), to define the
41 // functionality you wish to have the task perform.
42 ////////////////////////////////////////////////////////////////////
43 class EXPCL_PANDA_EVENT AsyncTask : public AsyncTaskBase {
44 public:
45  AsyncTask(const string &name = string());
46  ALLOC_DELETED_CHAIN(AsyncTask);
47 
48 PUBLISHED:
49  virtual ~AsyncTask();
50 
51  enum DoneStatus {
52  DS_done, // normal task completion
53  DS_cont, // run task again next epoch
54  DS_again, // start the task over from the beginning
55  DS_pickup, // run task again this frame, if frame budget allows
56  DS_exit, // stop the enclosing sequence
57  DS_pause, // pause, then exit (useful within a sequence)
58  DS_interrupt, // interrupt the task manager, but run task again
59  };
60 
61  enum State {
62  S_inactive,
63  S_active,
64  S_servicing,
65  S_servicing_removed, // Still servicing, but wants removal from manager.
66  S_sleeping,
67  S_active_nested, // active within a sequence.
68  };
69 
70  INLINE State get_state() const;
71  INLINE bool is_alive() const;
72  INLINE AsyncTaskManager *get_manager() const;
73 
74  void remove();
75 
76  INLINE void set_delay(double delay);
77  INLINE void clear_delay();
78  INLINE bool has_delay() const;
79  INLINE double get_delay() const;
80  double get_wake_time() const;
81  void recalc_wake_time();
82 
83  INLINE double get_start_time() const;
84  double get_elapsed_time() const;
85  INLINE int get_start_frame() const;
86  int get_elapsed_frames() const;
87 
88  void set_name(const string &name);
89  INLINE void clear_name();
90  string get_name_prefix() const;
91 
92  INLINE AtomicAdjust::Integer get_task_id() const;
93 
94  void set_task_chain(const string &chain_name);
95  INLINE const string &get_task_chain() const;
96 
97  void set_sort(int sort);
98  INLINE int get_sort() const;
99 
100  void set_priority(int priority);
101  INLINE int get_priority() const;
102 
103  INLINE void set_done_event(const string &done_event);
104  INLINE const string &get_done_event() const;
105 
106 #ifdef HAVE_PYTHON
107  INLINE void set_python_object(PyObject *python_object);
108  INLINE PyObject *get_python_object() const;
109 #endif // HAVE_PYTHON
110 
111  INLINE double get_dt() const;
112  INLINE double get_max_dt() const;
113  INLINE double get_average_dt() const;
114 
115  virtual void output(ostream &out) const;
116 
117 protected:
118  void jump_to_task_chain(AsyncTaskManager *manager);
119  DoneStatus unlock_and_do_task();
120 
121  virtual bool is_runnable();
122  virtual DoneStatus do_task();
123  virtual void upon_birth(AsyncTaskManager *manager);
124  virtual void upon_death(AsyncTaskManager *manager, bool clean_exit);
125 
126 protected:
127  AtomicAdjust::Integer _task_id;
128  string _chain_name;
129  double _delay;
130  bool _has_delay;
131  double _wake_time;
132  int _sort;
133  int _priority;
134  string _done_event;
135 
136  State _state;
137  Thread *_servicing_thread;
138  AsyncTaskManager *_manager;
139  AsyncTaskChain *_chain;
140 
141  double _start_time;
142  int _start_frame;
143 
144  double _dt;
145  double _max_dt;
146  double _total_dt;
147  int _num_frames;
148 
149  static AtomicAdjust::Integer _next_task_id;
150 
151  static PStatCollector _show_code_pcollector;
152  PStatCollector _task_pcollector;
153 
154 private:
155 #ifdef HAVE_PYTHON
156  PyObject *_python_object;
157 #endif // HAVE_PYTHON
158 
159 public:
160  static TypeHandle get_class_type() {
161  return _type_handle;
162  }
163  static void init_type() {
164  AsyncTaskBase::init_type();
165  register_type(_type_handle, "AsyncTask",
166  AsyncTaskBase::get_class_type());
167  }
168  virtual TypeHandle get_type() const {
169  return get_class_type();
170  }
171  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
172 
173 private:
174  static TypeHandle _type_handle;
175 
176  friend class AsyncTaskManager;
177  friend class AsyncTaskChain;
178  friend class AsyncTaskSequence;
179 };
180 
181 INLINE ostream &operator << (ostream &out, const AsyncTask &task) {
182  task.output(out);
183  return out;
184 };
185 
186 #include "asyncTask.I"
187 
188 #endif
The abstract base class for AsyncTask.
Definition: asyncTaskBase.h:31
A class to manage a loose queue of isolated tasks, which can be performed either synchronously (in th...
void clear_name()
Resets the Namable&#39;s name to empty.
Definition: namable.I:64
A lightweight class that represents a single element that may be timed and/or counted via stats...
A special kind of task that serves as a list of tasks internally.
The AsyncTaskChain is a subset of the AsyncTaskManager.
This class represents a concrete task performed by an AsyncManager.
Definition: asyncTask.h:43
void output(ostream &out) const
Outputs the Namable.
Definition: namable.I:97
A thread; that is, a lightweight process.
Definition: thread.h:51
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85