Panda3D
 All Classes Functions Variables Enumerations
asyncTaskManager.h
1 // Filename: asyncTaskManager.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 ASYNCTASKMANAGER_H
16 #define ASYNCTASKMANAGER_H
17 
18 #include "pandabase.h"
19 
20 #include "asyncTask.h"
21 #include "asyncTaskCollection.h"
22 #include "asyncTaskChain.h"
23 #include "typedReferenceCount.h"
24 #include "thread.h"
25 #include "pmutex.h"
26 #include "mutexHolder.h"
27 #include "conditionVarFull.h"
28 #include "pvector.h"
29 #include "pdeque.h"
30 #include "pStatCollector.h"
31 #include "clockObject.h"
32 #include "ordered_vector.h"
33 #include "indirectCompareNames.h"
34 
35 ////////////////////////////////////////////////////////////////////
36 // Class : AsyncTaskManager
37 // Description : A class to manage a loose queue of isolated tasks,
38 // which can be performed either synchronously (in the
39 // foreground thread) or asynchronously (by a background
40 // thread).
41 //
42 // The AsyncTaskManager is actually a collection of
43 // AsyncTaskChains, each of which maintains a list of
44 // tasks. Each chain can be either foreground or
45 // background (it may run only in the main thread, or it
46 // may be serviced by one or more background threads).
47 // See AsyncTaskChain for more information.
48 //
49 // If you do not require background processing, it is
50 // perfectly acceptable to create only one
51 // AsyncTaskChain, which runs in the main thread. This
52 // is a common configuration.
53 ////////////////////////////////////////////////////////////////////
54 class EXPCL_PANDA_EVENT AsyncTaskManager : public TypedReferenceCount, public Namable {
55 PUBLISHED:
56  AsyncTaskManager(const string &name);
57  BLOCKING virtual ~AsyncTaskManager();
58 
59  BLOCKING void cleanup();
60 
61  INLINE void set_clock(ClockObject *clock);
62  INLINE ClockObject *get_clock();
63 
64  int get_num_task_chains() const;
65  AsyncTaskChain *get_task_chain(int n) const;
66  MAKE_SEQ(get_task_chains, get_num_task_chains, get_task_chain);
67  AsyncTaskChain *make_task_chain(const string &name);
68  AsyncTaskChain *find_task_chain(const string &name);
69  BLOCKING bool remove_task_chain(const string &name);
70 
71  void add(AsyncTask *task);
72  bool has_task(AsyncTask *task) const;
73 
74  AsyncTask *find_task(const string &name) const;
75  AsyncTaskCollection find_tasks(const string &name) const;
76  AsyncTaskCollection find_tasks_matching(const GlobPattern &pattern) const;
77 
78  bool remove(AsyncTask *task);
79  int remove(const AsyncTaskCollection &tasks);
80 
81  BLOCKING void wait_for_tasks();
82  BLOCKING void stop_threads();
83  void start_threads();
84 
85  INLINE int get_num_tasks() const;
86 
87  AsyncTaskCollection get_tasks() const;
88  AsyncTaskCollection get_active_tasks() const;
89  AsyncTaskCollection get_sleeping_tasks() const;
90 
91  void poll();
92  double get_next_wake_time() const;
93 
94  virtual void output(ostream &out) const;
95  virtual void write(ostream &out, int indent_level = 0) const;
96 
97  INLINE static AsyncTaskManager *get_global_ptr();
98 
99 protected:
100  AsyncTaskChain *do_make_task_chain(const string &name);
101  AsyncTaskChain *do_find_task_chain(const string &name);
102 
103  INLINE void add_task_by_name(AsyncTask *task);
104  void remove_task_by_name(AsyncTask *task);
105 
106  bool do_has_task(AsyncTask *task) const;
107 
108  virtual void do_output(ostream &out) const;
109 
110 private:
111  static void make_global_ptr();
112 
113 protected:
114  class AsyncTaskSortName {
115  public:
116  bool operator () (AsyncTask *a, AsyncTask *b) const {
117  return a->get_name() < b->get_name();
118  }
119  };
120 
122 
123  // Protects all the following members. This same lock is also used
124  // to protect all of our AsyncTaskChain members.
125  Mutex _lock;
126 
128  TaskChains _task_chains;
129 
130  int _num_tasks;
131  TasksByName _tasks_by_name;
132  PT(ClockObject) _clock;
133 
134  ConditionVarFull _frame_cvar; // Signalled when the clock ticks.
135 
136  static AsyncTaskManager* _global_ptr;
137 
138 public:
139  static TypeHandle get_class_type() {
140  return _type_handle;
141  }
142  static void init_type() {
143  TypedReferenceCount::init_type();
144  register_type(_type_handle, "AsyncTaskManager",
145  TypedReferenceCount::get_class_type());
146  }
147  virtual TypeHandle get_type() const {
148  return get_class_type();
149  }
150  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
151 
152 private:
153  static TypeHandle _type_handle;
154 
155  friend class AsyncTaskChain;
156  friend class AsyncTaskChain::AsyncTaskChainThread;
157  friend class AsyncTask;
158  friend class AsyncTaskSequence;
159 };
160 
161 INLINE ostream &operator << (ostream &out, const AsyncTaskManager &manager) {
162  manager.output(out);
163  return out;
164 };
165 
166 #include "asyncTaskManager.I"
167 
168 #endif
A class to manage a loose queue of isolated tasks, which can be performed either synchronously (in th...
A list of tasks, for instance as returned by some of the AsyncTaskManager query functions.
A base class for things which need to inherit from both TypedObject and from ReferenceCount.
A standard mutex, or mutual exclusion lock.
Definition: pmutex.h:44
A specialization of ordered_vector that emulates a standard STL set: one copy of each element is allo...
A base class for all things which can have a name.
Definition: namable.h:29
A ClockObject keeps track of elapsed real time and discrete time.
Definition: clockObject.h:66
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
This class implements a condition variable; see ConditionVar for a brief introduction to this class...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
This class can be used to test for string matches against standard Unix-shell filename globbing conve...
Definition: globPattern.h:37