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.

97 lines
1.5 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: mutex.hxx
  8. //
  9. //--------------------------------------------------------------------------
  10. class MUTEX
  11. {
  12. public:
  13. MUTEX( DWORD * pStatus )
  14. {
  15. NTSTATUS status = RtlInitializeCriticalSection(&c);
  16. ASSERT(!NT_SUCCESS(status) || c.DebugInfo);
  17. if (!NT_SUCCESS(*pStatus))
  18. {
  19. c.DebugInfo = NULL;
  20. *pStatus = ERROR_OUTOFMEMORY;
  21. }
  22. else
  23. {
  24. *pStatus = 0;
  25. }
  26. }
  27. ~MUTEX()
  28. {
  29. if (c.DebugInfo) RtlDeleteCriticalSection(&c);
  30. }
  31. void Enter()
  32. {
  33. RtlEnterCriticalSection(&c);
  34. }
  35. void Leave()
  36. {
  37. RtlLeaveCriticalSection(&c);
  38. }
  39. private:
  40. RTL_CRITICAL_SECTION c;
  41. };
  42. class CLAIM_MUTEX
  43. {
  44. public:
  45. CLAIM_MUTEX( MUTEX & Mutex )
  46. : Lock( Mutex )
  47. {
  48. Taken = 0;
  49. Enter();
  50. }
  51. void Enter()
  52. {
  53. Lock.Enter();
  54. ++Taken;
  55. }
  56. void Leave()
  57. {
  58. ASSERT( Taken > 0 );
  59. Lock.Leave();
  60. --Taken;
  61. }
  62. ~CLAIM_MUTEX()
  63. {
  64. ASSERT( Taken >= 0 );
  65. while (Taken > 0)
  66. {
  67. Leave();
  68. }
  69. }
  70. private:
  71. signed Taken;
  72. MUTEX & Lock;
  73. };