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.

93 lines
2.0 KiB

  1. ///+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1993 - 1998.
  5. //
  6. // File: Mutex.Hxx
  7. //
  8. // Contents: Mutex classes
  9. //
  10. // Classes: CNamedMutex, CNamedMutexLock
  11. //
  12. // History: 29-March-94 t-joshh Created.
  13. //
  14. //----------------------------------------------------------------------------
  15. #pragma once
  16. //+---------------------------------------------------------------------------
  17. //
  18. // Class: CNamedMutex
  19. //
  20. // Purpose: Provide mutual exclusion among several processes
  21. //
  22. // History: 29-March-94 t-joshh Created.
  23. //
  24. // Notes: When creating a mutex, a well-known name must be provided.
  25. //
  26. //----------------------------------------------------------------------------
  27. class CNamedMutex
  28. {
  29. public :
  30. CNamedMutex() : _hNamedMutex( 0 ) {}
  31. void Init( WCHAR const * pwszNameMutex )
  32. {
  33. // Create with the process default security
  34. _hNamedMutex = CreateMutex( 0, FALSE, pwszNameMutex );
  35. if ( 0 == _hNamedMutex )
  36. THROW( CException() );
  37. }
  38. ~CNamedMutex ()
  39. {
  40. CloseHandle( _hNamedMutex );
  41. }
  42. ULONG Request(DWORD dwMilliseconds = INFINITE)
  43. {
  44. return WaitForSingleObject ( _hNamedMutex, dwMilliseconds );
  45. }
  46. void Release()
  47. {
  48. ReleaseMutex( _hNamedMutex );
  49. }
  50. private :
  51. HANDLE _hNamedMutex;
  52. };
  53. //+---------------------------------------------------------------------------
  54. //
  55. // Class: CNamedMutexLock
  56. //
  57. // Purpose: Gets and releases the lock
  58. //
  59. // History: 14-Jan-98 dlee Created.
  60. //
  61. //----------------------------------------------------------------------------
  62. class CNamedMutexLock
  63. {
  64. public:
  65. CNamedMutexLock( CNamedMutex & mutex ) :
  66. _mutex( mutex )
  67. {
  68. _mutex.Request();
  69. }
  70. ~CNamedMutexLock()
  71. {
  72. _mutex.Release();
  73. }
  74. private:
  75. CNamedMutex & _mutex;
  76. };