forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasyncTask.h
More file actions
188 lines (149 loc) · 4.83 KB
/
asyncTask.h
File metadata and controls
188 lines (149 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Filename: asyncTask.h
// Created by: drose (23Aug06)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) Carnegie Mellon University. All rights reserved.
//
// All use of this software is subject to the terms of the revised BSD
// license. You should have received a copy of this license along
// with this source code in a file named "LICENSE."
//
////////////////////////////////////////////////////////////////////
#ifndef ASYNCTASK_H
#define ASYNCTASK_H
#include "pandabase.h"
#include "asyncTaskBase.h"
#include "pmutex.h"
#include "conditionVar.h"
#include "pStatCollector.h"
#ifdef HAVE_PYTHON
#undef HAVE_LONG_LONG // NSPR and Python both define this.
#undef _POSIX_C_SOURCE
#include <Python.h>
#endif // HAVE_PYTHON
class AsyncTaskManager;
class AsyncTaskChain;
////////////////////////////////////////////////////////////////////
// Class : AsyncTask
// Description : This class represents a concrete task performed by an
// AsyncManager. Normally, you would subclass from this
// class, and override do_task(), to define the
// functionality you wish to have the task perform.
////////////////////////////////////////////////////////////////////
class EXPCL_PANDA_EVENT AsyncTask : public AsyncTaskBase {
public:
AsyncTask(const string &name = string());
ALLOC_DELETED_CHAIN(AsyncTask);
PUBLISHED:
virtual ~AsyncTask();
enum DoneStatus {
DS_done, // normal task completion
DS_cont, // run task again next epoch
DS_again, // start the task over from the beginning
DS_pickup, // run task again this frame, if frame budget allows
DS_exit, // stop the enclosing sequence
DS_pause, // pause, then exit (useful within a sequence)
DS_interrupt, // interrupt the task manager, but run task again
};
enum State {
S_inactive,
S_active,
S_servicing,
S_servicing_removed, // Still servicing, but wants removal from manager.
S_sleeping,
S_active_nested, // active within a sequence.
};
INLINE State get_state() const;
INLINE bool is_alive() const;
INLINE AsyncTaskManager *get_manager() const;
void remove();
INLINE void set_delay(double delay);
INLINE void clear_delay();
INLINE bool has_delay() const;
INLINE double get_delay() const;
double get_wake_time() const;
void recalc_wake_time();
INLINE double get_start_time() const;
double get_elapsed_time() const;
INLINE int get_start_frame() const;
int get_elapsed_frames() const;
void set_name(const string &name);
INLINE void clear_name();
string get_name_prefix() const;
INLINE AtomicAdjust::Integer get_task_id() const;
void set_task_chain(const string &chain_name);
INLINE const string &get_task_chain() const;
void set_sort(int sort);
INLINE int get_sort() const;
void set_priority(int priority);
INLINE int get_priority() const;
INLINE void set_done_event(const string &done_event);
INLINE const string &get_done_event() const;
#ifdef HAVE_PYTHON
INLINE void set_python_object(PyObject *python_object);
INLINE PyObject *get_python_object() const;
#endif // HAVE_PYTHON
INLINE double get_dt() const;
INLINE double get_max_dt() const;
INLINE double get_average_dt() const;
virtual void output(ostream &out) const;
protected:
void jump_to_task_chain(AsyncTaskManager *manager);
DoneStatus unlock_and_do_task();
virtual bool is_runnable();
virtual DoneStatus do_task();
virtual void upon_birth(AsyncTaskManager *manager);
virtual void upon_death(AsyncTaskManager *manager, bool clean_exit);
protected:
AtomicAdjust::Integer _task_id;
string _chain_name;
double _delay;
bool _has_delay;
double _wake_time;
int _sort;
int _priority;
string _done_event;
State _state;
Thread *_servicing_thread;
AsyncTaskManager *_manager;
AsyncTaskChain *_chain;
double _start_time;
int _start_frame;
double _dt;
double _max_dt;
double _total_dt;
int _num_frames;
static AtomicAdjust::Integer _next_task_id;
static PStatCollector _show_code_pcollector;
PStatCollector _task_pcollector;
private:
#ifdef HAVE_PYTHON
PyObject *_python_object;
#endif // HAVE_PYTHON
public:
static TypeHandle get_class_type() {
return _type_handle;
}
static void init_type() {
AsyncTaskBase::init_type();
register_type(_type_handle, "AsyncTask",
AsyncTaskBase::get_class_type());
}
virtual TypeHandle get_type() const {
return get_class_type();
}
virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
private:
static TypeHandle _type_handle;
friend class AsyncTaskManager;
friend class AsyncTaskChain;
friend class AsyncTaskSequence;
};
INLINE ostream &operator << (ostream &out, const AsyncTask &task) {
task.output(out);
return out;
};
#include "asyncTask.I"
#endif