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.

193 lines
4.5 KiB

  1. /*
  2. *
  3. * REVISIONS:
  4. * pcy16Jul93: Added NT semaphores
  5. * ash10Jun96: Cleaned up the class - overloaded the constructor
  6. * and added logic to handle interprocess synchronization
  7. *
  8. */
  9. #include "cdefine.h"
  10. extern "C"
  11. {
  12. #include <windows.h>
  13. }
  14. #include "mutexnt.h"
  15. #include "err.h"
  16. /* -------------------------------------------------------------------------
  17. ApcMutexLock::ApcMutexLock() - The mutex is created as unnamed and shared
  18. ------------------------------------------------------------------------- */
  19. ApcMutexLock::ApcMutexLock(VOID) :
  20. MutexLock()
  21. {
  22. SetObjectStatus(ErrNO_ERROR);
  23. theSemHand = CreateMutex((LPSECURITY_ATTRIBUTES)NULL,
  24. FALSE,
  25. (LPTSTR)NULL);
  26. if(!theSemHand)
  27. {
  28. SetObjectStatus(ErrSEM_CREATE_FAILED);
  29. }
  30. }
  31. /* -------------------------------------------------------------------------
  32. ApcMutexLock::ApcMutexLock() - creates an named mutex for interprocess
  33. synchronization
  34. ------------------------------------------------------------------------- */
  35. ApcMutexLock::ApcMutexLock(PCHAR aUniqueMutexName) : MutexLock()
  36. {
  37. SetObjectStatus(ErrNO_ERROR);
  38. theSemHand = CreateMutex((LPSECURITY_ATTRIBUTES)NULL,
  39. FALSE,
  40. (LPTSTR)aUniqueMutexName);
  41. if(!theSemHand )
  42. {
  43. SetObjectStatus(ErrSEM_CREATE_FAILED);
  44. }
  45. }
  46. /* -------------------------------------------------------------------------
  47. ApcMutexLock::~ApcMutexLock() - Close the mutex handle
  48. ------------------------------------------------------------------------- */
  49. ApcMutexLock::~ApcMutexLock()
  50. {
  51. CloseHandle(theSemHand);
  52. }
  53. /* -------------------------------------------------------------------------
  54. ApcMutexLock::GetExistingMutex() - Opens the handle to an existing
  55. mutex by specifying the mutex name. We need full access to the mutex object
  56. before the mutex handle can be used in any of the wait functions.
  57. ------------------------------------------------------------------------- */
  58. INT ApcMutexLock::GetExistingMutex(TCHAR aMutexName)
  59. {
  60. INT err = ErrNO_ERROR;
  61. theSemHand = OpenMutex(MUTEX_ALL_ACCESS, // Request full access
  62. FALSE,
  63. (LPCTSTR)aMutexName); // Mutex name
  64. if (!theSemHand )
  65. {
  66. err = ErrSEM_GENERAL_ERROR;
  67. }
  68. return err;
  69. }
  70. /* -------------------------------------------------------------------------
  71. ApcMutexLock::TimedRequest() - Wait for lock to be available
  72. ulTimeout: 0 - Don't wait at all
  73. <0 - Wait forever (same as Request())
  74. >0 - Wait for ulTimeout milliseconds
  75. ------------------------------------------------------------------------- */
  76. INT ApcMutexLock::TimedRequest(LONG ulTimeout)
  77. {
  78. INT err;
  79. DWORD time_out = (ulTimeout < 0) ? INFINITE : ulTimeout;
  80. DWORD waitresult = WaitForSingleObject(theSemHand, time_out);
  81. if(waitresult == 0)
  82. {
  83. err = ErrNO_ERROR;
  84. }
  85. else if (waitresult == WAIT_TIMEOUT)
  86. {
  87. err = ErrSEM_TIMED_OUT;
  88. }
  89. else
  90. {
  91. err = ErrSEM_GENERAL_ERROR;
  92. }
  93. return err;
  94. }
  95. /* -------------------------------------------------------------------------
  96. ApcMutexLock::IsHeld() - Checks the state of the mutex
  97. ------------------------------------------------------------------------- */
  98. INT ApcMutexLock::IsHeld(VOID)
  99. {
  100. INT ret;
  101. DWORD waitresult = WaitForSingleObject(theSemHand ,(DWORD)0);
  102. if(waitresult == 0)
  103. {
  104. ret = ErrNO_ERROR;
  105. }
  106. else
  107. {
  108. ret = ErrSEM_BLOCK_NG;
  109. }
  110. return ret;
  111. }
  112. /* -------------------------------------------------------------------------
  113. ApcMutexLock::Release() - Releases control of a mutex previously held
  114. ------------------------------------------------------------------------- */
  115. INT ApcMutexLock::Release(VOID)
  116. {
  117. INT err;
  118. if(!ReleaseMutex(theSemHand ))
  119. {
  120. err =ErrSEM_GENERAL_ERROR;
  121. }
  122. else
  123. {
  124. err = ErrNO_ERROR;
  125. }
  126. return err;
  127. }
  128. /* -------------------------------------------------------------------------
  129. ApcMutexLock::Wait() - Waits indefinitely for a mutex to be released
  130. ------------------------------------------------------------------------- */
  131. INT ApcMutexLock::Wait()
  132. {
  133. DWORD waitresult = WaitForSingleObject(theSemHand , INFINITE);
  134. if(waitresult == 0)
  135. {
  136. return ErrNO_ERROR;
  137. }
  138. else
  139. {
  140. return ErrSEM_GENERAL_ERROR;
  141. }
  142. }