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.

150 lines
3.4 KiB

  1. /*******************************Module*Header*********************************\
  2. * Module Name: support.c
  3. *
  4. * MultiMedia Systems MIDI Sequencer DLL
  5. *
  6. * Created: 27-Feb-1992
  7. * Author: ROBINSP
  8. *
  9. * History:
  10. *
  11. * Copyright (c) 1985-1998 Microsoft Corporation
  12. *
  13. \******************************************************************************/
  14. #define UNICODE
  15. #include <windows.h>
  16. #include <mmsystem.h>
  17. #include <mmsys.h>
  18. CRITICAL_SECTION CritSec;
  19. CRITICAL_SECTION SeqCritSec;
  20. //
  21. // Cut-down critical section stuff
  22. //
  23. VOID InitCrit(VOID)
  24. {
  25. InitializeCriticalSection(&CritSec);
  26. InitializeCriticalSection(&SeqCritSec);
  27. }
  28. VOID DeleteCrit(VOID)
  29. {
  30. DeleteCriticalSection(&CritSec);
  31. DeleteCriticalSection(&SeqCritSec);
  32. }
  33. VOID EnterCrit(VOID)
  34. {
  35. EnterCriticalSection(&CritSec);
  36. }
  37. VOID LeaveCrit(VOID)
  38. {
  39. LeaveCriticalSection(&CritSec);
  40. }
  41. VOID EnterSeq(VOID)
  42. {
  43. EnterCriticalSection(&SeqCritSec);
  44. }
  45. VOID LeaveSeq(VOID)
  46. {
  47. LeaveCriticalSection(&SeqCritSec);
  48. Sleep(4); // Give someone else a chance
  49. }
  50. /*************************************************************************
  51. *
  52. * @doc MCISEQ
  53. *
  54. * @func UINT | TaskBlock | This function blocks the current
  55. * task context if its event count is 0.
  56. *
  57. * @rdesc Returns the message value of the signal sent.
  58. *
  59. ************************************************************************/
  60. UINT TaskBlock(VOID)
  61. {
  62. MSG msg;
  63. LeaveSeq();
  64. /*
  65. * Loop until we get the message we want
  66. */
  67. for (;;) {
  68. /*
  69. * Retrieve any message for task
  70. */
  71. GetMessage(&msg, NULL, 0, 0);
  72. /*
  73. * If the message is for a window dispatch it
  74. */
  75. if (msg.hwnd != NULL) {
  76. DispatchMessage(&msg);
  77. } else {
  78. break;
  79. }
  80. }
  81. EnterSeq();
  82. return msg.message;
  83. }
  84. /*************************************************************************
  85. *
  86. * @doc MCISEQ
  87. *
  88. * @func BOOL | TaskSignal | This function signals the specified
  89. * task, incrementing its event count and unblocking
  90. * it.
  91. *
  92. * @parm DWORD | dwThreadId | Thread ID from <f mmGetCurrentTask>.
  93. *
  94. * @parm UINT | Msg | Signal message to send.
  95. *
  96. * @rdesc Returns TRUE if the signal was sent, else FALSE if the message
  97. * queue was full.
  98. *
  99. * @xref mmTaskBlock mmTaskCreate
  100. *
  101. * @comm For predictable results, must only be called from a task
  102. * created with <f mmTaskCreate>.
  103. *
  104. ************************************************************************/
  105. BOOL TaskSignal(DWORD dwThreadId, UINT Msg)
  106. {
  107. return PostThreadMessage(dwThreadId, Msg, 0, 0);
  108. }
  109. /*************************************************************************
  110. *
  111. * @doc MCISEQ
  112. *
  113. * @func VOID | TaskWaitComplete | This function waits for the
  114. * specified task to terminate.
  115. *
  116. * @parm HANDLE | h | Task handle. For predictable results, get the
  117. * task handle from <f mmGetCurrentTask>.
  118. *
  119. * @rdesc No return code
  120. *
  121. ************************************************************************/
  122. VOID TaskWaitComplete(HANDLE h)
  123. {
  124. LeaveSeq();
  125. WaitForSingleObject(h, INFINITE); // Wait (no timeout) for thread to complete
  126. CloseHandle(h);
  127. // Note that the handle will be freed by thread itself.
  128. EnterSeq();
  129. }