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.

91 lines
1.5 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. mutex.cpp
  5. Abstract:
  6. SIS Groveler named mutex class
  7. Authors:
  8. John Douceur, 1998
  9. Environment:
  10. User Mode
  11. Revision History:
  12. --*/
  13. #include "all.hxx"
  14. NamedMutex::NamedMutex(
  15. const _TCHAR *name,
  16. SECURITY_ATTRIBUTES *security_attributes)
  17. {
  18. ASSERT(this != 0);
  19. mutex_handle = CreateMutex(security_attributes, FALSE, name);
  20. if (mutex_handle == 0)
  21. {
  22. DWORD err = GetLastError();
  23. PRINT_DEBUG_MSG((_T("GROVELER: CreateMutex() failed with error %d\n"), err));
  24. }
  25. }
  26. NamedMutex::~NamedMutex()
  27. {
  28. ASSERT(this != 0);
  29. if (mutex_handle != 0)
  30. {
  31. int ok = CloseHandle(mutex_handle);
  32. if (!ok)
  33. {
  34. DWORD err = GetLastError();
  35. PRINT_DEBUG_MSG((_T("GROVELER: CloseHandle() failed with error %d\n"), err));
  36. }
  37. mutex_handle = 0;
  38. }
  39. }
  40. bool
  41. NamedMutex::release()
  42. {
  43. ASSERT(this != 0);
  44. if (mutex_handle == 0)
  45. {
  46. return false;
  47. }
  48. BOOL ok = ReleaseMutex(mutex_handle);
  49. if (!ok)
  50. {
  51. DWORD err = GetLastError();
  52. PRINT_DEBUG_MSG((_T("GROVELER: ReleaseMutex() failed with error %d\n"), err));
  53. }
  54. return (ok != 0);
  55. }
  56. bool
  57. NamedMutex::acquire(
  58. unsigned int timeout)
  59. {
  60. ASSERT(this != 0);
  61. if (mutex_handle == 0)
  62. {
  63. return false;
  64. }
  65. ASSERT(signed(timeout) >= 0);
  66. DWORD result = WaitForSingleObject(mutex_handle, timeout);
  67. if (result != WAIT_TIMEOUT && result != WAIT_OBJECT_0)
  68. {
  69. PRINT_DEBUG_MSG((_T("GROVELER: WaitForSingleObject() returned error %d\n"),
  70. result));
  71. }
  72. return (result == WAIT_OBJECT_0);
  73. }