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