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;
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
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
120 typedef pmultiset<AsyncTask *, AsyncTaskSortName> TasksByName;
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
126 typedef ov_set<PT(AsyncTaskChain), IndirectCompareNames<AsyncTaskChain> > TaskChains;
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.
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...
get_num_task_chains
Returns the number of different task chains.
size_t get_num_tasks() const
Returns the number of tasks that are currently active or sleeping within the task manager.
void wait_for_tasks()
Blocks until the task list is empty.
bool remove(AsyncTask *task)
Removes the indicated task from the active queue.
void add(AsyncTask *task)
Adds the indicated task to the active queue.
AsyncTaskCollection find_tasks_matching(const GlobPattern &pattern) const
Returns the list of tasks found whose name matches the indicated glob pattern, e.g.
get_clock
Returns the clock pointer used within the AsyncTaskManager.
void stop_threads()
Stops any threads that are currently running.
void poll()
Runs through all the tasks in the task list, once, if the task manager is running in single-threaded ...
AsyncTaskChain * find_task_chain(const std::string &name)
Searches a new AsyncTaskChain of the indicated name and returns it if it exists, or NULL otherwise.
void cleanup()
Stops all threads and messily empties the task list.
get_sleeping_tasks
Returns the set of tasks that are sleeping (and not active) on the task manager, at the time of the c...
get_next_wake_time
Returns the scheduled time (on the manager's clock) of the next sleeping task, on any task chain,...
AsyncTaskChain * make_task_chain(const std::string &name)
Creates a new AsyncTaskChain of the indicated name and stores it within the AsyncTaskManager.
set_clock
Replaces the clock pointer used within the AsyncTaskManager.
get_tasks
Returns the set of tasks that are active or sleeping on the task manager, at the time of the call.
AsyncTask * find_task(const std::string &name) const
Returns the first task found with the indicated name, or NULL if there is no task with the indicated ...
get_active_tasks
Returns the set of tasks that are active (and not sleeping) on the task manager, at the time of the c...
get_task_chain
Returns the nth task chain.
bool has_task(AsyncTask *task) const
Returns true if the indicated task has been added to this AsyncTaskManager, false otherwise.
bool remove_task_chain(const std::string &name)
Removes the AsyncTaskChain of the indicated name.
void start_threads()
Starts any requested threads to service the tasks on the queue.
AsyncTaskCollection find_tasks(const std::string &name) const
Returns the list of tasks found with the indicated name.
static AsyncTaskManager * get_global_ptr()
Returns a pointer to the global AsyncTaskManager.
A ClockObject keeps track of elapsed real time and discrete time.
Definition clockObject.h:58
This class can be used to test for string matches against standard Unix- shell filename globbing conv...
Definition globPattern.h:32
void output(std::ostream &out) const
Outputs the Namable.
Definition namable.I:61
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.