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.

162 lines
6.4 KiB

  1. //================================================================================
  2. // Microsoft Confidential
  3. // Copyright (C) Microsoft 1997
  4. //
  5. // Author: RameshV
  6. //================================================================================
  7. //================================================================================
  8. // Required Headers
  9. //================================================================================
  10. #include <adt.h>
  11. //================================================================================
  12. // Definitions of the functions.
  13. //================================================================================
  14. DWORD // Win32 Errors
  15. InitializeProducerConsumer( // Initialize the pointer
  16. OUT PPRODCONS Synch, // The memory must be pre-allocated
  17. IN DWORD MaxProducers, // Total # of simultaneous producers
  18. IN DWORD MaxConsumers // Totat # of consumers
  19. ) {
  20. Synch->ProducerSemaphore = Synch->ConsumerSemaphore = NULL;
  21. Synch->ProducerSemaphore = CreateSemaphore(
  22. (LPSECURITY_ATTRIBUTES)NULL, // No security here
  23. MaxProducers, // Initial allowed # of producers
  24. MaxProducers, // Max allowed at any time
  25. NULL // Unnamed
  26. );
  27. if( NULL == Synch->ProducerSemaphore ) return GetLastError();
  28. Synch->ConsumerSemaphore = CreateSemaphore(
  29. (LPSECURITY_ATTRIBUTES)NULL, // No security here
  30. 0, // Initial allowed # of consumers
  31. MaxConsumers, // Max allowed at any time
  32. NULL // Unnamed
  33. );
  34. if( NULL == Synch->ConsumerSemaphore ) return GetLastError();
  35. Synch->MaxProducers = MaxProducers;
  36. Synch->MaxConsumers = MaxConsumers;
  37. return ERROR_SUCCESS;
  38. }
  39. VOID
  40. DestroyProducerConsumer( // Destroy and free up resources
  41. IN PPRODCONS Synch // The producer consumer object
  42. ) {
  43. if(Synch->ProducerSemaphore) CloseHandle(Synch->ProducerSemaphore);
  44. if(Synch->ConsumerSemaphore) CloseHandle(Synch->ConsumerSemaphore);
  45. }
  46. //================================================================================
  47. // StartProducerEx is expected to be called before trying to "produce" an
  48. // element. If a timeout is specified, then the timeout will act like the
  49. // same as in WaitForSingleObjectEx. Also, if the Alertable flag is on, the
  50. // wait is altertable. The same thing with StartConsumerEx.
  51. //================================================================================
  52. DWORD static _inline // Win32 errors
  53. StartProdConsExInternal( // Gen. fn to enter prod/cons code (internal)
  54. IN PPRODCONS Synch, // Synchronization object
  55. IN DWORD TimeOutMilliSec,// Timeout in milliseconds; 0 ==> polling
  56. IN BOOL Alertable, // Is the wait alertable?
  57. IN BOOL Producer // Is this call from a producer or consumer?
  58. ) {
  59. DWORD Status;
  60. Status = WaitForSingleObjectEx(
  61. Producer? Synch->ProducerSemaphore : Synch->ConsumerSemaphore,
  62. TimeOutMilliSec,
  63. Alertable
  64. );
  65. return Status;
  66. }
  67. DWORD static _inline // Win32 errors
  68. EndProdConsExInternal( // Gen. fn to leave prod/cons code(internal)
  69. IN PPRODCONS Synch, // Synchronization object
  70. IN BOOL Producer // Is this call from a producer or consumer?
  71. ) {
  72. BOOL Status;
  73. Status = ReleaseSemaphore(
  74. Producer? Synch->ConsumerSemaphore : Synch->ProducerSemaphore,
  75. 1, // Release one producer/consumer
  76. NULL // Dont care about how many are still out there
  77. );
  78. if( FALSE == Status ) return GetLastError();
  79. return ERROR_SUCCESS;
  80. }
  81. //================================================================================
  82. // The exported versions are below. The Ex functions allow specification of
  83. // TimeOut and if the wait is alertable. The non-Ex functions have the timeout
  84. // as INFINITE and Alertable as FALSE by default.
  85. //================================================================================
  86. DWORD // Win32 errors
  87. StartProducerEx( // See StartProdConsExInternal
  88. IN PPRODCONS Synch, // Syncho. object
  89. IN DWORD TimeOutMilliSec,// Timeout for wait in milliseconds
  90. IN BOOL Alertable // Is the wait alertable?
  91. ) {
  92. return StartProdConsExInternal(Synch,TimeOutMilliSec,Alertable,TRUE);
  93. }
  94. DWORD // Win32 errors
  95. StartConsumerEx( // See StartProdConsExInternal
  96. IN PPRODCONS Synch, // Syncho. object
  97. IN DWORD TimeOutMilliSec,// Timeout for wait in milliseconds
  98. IN BOOL Alertable // Is the wait alertable?
  99. ) {
  100. return StartProdConsExInternal(Synch,TimeOutMilliSec,Alertable,FALSE);
  101. }
  102. // These are usual versions. Return value is success if we entered.
  103. // Alertable wait by default, timeout = infinite. So, can return
  104. // WAIT_IO_COMPLETION on success.
  105. DWORD // Win32 errors
  106. StartProducer( // Wait until a producer can start
  107. IN PPRODCONS Synch // The synch object
  108. ) {
  109. DWORD Status = StartProducerEx(Synch, INFINITE, FALSE);
  110. if( WAIT_OBJECT_0 == Status ) return ERROR_SUCCESS;
  111. return Status;
  112. }
  113. DWORD // Win32 errors
  114. StartConsumer( // Wait until a consumer can start
  115. IN PPRODCONS Synch // The synch object
  116. ) {
  117. DWORD Status = StartConsumerEx(Synch, INFINITE, FALSE);
  118. if( WAIT_OBJECT_0 == Status ) return ERROR_SUCCESS;
  119. return Status;
  120. }
  121. DWORD // Win32 Errors
  122. EndProducer( // See EndProdConsExInternal
  123. IN PPRODCONS Synch // Synch object
  124. ) {
  125. return EndProdConsExInternal(Synch, TRUE);
  126. }
  127. DWORD // Win32 errors
  128. EndConsumer( // See EndProdConsExInternal
  129. IN PPRODCONS Synch // Synch object
  130. ) {
  131. return EndProdConsExInternal(Synch, TRUE);
  132. }
  133. //================================================================================
  134. // End of file
  135. //================================================================================