Source code of Windows XP (NT5)
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.

84 lines
1.1 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. *pStatus = RtlInitializeCriticalSection(&c);
  16. }
  17. ~MUTEX()
  18. {
  19. RtlDeleteCriticalSection(&c);
  20. }
  21. void Enter()
  22. {
  23. RtlEnterCriticalSection(&c);
  24. }
  25. void Leave()
  26. {
  27. RtlLeaveCriticalSection(&c);
  28. }
  29. private:
  30. RTL_CRITICAL_SECTION c;
  31. };
  32. class CLAIM_MUTEX
  33. {
  34. public:
  35. CLAIM_MUTEX( MUTEX & Mutex )
  36. : Lock( Mutex )
  37. {
  38. Taken = 0;
  39. Enter();
  40. }
  41. void Enter()
  42. {
  43. Lock.Enter();
  44. ++Taken;
  45. }
  46. void Leave()
  47. {
  48. ASSERT( Taken > 0 );
  49. Lock.Leave();
  50. --Taken;
  51. }
  52. ~CLAIM_MUTEX()
  53. {
  54. ASSERT( Taken >= 0 );
  55. while (Taken > 0)
  56. {
  57. Leave();
  58. }
  59. }
  60. private:
  61. signed Taken;
  62. MUTEX & Lock;
  63. };