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.

130 lines
2.7 KiB

  1. /******************************************************************************
  2. *
  3. * Copyright (c) 2000 Microsoft Corporation
  4. *
  5. * Module Name:
  6. * rwlock.cpp
  7. *
  8. * Abstract:
  9. * Implements CLock - for synchronizing access to a resource
  10. *
  11. * Revision History:
  12. * Brijesh Krishnaswami (brijeshk) 04/13/2000
  13. * created
  14. *
  15. *****************************************************************************/
  16. #include <nt.h>
  17. #include <ntrtl.h>
  18. #include <nturtl.h>
  19. #include <windows.h>
  20. #include "utils.h"
  21. #include <accctrl.h>
  22. #include "dbgtrace.h"
  23. #include "srdefs.h"
  24. #ifdef THIS_FILE
  25. #undef THIS_FILE
  26. #endif
  27. static char __szTraceSourceFile[] = __FILE__;
  28. #define THIS_FILE __szTraceSourceFile
  29. //////////////////////////////////////////////////////////////////////
  30. // CLock - class that allows exclusive access to a resource
  31. // uses a mutex - does not differentiate between readers/writers
  32. CLock::CLock() // object constructor
  33. {
  34. hResource = NULL;
  35. }
  36. DWORD CLock::Init()
  37. {
  38. DWORD dwRc = ERROR_SUCCESS;
  39. //
  40. // first - try opening the mutex
  41. //
  42. hResource = OpenMutex(SYNCHRONIZE, FALSE, s_cszDSMutex);
  43. if (hResource)
  44. {
  45. goto done;
  46. }
  47. //
  48. // if doesn't exist - create it
  49. //
  50. hResource = CreateMutex(NULL, FALSE, s_cszDSMutex);
  51. dwRc = GetLastError();
  52. if (! hResource)
  53. goto done;
  54. if (dwRc == ERROR_SUCCESS)
  55. {
  56. dwRc = SetAclInObject(hResource,
  57. SE_KERNEL_OBJECT,
  58. STANDARD_RIGHTS_ALL | GENERIC_ALL,
  59. SYNCHRONIZE,
  60. FALSE);
  61. }
  62. else // we got a good handle, so we don't care
  63. {
  64. dwRc = ERROR_SUCCESS;
  65. }
  66. done:
  67. return dwRc;
  68. }
  69. CLock::~CLock() // object destructor
  70. {
  71. if (hResource)
  72. CloseHandle(hResource);
  73. }
  74. BOOL CLock::Lock(int iTimeOut) // Get access to the resource w/ Timeout
  75. {
  76. DWORD dwRc;
  77. BOOL fRet;
  78. DWORD dwId = GetCurrentThreadId();
  79. tenter("CLock::Lock");
  80. ASSERT(hResource != NULL);
  81. dwRc = WaitForSingleObject(hResource, iTimeOut);
  82. if (dwRc == WAIT_OBJECT_0 || dwRc == WAIT_ABANDONED)
  83. {
  84. trace(0, "Thread(%08x) got DS lock", dwId);
  85. fRet = TRUE;
  86. }
  87. else
  88. {
  89. trace(0, "Thread(%08x) cannot get DS lock", dwId);
  90. fRet = FALSE;
  91. }
  92. tleave();
  93. return fRet;
  94. }
  95. void CLock::Unlock() // Relinquish access to the resource
  96. {
  97. tenter("CLock::Unlock");
  98. DWORD dwId = GetCurrentThreadId();
  99. ASSERT(hResource != NULL);
  100. ReleaseMutex(hResource);
  101. trace(0, "Thread(%08x) released DS lock", dwId);
  102. tleave();
  103. }