Panda3D
asyncTask.h
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 asyncTask.h
10  * @author drose
11  * @date 2006-08-23
12  */
13 
14 #ifndef ASYNCTASK_H
15 #define ASYNCTASK_H
16 
17 #include "pandabase.h"
18 #include "asyncFuture.h"
19 #include "namable.h"
20 #include "pmutex.h"
21 #include "conditionVar.h"
22 #include "pStatCollector.h"
23 
24 class AsyncTaskManager;
25 class AsyncTaskChain;
26 
27 /**
28  * This class represents a concrete task performed by an AsyncManager.
29  * Normally, you would subclass from this class, and override do_task(), to
30  * define the functionality you wish to have the task perform.
31  */
32 class EXPCL_PANDA_EVENT AsyncTask : public AsyncFuture, public Namable {
33 public:
34  AsyncTask(const std::string &name = std::string());
35  ALLOC_DELETED_CHAIN(AsyncTask);
36 
37 PUBLISHED:
38  virtual ~AsyncTask();
39 
40  enum DoneStatus {
41  DS_done, // normal task completion
42  DS_cont, // run task again next epoch
43  DS_again, // start the task over from the beginning
44  DS_pickup, // run task again this frame, if frame budget allows
45  DS_exit, // stop the enclosing sequence
46  DS_pause, // pause, then exit (useful within a sequence)
47  DS_interrupt, // interrupt the task manager, but run task again
48  DS_await, // await a different task's completion
49  };
50 
51  enum State {
52  S_inactive,
53  S_active,
54  S_servicing,
55  S_servicing_removed, // Still servicing, but wants removal from manager.
56  S_sleeping,
57  S_active_nested, // active within a sequence.
58  S_awaiting, // Waiting for a dependent task to complete
59  };
60 
61  INLINE State get_state() const;
62  INLINE bool is_alive() const;
63  INLINE AsyncTaskManager *get_manager() const;
64 
65  bool remove();
66 
67  INLINE void set_delay(double delay);
68  INLINE void clear_delay();
69  INLINE bool has_delay() const;
70  INLINE double get_delay() const;
71  double get_wake_time() const;
72  void recalc_wake_time();
73 
74  INLINE double get_start_time() const;
75  double get_elapsed_time() const;
76  INLINE int get_start_frame() const;
77  int get_elapsed_frames() const;
78 
79  void set_name(const std::string &name);
80  INLINE void clear_name();
81  std::string get_name_prefix() const;
82 
83  INLINE AtomicAdjust::Integer get_task_id() const;
84 
85  void set_task_chain(const std::string &chain_name);
86  INLINE const std::string &get_task_chain() const;
87 
88  void set_sort(int sort);
89  INLINE int get_sort() const;
90 
91  void set_priority(int priority);
92  INLINE int get_priority() const;
93 
94  INLINE void set_done_event(const std::string &done_event);
95 
96  INLINE double get_dt() const;
97  INLINE double get_max_dt() const;
98  INLINE double get_average_dt() const;
99 
100  virtual void output(std::ostream &out) const;
101 
102 PUBLISHED:
103  MAKE_PROPERTY(state, get_state);
104  MAKE_PROPERTY(alive, is_alive);
105  MAKE_PROPERTY(manager, get_manager);
106 
107  // The name of this task.
108  MAKE_PROPERTY(name, get_name, set_name);
109 
110  // This is a number guaranteed to be unique for each different AsyncTask
111  // object in the universe.
112  MAKE_PROPERTY(id, get_task_id);
113 
114  MAKE_PROPERTY(task_chain, get_task_chain, set_task_chain);
115  MAKE_PROPERTY(sort, get_sort, set_sort);
116  MAKE_PROPERTY(priority, get_priority, set_priority);
117  MAKE_PROPERTY(done_event, get_done_event, set_done_event);
118 
119  MAKE_PROPERTY(dt, get_dt);
120  MAKE_PROPERTY(max_dt, get_max_dt);
121  MAKE_PROPERTY(average_dt, get_average_dt);
122 
123 protected:
124  void jump_to_task_chain(AsyncTaskManager *manager);
125  DoneStatus unlock_and_do_task();
126 
127  virtual bool cancel() final;
128  virtual bool is_task() const final {return true;}
129 
130  virtual bool is_runnable();
131  virtual DoneStatus do_task();
132  virtual void upon_birth(AsyncTaskManager *manager);
133  virtual void upon_death(AsyncTaskManager *manager, bool clean_exit);
134 
135 protected:
136  AtomicAdjust::Integer _task_id;
137  std::string _chain_name;
138  double _delay;
139  bool _has_delay;
140  double _wake_time;
141  int _sort;
142  int _priority;
143  unsigned int _implicit_sort;
144 
145  State _state;
146  Thread *_servicing_thread;
147  AsyncTaskChain *_chain;
148 
149  double _start_time;
150  int _start_frame;
151 
152  double _dt;
153  double _max_dt;
154  double _total_dt;
155  int _num_frames;
156 
157  static AtomicAdjust::Integer _next_task_id;
158 
159  static PStatCollector _show_code_pcollector;
160  PStatCollector _task_pcollector;
161 
162  friend class PythonTask;
163 
164 public:
165  static TypeHandle get_class_type() {
166  return _type_handle;
167  }
168  static void init_type() {
169  AsyncFuture::init_type();
170  register_type(_type_handle, "AsyncTask",
171  AsyncFuture::get_class_type());
172  }
173  virtual TypeHandle get_type() const {
174  return get_class_type();
175  }
176  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
177 
178 private:
179  static TypeHandle _type_handle;
180 
181  friend class AsyncFuture;
182  friend class AsyncTaskManager;
183  friend class AsyncTaskChain;
184  friend class AsyncTaskSequence;
185 };
186 
187 INLINE std::ostream &operator << (std::ostream &out, const AsyncTask &task) {
188  task.output(out);
189  return out;
190 };
191 
192 #include "asyncTask.I"
193 
194 #endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This class represents a thread-safe handle to a promised future result of an asynchronous operation,...
Definition: asyncFuture.h:61
virtual bool cancel()
Cancels the future.
Definition: asyncFuture.cxx:57
set_done_event
Sets the event name that will be triggered when the future finishes.
Definition: asyncFuture.h:77
The AsyncTaskChain is a subset of the AsyncTaskManager.
A class to manage a loose queue of isolated tasks, which can be performed either synchronously (in th...
A special kind of task that serves as a list of tasks internally.
This class represents a concrete task performed by an AsyncManager.
Definition: asyncTask.h:32
A base class for all things which can have a name.
Definition: namable.h:26
void clear_name()
Resets the Namable's name to empty.
Definition: namable.I:35
void output(std::ostream &out) const
Outputs the Namable.
Definition: namable.I:61
A lightweight class that represents a single element that may be timed and/or counted via stats.
A thread; that is, a lightweight process.
Definition: thread.h:46
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22