thread.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Filename: thread.h
  2. // Created by: cary (16Sep98)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://etc.cmu.edu/panda3d/docs/license/ .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #ifndef THREAD_H
  19. #define THREAD_H
  20. #include "pandabase.h"
  21. #include "typedReferenceCount.h"
  22. #include "pointerTo.h"
  23. #include "threadPriority.h"
  24. #include "threadImpl.h"
  25. #include "pnotify.h"
  26. #include "config_pipeline.h"
  27. class Mutex;
  28. class ReMutex;
  29. class MutexDebug;
  30. ////////////////////////////////////////////////////////////////////
  31. // Class : Thread
  32. // Description : A thread; that is, a lightweight process. This is an
  33. // abstract base class; to use it, you must subclass
  34. // from it and redefine thread_main().
  35. //
  36. // The thread itself will keep a reference count on the
  37. // Thread object while it is running; when the thread
  38. // returns from its root function, the Thread object
  39. // will automatically be destructed if no other pointers
  40. // are referencing it.
  41. ////////////////////////////////////////////////////////////////////
  42. class EXPCL_PANDA Thread : public TypedReferenceCount {
  43. protected:
  44. INLINE Thread(const string &name, const string &sync_name);
  45. PUBLISHED:
  46. virtual ~Thread();
  47. private:
  48. INLINE Thread(const Thread &copy);
  49. INLINE void operator = (const Thread &copy);
  50. protected:
  51. virtual void thread_main()=0;
  52. PUBLISHED:
  53. static PT(Thread) bind_thread(const string &name, const string &sync_name);
  54. INLINE const string &get_name() const;
  55. INLINE const string &get_sync_name() const;
  56. INLINE int get_pstats_index() const;
  57. INLINE void set_pstats_index(int pstats_index);
  58. INLINE int get_pipeline_stage() const;
  59. void set_pipeline_stage(int pipeline_stage);
  60. INLINE void set_min_pipeline_stage(int min_pipeline_stage);
  61. INLINE static Thread *get_main_thread();
  62. INLINE static Thread *get_external_thread();
  63. INLINE static Thread *get_current_thread();
  64. INLINE static int get_current_pipeline_stage();
  65. INLINE static bool is_threading_supported();
  66. BLOCKING INLINE static void sleep(double seconds);
  67. virtual void output(ostream &out) const;
  68. INLINE bool is_started() const;
  69. bool start(ThreadPriority priority, bool joinable);
  70. INLINE void interrupt();
  71. BLOCKING INLINE void join();
  72. public:
  73. INLINE static void prepare_for_exit();
  74. private:
  75. static void init_main_thread();
  76. static void init_external_thread();
  77. protected:
  78. bool _started;
  79. private:
  80. string _name;
  81. string _sync_name;
  82. ThreadImpl _impl;
  83. int _pstats_index;
  84. int _pipeline_stage;
  85. #ifdef DEBUG_THREADS
  86. MutexDebug *_blocked_on_mutex;
  87. #endif
  88. private:
  89. static Thread *_main_thread;
  90. static Thread *_external_thread;
  91. public:
  92. static TypeHandle get_class_type() {
  93. return _type_handle;
  94. }
  95. static void init_type() {
  96. TypedReferenceCount::init_type();
  97. register_type(_type_handle, "Thread",
  98. TypedReferenceCount::get_class_type());
  99. }
  100. virtual TypeHandle get_type() const {
  101. return get_class_type();
  102. }
  103. virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
  104. private:
  105. static TypeHandle _type_handle;
  106. friend class MutexDebug;
  107. friend class ThreadDummyImpl;
  108. friend class ThreadWin32Impl;
  109. friend class ThreadPosixImpl;
  110. };
  111. INLINE ostream &operator << (ostream &out, const Thread &thread);
  112. #include "thread.I"
  113. #endif