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.

121 lines
1.9 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: mutex.hxx
  8. //
  9. //--------------------------------------------------------------------------
  10. class MUTEX
  11. {
  12. public:
  13. MUTEX( DWORD * pStatus )
  14. {
  15. __try
  16. {
  17. InitializeCriticalSection(&c);
  18. }
  19. __except( EXCEPTION_EXECUTE_HANDLER )
  20. {
  21. *pStatus = GetExceptionCode();
  22. }
  23. }
  24. ~MUTEX()
  25. {
  26. DeleteCriticalSection(&c);
  27. }
  28. DWORD Enter()
  29. {
  30. DWORD Status = 0;
  31. __try
  32. {
  33. EnterCriticalSection(&c);
  34. }
  35. __except( EXCEPTION_EXECUTE_HANDLER )
  36. {
  37. Status = GetExceptionCode();
  38. }
  39. return Status;
  40. }
  41. DWORD Leave()
  42. {
  43. DWORD Status = 0;
  44. __try
  45. {
  46. LeaveCriticalSection(&c);
  47. }
  48. __except( EXCEPTION_EXECUTE_HANDLER )
  49. {
  50. Status = GetExceptionCode();
  51. }
  52. return Status;
  53. }
  54. private:
  55. CRITICAL_SECTION c;
  56. };
  57. class CLAIM_MUTEX
  58. {
  59. public:
  60. CLAIM_MUTEX( MUTEX * Mutex )
  61. : Lock( Mutex )
  62. {
  63. Taken = 0;
  64. Enter();
  65. }
  66. DWORD Enter()
  67. {
  68. DWORD Status = Lock->Enter();
  69. if (0 == Status)
  70. {
  71. ++Taken;
  72. }
  73. return Status;
  74. }
  75. DWORD Leave()
  76. {
  77. DWORD Status = Lock->Leave();
  78. if (0 == Status)
  79. {
  80. --Taken;
  81. }
  82. return Status;
  83. }
  84. ~CLAIM_MUTEX()
  85. {
  86. ASSERT( Taken >= 0 );
  87. while (Taken > 0)
  88. {
  89. Leave();
  90. }
  91. }
  92. private:
  93. signed Taken;
  94. MUTEX * Lock;
  95. };