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.

135 lines
5.2 KiB

  1. // Ruler
  2. // 1 2 3 4 5 6 7 8
  3. //345678901234567890123456789012345678901234567890123456789012345678901234567890
  4. /********************************************************************/
  5. /* */
  6. /* The standard layout. */
  7. /* */
  8. /* The standard layout for 'cpp' files in this code is as */
  9. /* follows: */
  10. /* */
  11. /* 1. Include files. */
  12. /* 2. Constants local to the class. */
  13. /* 3. Data structures local to the class. */
  14. /* 4. Data initializations. */
  15. /* 5. Static functions. */
  16. /* 6. Class functions. */
  17. /* */
  18. /* The constructor is typically the first function, class */
  19. /* member functions appear in alphabetical order with the */
  20. /* destructor appearing at the end of the file. Any section */
  21. /* or function this is not required is simply omitted. */
  22. /* */
  23. /********************************************************************/
  24. #include "LibraryPCH.hpp"
  25. #include "Barrier.hpp"
  26. /********************************************************************/
  27. /* */
  28. /* Class constructor. */
  29. /* */
  30. /* Create a new barrier and initialize it. This call is not */
  31. /* thread safe and should only be made in a single thread */
  32. /* environment. */
  33. /* */
  34. /********************************************************************/
  35. BARRIER::BARRIER( VOID )
  36. {
  37. Waiting = 0;
  38. if ( (Semaphore = CreateSemaphore( NULL,0,MaxCpus,NULL )) == NULL)
  39. { Failure( "Create semaphore in constructor for BARRIER" ); }
  40. }
  41. /********************************************************************/
  42. /* */
  43. /* Synchronize Threads. */
  44. /* */
  45. /* Synchronize a collection of threads. */
  46. /* */
  47. /********************************************************************/
  48. VOID BARRIER::Synchronize( SBIT32 Expecting )
  49. {
  50. //
  51. // We make sure we are expecting at least two threads.
  52. // If not then do nothing.
  53. //
  54. if ( Expecting > 1 )
  55. {
  56. //
  57. // Cliam the lock.
  58. //
  59. Spinlock.ClaimLock();
  60. //
  61. // We see if the required number of threads has
  62. // arrived.
  63. //
  64. if ( Expecting > (++ Waiting) )
  65. {
  66. //
  67. // We are about to sleep so drop the lock.
  68. //
  69. Spinlock.ReleaseLock();
  70. //
  71. // The threads sleep here waiting for everyone
  72. // else to arrive.
  73. //
  74. if
  75. (
  76. WaitForSingleObject( Semaphore,INFINITE )
  77. !=
  78. WAIT_OBJECT_0
  79. )
  80. { Failure( "Sleep failed in Synchronize()" ); }
  81. }
  82. else
  83. {
  84. REGISTER SBIT32 Wakeup = (Expecting-1);
  85. //
  86. // The count of threads that have arrived is
  87. // updated and the number of threads that are
  88. // about to leave is updated.
  89. //
  90. Waiting -= Expecting;
  91. //
  92. // We are about to wake up all the sleepers so
  93. // drop the lock.
  94. //
  95. Spinlock.ReleaseLock();
  96. //
  97. // Wakeup any sleeping threads and exit.
  98. //
  99. if ( Wakeup > 0 )
  100. {
  101. if ( ! ReleaseSemaphore( Semaphore,Wakeup,NULL ) )
  102. { Failure( "Wakeup failed in Synchronize()" ); }
  103. }
  104. }
  105. }
  106. }
  107. /********************************************************************/
  108. /* */
  109. /* Class destructor. */
  110. /* */
  111. /* Destory a barrier. This call is not thread safe and should */
  112. /* only be made in a single thread environment. */
  113. /* */
  114. /********************************************************************/
  115. BARRIER::~BARRIER( VOID )
  116. {
  117. if ( ! CloseHandle( Semaphore ) )
  118. { Failure( "Close semaphore in destructor for BARRIER" ); }
  119. }