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.

87 lines
1.7 KiB

  1. #ifndef PASSPORTLOCKEDINTERGER_HPP
  2. #define PASSPORTLOCKEDINTERGER_HPP
  3. #include <windows.h>
  4. #include <winbase.h>
  5. // a thread safe integer class..
  6. class PassportLockedInteger
  7. {
  8. public:
  9. PassportLockedInteger(LONG l = 0)
  10. {
  11. m_Long = l;
  12. }
  13. // returns the new value of the integer.
  14. LONG operator++()
  15. {
  16. return InterlockedIncrement(&m_Long);
  17. }
  18. // returns the new value of the integer.
  19. LONG operator--()
  20. {
  21. return InterlockedDecrement(&m_Long);
  22. }
  23. // returns the new value of the integer.
  24. LONG operator+=(LONG value)
  25. {
  26. return InterlockedExchangeAdd( &m_Long , value ) + value;
  27. }
  28. // returns the new value of the integer.
  29. LONG operator-=(LONG value)
  30. {
  31. return InterlockedExchangeAdd( &m_Long , -value ) - value;
  32. }
  33. // returns the current value of the integer.
  34. LONG value()
  35. {
  36. return m_Long;
  37. }
  38. private:
  39. LONG m_Long;
  40. };
  41. // smart wrapper class for PassportLockedInteger
  42. class CPassportLockedIntegerWrapper
  43. {
  44. public:
  45. // Constructor automatically does increment
  46. CPassportLockedIntegerWrapper(PassportLockedInteger *pLock)
  47. {
  48. m_pLock = pLock;
  49. if (m_pLock)
  50. {
  51. m_Value = ++(*m_pLock);
  52. }
  53. else
  54. {
  55. m_Value = 0;
  56. }
  57. }
  58. // Destructor automatically decrements
  59. ~CPassportLockedIntegerWrapper()
  60. {
  61. if (m_pLock)
  62. {
  63. --(*m_pLock);
  64. }
  65. }
  66. // returns the current value of the integer.
  67. LONG value()
  68. {
  69. return m_Value;
  70. }
  71. private:
  72. PassportLockedInteger *m_pLock;
  73. LONG m_Value;
  74. };
  75. #endif