asyncTaskManager.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #ifndef ASYNCTASKMANAGER_H
  14. #define ASYNCTASKMANAGER_H
  15. #include "pandabase.h"
  16. #include "asyncTask.h"
  17. #include "asyncTaskCollection.h"
  18. #include "asyncTaskChain.h"
  19. #include "typedReferenceCount.h"
  20. #include "thread.h"
  21. #include "pmutex.h"
  22. #include "mutexHolder.h"
  23. #include "conditionVarFull.h"
  24. #include "pvector.h"
  25. #include "pdeque.h"
  26. #include "pStatCollector.h"
  27. #include "clockObject.h"
  28. #include "ordered_vector.h"
  29. #include "indirectCompareNames.h"
  30. /**
  31. * A class to manage a loose queue of isolated tasks, which can be performed
  32. * either synchronously (in the foreground thread) or asynchronously (by a
  33. * background thread).
  34. *
  35. * The AsyncTaskManager is actually a collection of AsyncTaskChains, each of
  36. * which maintains a list of tasks. Each chain can be either foreground or
  37. * background (it may run only in the main thread, or it may be serviced by
  38. * one or more background threads). See AsyncTaskChain for more information.
  39. *
  40. * If you do not require background processing, it is perfectly acceptable to
  41. * create only one AsyncTaskChain, which runs in the main thread. This is a
  42. * common configuration.
  43. */
  44. class EXPCL_PANDA_EVENT AsyncTaskManager : public TypedReferenceCount, public Namable {
  45. PUBLISHED:
  46. explicit AsyncTaskManager(const std::string &name);
  47. BLOCKING virtual ~AsyncTaskManager();
  48. BLOCKING void cleanup();
  49. INLINE void set_clock(ClockObject *clock);
  50. INLINE ClockObject *get_clock();
  51. MAKE_PROPERTY(clock, get_clock, set_clock);
  52. int get_num_task_chains() const;
  53. AsyncTaskChain *get_task_chain(int n) const;
  54. MAKE_SEQ(get_task_chains, get_num_task_chains, get_task_chain);
  55. AsyncTaskChain *make_task_chain(const std::string &name);
  56. AsyncTaskChain *find_task_chain(const std::string &name);
  57. BLOCKING bool remove_task_chain(const std::string &name);
  58. void add(AsyncTask *task);
  59. bool has_task(AsyncTask *task) const;
  60. AsyncTask *find_task(const std::string &name) const;
  61. AsyncTaskCollection find_tasks(const std::string &name) const;
  62. AsyncTaskCollection find_tasks_matching(const GlobPattern &pattern) const;
  63. bool remove(AsyncTask *task);
  64. size_t remove(const AsyncTaskCollection &tasks);
  65. BLOCKING void wait_for_tasks();
  66. BLOCKING void stop_threads();
  67. void start_threads();
  68. INLINE size_t get_num_tasks() const;
  69. AsyncTaskCollection get_tasks() const;
  70. AsyncTaskCollection get_active_tasks() const;
  71. AsyncTaskCollection get_sleeping_tasks() const;
  72. MAKE_PROPERTY(tasks, get_tasks);
  73. MAKE_PROPERTY(active_tasks, get_active_tasks);
  74. MAKE_PROPERTY(sleeping_tasks, get_sleeping_tasks);
  75. void poll();
  76. double get_next_wake_time() const;
  77. MAKE_PROPERTY(next_wake_time, get_next_wake_time);
  78. virtual void output(std::ostream &out) const;
  79. virtual void write(std::ostream &out, int indent_level = 0) const;
  80. INLINE static AsyncTaskManager *get_global_ptr();
  81. protected:
  82. AsyncTaskChain *do_make_task_chain(const std::string &name);
  83. AsyncTaskChain *do_find_task_chain(const std::string &name);
  84. INLINE void add_task_by_name(AsyncTask *task);
  85. void remove_task_by_name(AsyncTask *task);
  86. bool do_has_task(AsyncTask *task) const;
  87. virtual void do_output(std::ostream &out) const;
  88. private:
  89. static void make_global_ptr();
  90. protected:
  91. class AsyncTaskSortName {
  92. public:
  93. bool operator () (AsyncTask *a, AsyncTask *b) const {
  94. return a->get_name() < b->get_name();
  95. }
  96. };
  97. typedef pmultiset<AsyncTask *, AsyncTaskSortName> TasksByName;
  98. // Protects all the following members. This same lock is also used to
  99. // protect all of our AsyncTaskChain members.
  100. Mutex _lock;
  101. typedef ov_set<PT(AsyncTaskChain), IndirectCompareNames<AsyncTaskChain> > TaskChains;
  102. TaskChains _task_chains;
  103. size_t _num_tasks;
  104. TasksByName _tasks_by_name;
  105. PT(ClockObject) _clock;
  106. ConditionVarFull _frame_cvar; // Signalled when the clock ticks.
  107. static AsyncTaskManager* _global_ptr;
  108. public:
  109. static TypeHandle get_class_type() {
  110. return _type_handle;
  111. }
  112. static void init_type() {
  113. TypedReferenceCount::init_type();
  114. register_type(_type_handle, "AsyncTaskManager",
  115. TypedReferenceCount::get_class_type());
  116. }
  117. virtual TypeHandle get_type() const {
  118. return get_class_type();
  119. }
  120. virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
  121. private:
  122. static TypeHandle _type_handle;
  123. friend class AsyncFuture;
  124. friend class AsyncTaskChain;
  125. friend class AsyncTaskChain::AsyncTaskChainThread;
  126. friend class AsyncTask;
  127. friend class AsyncTaskSequence;
  128. friend class PythonTask;
  129. };
  130. INLINE std::ostream &operator << (std::ostream &out, const AsyncTaskManager &manager) {
  131. manager.output(out);
  132. return out;
  133. };
  134. #include "asyncTaskManager.I"
  135. #endif