Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
5.5 KiB

  1. #ifndef _THREAD_HPP_
  2. #define _THREAD_HPP_
  3. // Ruler
  4. // 1 2 3 4 5 6 7 8
  5. //345678901234567890123456789012345678901234567890123456789012345678901234567890
  6. /********************************************************************/
  7. /* */
  8. /* The standard layout. */
  9. /* */
  10. /* The standard layout for 'hpp' files for this code is as */
  11. /* follows: */
  12. /* */
  13. /* 1. Include files. */
  14. /* 2. Constants exported from the class. */
  15. /* 3. Data structures exported from the class. */
  16. /* 4. Forward references to other data structures. */
  17. /* 5. Class specifications (including inline functions). */
  18. /* 6. Additional large inline functions. */
  19. /* */
  20. /* Any portion that is not required is simply omitted. */
  21. /* */
  22. /********************************************************************/
  23. #include "Global.hpp"
  24. #include "Environment.hpp"
  25. #include "Spinlock.hpp"
  26. #include "Vector.hpp"
  27. /********************************************************************/
  28. /* */
  29. /* Constants exported from the class. */
  30. /* */
  31. /* The thread constants indicate various thread status */
  32. /* information. */
  33. /* */
  34. /********************************************************************/
  35. CONST SBIT32 NoThread = -1;
  36. /********************************************************************/
  37. /* */
  38. /* Data structures exported from the class. */
  39. /* */
  40. /* A thread started by this class should conform to the type */
  41. /* specification given here. */
  42. /* */
  43. /********************************************************************/
  44. typedef VOID (*NEW_THREAD)( VOID *Parameter );
  45. /********************************************************************/
  46. /* */
  47. /* Thread synchronization. */
  48. /* */
  49. /* This class privides a method for synchronizing a number */
  50. /* of worker threads with a master thread. Each time the */
  51. /* master thread calls 'StartThread()' a new thread is created. */
  52. /* When a thread calls 'EndThread()' the thread is terminated. */
  53. /* At any point the master thread can enquire about the number */
  54. /* of active threads or wait for them to complete. */
  55. /* */
  56. /********************************************************************/
  57. class THREAD : private ENVIRONMENT
  58. {
  59. //
  60. // Private type definitions.
  61. //
  62. typedef struct
  63. {
  64. HANDLE Handle;
  65. SBIT32 ThreadId;
  66. }
  67. THREAD_INFO;
  68. //
  69. // Private data.
  70. //
  71. BOOLEAN Affinity;
  72. VOLATILE SBIT16 Cpu;
  73. BOOLEAN Priority;
  74. HANDLE Running;
  75. LONG Stack;
  76. HANDLE Started;
  77. SBIT32 MaxThreads;
  78. SBIT32 ThreadsUsed;
  79. VECTOR<THREAD_INFO> Threads;
  80. SPINLOCK Spinlock;
  81. public:
  82. //
  83. // Public functions.
  84. //
  85. THREAD( VOID );
  86. VOID EndThread( VOID );
  87. VOID RegisterThread( VOID );
  88. VOID SetThreadStackSize( LONG Stack = 0 );
  89. BOOLEAN StartThread
  90. (
  91. NEW_THREAD Function,
  92. VOID *Parameter = NULL,
  93. BOOLEAN Wait = True
  94. );
  95. VOID UnregisterThread( SBIT32 ThreadId = NoThread );
  96. BOOLEAN WaitForThreads( LONG WaitTime = INFINITE );
  97. ~THREAD( VOID );
  98. //
  99. // Public inline functions.
  100. //
  101. VOID SetThreadAffinity( BOOLEAN NewAffinity = True )
  102. { Affinity = NewAffinity; }
  103. VOID SetThreadPriority( BOOLEAN NewPriority = True )
  104. { Priority = NewPriority; }
  105. private:
  106. //
  107. // Private functions.
  108. //
  109. SBIT32 FindThread( SBIT32 ThreadId );
  110. //
  111. // Disabled operations.
  112. //
  113. THREAD( CONST THREAD & Copy );
  114. VOID operator=( CONST THREAD & Copy );
  115. };
  116. #endif