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.

215 lines
3.6 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1991 - 1999
  3. Module Name:
  4. interlck.hxx
  5. Abstract:
  6. A class which represents an integer on which you can perform
  7. interlocked increments and decrements lives in this file. This
  8. class will be implemented differently on every operating system
  9. where this support is necessary. This particular version is for
  10. NT and Win32.
  11. Author:
  12. Michael Montague (mikemon) 19-Nov-1991
  13. Revision History:
  14. --*/
  15. #ifndef __INTERLCK_HXX__
  16. #define __INTERLCK_HXX__
  17. class INTERLOCKED_INTEGER
  18. /*++
  19. Class Description:
  20. This class implements an integer on which you can perform interlocked
  21. increments and decrements.
  22. Fields:
  23. Integer - Contains the interlocked integer.
  24. --*/
  25. {
  26. private:
  27. long Integer;
  28. public:
  29. INTERLOCKED_INTEGER (
  30. IN long InitialValue
  31. );
  32. long
  33. Increment (
  34. );
  35. long
  36. Decrement (
  37. );
  38. long
  39. Exchange (
  40. IN long ExchangeValue
  41. );
  42. long
  43. CompareExchange(
  44. IN long NewValue,
  45. IN long Comparand
  46. );
  47. long
  48. GetInteger (
  49. );
  50. void
  51. SetInteger ( long
  52. );
  53. inline void SingleThreadedIncrement(void)
  54. {
  55. Integer ++;
  56. }
  57. inline void SingleThreadedIncrement(long nReferences)
  58. {
  59. Integer += nReferences;
  60. }
  61. };
  62. inline
  63. INTERLOCKED_INTEGER::INTERLOCKED_INTEGER (
  64. IN long InitialValue
  65. )
  66. /*++
  67. Routine Description:
  68. All this routine has got to do is to set the initial value of the
  69. integer.
  70. Arguments:
  71. InitialValue - Supplies the initial value for the integer contained
  72. in this.
  73. --*/
  74. {
  75. Integer = InitialValue;
  76. }
  77. inline long
  78. INTERLOCKED_INTEGER::Increment (
  79. )
  80. /*++
  81. Routine Description:
  82. This routine performs an interlocked increment of the integer
  83. contained in this. An interlocked increment is an atomic operation;
  84. if two threads each increment the interlocked integer, then the
  85. integer will be two larger than it was before in all cases.
  86. --*/
  87. {
  88. return((long) InterlockedIncrement(&Integer));
  89. }
  90. inline long
  91. INTERLOCKED_INTEGER::Decrement (
  92. )
  93. /*++
  94. Routine Description:
  95. This routine is the same as INTERLOCKED_INTEGER::Decrement, except
  96. that it decrements the integer rather than incrementing it.
  97. --*/
  98. {
  99. return((long) InterlockedDecrement(&Integer));
  100. }
  101. inline long
  102. INTERLOCKED_INTEGER::Exchange (
  103. IN long ExchangeValue
  104. )
  105. /*++
  106. Routine Description:
  107. This does the thread-safe exchange of 2 long values.
  108. --*/
  109. {
  110. return((long) InterlockedExchange(&Integer, ExchangeValue));
  111. }
  112. inline long
  113. INTERLOCKED_INTEGER::CompareExchange(
  114. IN long NewValue,
  115. IN long Comparand
  116. )
  117. /*++
  118. Routine Description:
  119. This does a thread-safe exchange iff the current value of the counter is
  120. equal to the value of the Comparand parameter
  121. Return Value:
  122. The original value of the counter. If not equal to the Comparand the
  123. value of the counter has not been changed by this call.
  124. --*/
  125. {
  126. return ((long) InterlockedCompareExchange(&Integer, NewValue, Comparand));
  127. }
  128. inline long
  129. INTERLOCKED_INTEGER::GetInteger (
  130. )
  131. /*++
  132. Routine Description:
  133. This routine returns the current value of the integer.
  134. --*/
  135. {
  136. return(Integer);
  137. }
  138. inline void
  139. INTERLOCKED_INTEGER::SetInteger ( long value
  140. )
  141. /*++
  142. Routine Description:
  143. This routine sets the current value of the integer.
  144. --*/
  145. {
  146. Integer = value;
  147. }
  148. #endif // __INTERLCK_HXX__