Windows NT 4.0 source code leak
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.

154 lines
3.9 KiB

4 years ago
  1. //--------------------------------------------------------------------------;
  2. //
  3. // File: FLocks.c
  4. //
  5. // Copyright (c) 1995 Microsoft Corporation. All Rights Reserved.
  6. //
  7. // Abstract:
  8. // This file implements the locking functions for protection of the Focus
  9. // Thread from other accesses to the gpdsinfo->pDSoundExternalObj. These
  10. // functions are particular to a fix such that the focustracker function
  11. // can not be protected by ENTER_DLL_CSECT. Thus a specific protection
  12. // on all destructive occurances of gpdsinfo->pDSoundExternalObj, and
  13. // in the Focus Tracker Thread is needed.
  14. //
  15. // Contents:
  16. // CreateFocusLock()
  17. // DestroyFocusLock()
  18. // GetFocusLock()
  19. // ReleaseFocusLock()
  20. //
  21. // History:
  22. // Date Person Reason
  23. // 12/10/95 angusm Initial Version
  24. //
  25. //--------------------------------------------------------------------------;
  26. #include "dsoundpr.h"
  27. #include "grace.h"
  28. #include "flocks.h"
  29. #define MUTEX_NAME "FocusLock49452"
  30. /* CreateFocusLock
  31. * This function should only be called once in focustracker. It creates
  32. * the named Mutex, and is protected by DLL_CSECT to prevent another thread
  33. * from manipulating DSoundExternalObj.
  34. *
  35. * IN: pointer to the Mutex Handle
  36. * OUT: TRUE on success, and FALSE otherwise(phMutex will be set to NULL)
  37. * SIDE EFFECTS: 1. Named Mutex is created
  38. * 2. phMutex is filled with the handle of the above
  39. *
  40. * REVISION HISTORY:
  41. * 12/10/95 angusm Initial Version
  42. */
  43. int CreateFocusLock (HANDLE *phMutex)
  44. {
  45. ASSERT (phMutex != NULL);
  46. *phMutex = CreateMutex (NULL, /* No security attribs */
  47. FALSE, /* initialy signaled */
  48. MUTEX_NAME);
  49. if (NULL == *phMutex)
  50. {
  51. return FALSE;
  52. }
  53. return TRUE;
  54. }
  55. /* DestroyFocusLock
  56. * This function closes the handle on the protection mutex. It should
  57. * only be called on a valid mutex.
  58. *
  59. * IN: a valid handle returned from CreateFocusLock
  60. * OUT: TRUE on success, and FALSE otherwise
  61. * SIDE EFFECTS: Mutex is closed out
  62. *
  63. * REVISION HISTORY:
  64. * 12/10/95 angusm Initial Version
  65. */
  66. int DestroyFocusLock (HANDLE hMutex)
  67. {
  68. if ((NULL == hMutex) ||
  69. (FALSE == CloseHandle (hMutex)))
  70. {
  71. return FALSE;
  72. }
  73. return TRUE;
  74. }
  75. /* GetFocusLock
  76. * This function attempt to get the named Mutex protecting
  77. * gpdsinfo->pDSoundExternalObj. If no Mutex exists this functions returns
  78. * successful. This allows calls to GetFocusLock only to use OpenMutex
  79. * instead of CreateMutex, which prevents the constant recreation of the
  80. * named mutex.
  81. *
  82. * IN: pointer to a HANDLE
  83. * OUT: TRUE on success, and FALSE otherwise(handle is nulled out)
  84. * SIDE EFFECTS: Mutex is taken
  85. *
  86. * REVISION HISTORY:
  87. * 12/10/95 angusm Initial Version
  88. */
  89. int GetFocusLock (HANDLE* phMutex)
  90. {
  91. DWORD dwResult;
  92. ASSERT (phMutex != NULL);
  93. *phMutex = OpenMutex (SYNCHRONIZE, /* Syncronize access */
  94. FALSE, /* No iheritance */
  95. MUTEX_NAME);
  96. if (NULL == *phMutex)
  97. {
  98. if (ERROR_INVALID_NAME == GetLastError()) return TRUE;
  99. return FALSE;
  100. }
  101. dwResult = WaitForSingleObjectEx(*phMutex, INFINITE, FALSE);
  102. if (WAIT_OBJECT_0 != dwResult) {
  103. CloseHandle (*phMutex);
  104. *phMutex = NULL;
  105. DPF (2, "Dsound: GetFocusLock: Error waiting for Focus Mutex");
  106. ASSERT (0);
  107. return FALSE;
  108. }
  109. return TRUE;
  110. }
  111. /* ReleaseFocusLock
  112. * This function closses the mutex handle, releases the lock. It must be
  113. * called with the handle of a previous GetFocusLock call in the same process
  114. * context.
  115. *
  116. * IN: a HANDLE
  117. * OUT: TRUE on success and FALSE otherwise
  118. * SIDE EFFECTS: The HANDLE is closed
  119. *
  120. * REVISION HISTORY:
  121. * 12/10/95 angusm Initial Version
  122. */
  123. int ReleaseFocusLock (HANDLE hMutex)
  124. {
  125. int nReturnValue = TRUE;
  126. if (NULL == hMutex) return FALSE;
  127. if (FALSE == ReleaseMutex (hMutex)) nReturnValue = FALSE;
  128. if (FALSE == CloseHandle (hMutex)) nReturnValue = FALSE;
  129. return nReturnValue;
  130. }